diff options
author | Akshay" (aider) <[email protected]> | 2024-11-08 22:28:19 +0000 |
---|---|---|
committer | Akshay" (aider) <[email protected]> | 2024-11-08 22:28:19 +0000 |
commit | df4311103042117f005a133f88bc3cc858878550 (patch) | |
tree | 5dd7de8ce9e40dfbbb029c9e3c71a35720e53921 /src/routes/index.js | |
parent | 8dbc811d49cef793b4d6405b2b1d9f6cc71fa619 (diff) |
feat: Add unsubscribe endpoint to handle user unsubscriptions
Diffstat (limited to 'src/routes/index.js')
-rw-r--r-- | src/routes/index.js | 26 |
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 | ||
127 | router.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 | |||
127 | module.exports = router; | 153 | module.exports = router; |
128 | 154 | ||
129 | function unescape_submission(response) { | 155 | function unescape_submission(response) { |