aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-11-09 09:50:13 +0000
committerAkshay" (aider) <[email protected]>2024-11-09 09:50:13 +0000
commit7a727510109c1062ac9009df88db992791ee1ce8 (patch)
tree1103df8646e7d37c792ace2cb660d271fc24b6ce /src
parent6c6c9a6cf021cee7877a96d34a953d2885ef8a42 (diff)
refactor: Replace db.run with db.query and update database name
Diffstat (limited to 'src')
-rw-r--r--src/index.js10
-rw-r--r--src/routes/index.js8
2 files changed, 11 insertions, 7 deletions
diff --git a/src/index.js b/src/index.js
index ae9d87b..cc7e2e6 100644
--- a/src/index.js
+++ b/src/index.js
@@ -13,9 +13,9 @@ app.use(express.json());
13app.use(express.static(path.join(__dirname, "public"))); 13app.use(express.static(path.join(__dirname, "public")));
14app.use("/", routes); 14app.use("/", routes);
15 15
16const db = new Database("users.db"); 16const db = new Database("readit.db");
17 17
18db.run(` 18const createUsers = db.query(`
19 CREATE TABLE IF NOT EXISTS users ( 19 CREATE TABLE IF NOT EXISTS users (
20 id INTEGER PRIMARY KEY AUTOINCREMENT, 20 id INTEGER PRIMARY KEY AUTOINCREMENT,
21 username TEXT UNIQUE, 21 username TEXT UNIQUE,
@@ -23,7 +23,9 @@ db.run(`
23 ) 23 )
24`); 24`);
25 25
26db.run(` 26createUsers.run();
27
28const createSubs = db.query(`
27 CREATE TABLE IF NOT EXISTS subscriptions ( 29 CREATE TABLE IF NOT EXISTS subscriptions (
28 id INTEGER PRIMARY KEY AUTOINCREMENT, 30 id INTEGER PRIMARY KEY AUTOINCREMENT,
29 user_id INTEGER, 31 user_id INTEGER,
@@ -33,6 +35,8 @@ db.run(`
33 ) 35 )
34`); 36`);
35 37
38createSubs.run();
39
36module.exports = { db }; 40module.exports = { db };
37 41
38const port = process.env.READIT_PORT; 42const port = process.env.READIT_PORT;
diff --git a/src/routes/index.js b/src/routes/index.js
index 93ebf46..6153692 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -82,10 +82,10 @@ router.post("/register", async (req, res) => {
82 return res.status(400).send("Passwords do not match"); 82 return res.status(400).send("Passwords do not match");
83 } 83 }
84 try { 84 try {
85 db.run("INSERT INTO users (username, password) VALUES (?, ?)", [ 85 db.query("INSERT INTO users (username, password) VALUES (?, ?)", [
86 username, 86 username,
87 password, 87 password,
88 ]); 88 ]).run();
89 res.status(201).send("User registered successfully"); 89 res.status(201).send("User registered successfully");
90 } catch (err) { 90 } catch (err) {
91 console.log(err); 91 console.log(err);
@@ -125,10 +125,10 @@ router.post("/subscribe", async (req, res) => {
125 if (existingSubscription) { 125 if (existingSubscription) {
126 res.status(400).send("Already subscribed to this subreddit"); 126 res.status(400).send("Already subscribed to this subreddit");
127 } else { 127 } else {
128 db.run("INSERT INTO subscriptions (user_id, subreddit) VALUES (?, ?)", [ 128 db.query("INSERT INTO subscriptions (user_id, subreddit) VALUES (?, ?)", [
129 user.id, 129 user.id,
130 subreddit, 130 subreddit,
131 ]); 131 ]).run();
132 res.status(201).send("Subscribed successfully"); 132 res.status(201).send("Subscribed successfully");
133 } 133 }
134 } else { 134 } else {