From df4311103042117f005a133f88bc3cc858878550 Mon Sep 17 00:00:00 2001 From: "Akshay\" (aider)" Date: Fri, 8 Nov 2024 22:28:19 +0000 Subject: feat: Add unsubscribe endpoint to handle user unsubscriptions --- src/routes/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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) => { } }); +router.post("/unsubscribe", async (req, res) => { + const { username, subreddit } = req.body; + const user = db + .query("SELECT * FROM users WHERE username = ?", [username]) + .get(); + if (user) { + const existingSubscription = db + .query("SELECT * FROM subscriptions WHERE user_id = ? AND subreddit = ?", [ + user.id, + subreddit, + ]) + .get(); + if (existingSubscription) { + db.run("DELETE FROM subscriptions WHERE user_id = ? AND subreddit = ?", [ + user.id, + subreddit, + ]); + res.status(200).send("Unsubscribed successfully"); + } else { + res.status(400).send("Subscription not found"); + } + } else { + res.status(404).send("User not found"); + } +}); + module.exports = router; function unescape_submission(response) { -- cgit v1.2.3