aboutsummaryrefslogtreecommitdiff
path: root/src/routes/index.js
diff options
context:
space:
mode:
authorAkshay" (aider) <[email protected]>2024-11-08 22:27:42 +0000
committerAkshay" (aider) <[email protected]>2024-11-08 22:27:42 +0000
commit8dbc811d49cef793b4d6405b2b1d9f6cc71fa619 (patch)
tree9d784999b7cb4cc136ef4055fb06acfd2cc7e0e3 /src/routes/index.js
parent25397b7cf0e48665b4499b881909f505de9e899b (diff)
fix: Return error when subscribing to the same subreddit more than once
Diffstat (limited to 'src/routes/index.js')
-rw-r--r--src/routes/index.js20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/routes/index.js b/src/routes/index.js
index f703d2e..a1153d8 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -104,11 +104,21 @@ router.post("/subscribe", async (req, res) => {
104 .query("SELECT * FROM users WHERE username = ?", [username]) 104 .query("SELECT * FROM users WHERE username = ?", [username])
105 .get(); 105 .get();
106 if (user) { 106 if (user) {
107 db.run("INSERT INTO subscriptions (user_id, subreddit) VALUES (?, ?)", [ 107 const existingSubscription = db
108 user.id, 108 .query("SELECT * FROM subscriptions WHERE user_id = ? AND subreddit = ?", [
109 subreddit, 109 user.id,
110 ]); 110 subreddit,
111 res.status(201).send("Subscribed successfully"); 111 ])
112 .get();
113 if (existingSubscription) {
114 res.status(400).send("Already subscribed to this subreddit");
115 } else {
116 db.run("INSERT INTO subscriptions (user_id, subreddit) VALUES (?, ?)", [
117 user.id,
118 subreddit,
119 ]);
120 res.status(201).send("Subscribed successfully");
121 }
112 } else { 122 } else {
113 res.status(404).send("User not found"); 123 res.status(404).send("User not found");
114 } 124 }