aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/routes/index.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/routes/index.js b/src/routes/index.js
index a1153d8..069cca8 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -124,6 +124,32 @@ router.post("/subscribe", async (req, res) => {
124 } 124 }
125}); 125});
126 126
127router.post("/unsubscribe", async (req, res) => {
128 const { username, subreddit } = req.body;
129 const user = db
130 .query("SELECT * FROM users WHERE username = ?", [username])
131 .get();
132 if (user) {
133 const existingSubscription = db
134 .query("SELECT * FROM subscriptions WHERE user_id = ? AND subreddit = ?", [
135 user.id,
136 subreddit,
137 ])
138 .get();
139 if (existingSubscription) {
140 db.run("DELETE FROM subscriptions WHERE user_id = ? AND subreddit = ?", [
141 user.id,
142 subreddit,
143 ]);
144 res.status(200).send("Unsubscribed successfully");
145 } else {
146 res.status(400).send("Subscription not found");
147 }
148 } else {
149 res.status(404).send("User not found");
150 }
151});
152
127module.exports = router; 153module.exports = router;
128 154
129function unescape_submission(response) { 155function unescape_submission(response) {