-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
325 lines (292 loc) · 8.84 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const express = require("express");
const cors = require("cors");
const bcrypt = require("bcrypt");
const pool = require("./db");
const jwtGenerator = require("./utils/jwtGenerator");
const auth = require("./middleware/auth");
const PORT = process.env.PORT;
const path = require("path");
const app = express();
app.use(express.json());
app.use(cors());
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
}
//-------------------------------------------------- AUTHENTICATION AND AUTHORIZATION ---------------------------------
//authenticating new user using jwt
app.post("/registerUser", (req, res) => {
const { username, email, password } = req.body;
pool.query(
"SELECT * FROM users WHERE username=$1",
[username],
(err, user) => {
if (user.rows.length !== 0) {
return res.json("User already exist");
}
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(password, salt, (err, hash) => {
pool.query(
"INSERT INTO users (username,email,password) VALUES ($1,$2,$3) RETURNING *",
[username, email, hash],
(err, user) => {
const token = jwtGenerator(user.rows[0].user_id);
res.json({ token });
},
);
});
});
},
);
});
//signing in the user if registered
app.post("/loginUser", (req, res) => {
const { username, password } = req.body;
pool.query(
"SELECT * FROM users WHERE username=$1",
[username],
(err, user) => {
if (err) res.json(err);
if (user.rows.length === 0) {
return res.json("Email or Password is incorrect!");
}
bcrypt.compare(password, user.rows[0].password, (err, result) => {
if (err) throw err;
else if (!result) res.json("Email or Password is incorrect");
else {
const token = jwtGenerator(user.rows[0].user_id);
res.json({ token });
}
});
},
);
});
//checking if user is authorized
app.get("/verified", auth, (req, res) => {
res.json(true);
});
//sending user_id to frontend
app.get("/dash", auth, (req, res) => {
res.json(req.user);
});
// ---------------------------------------------------- ASSIGNMENTS -------------------------------------------------
//adding a new task
app.post("/newTask", (req, res) => {
const { user_id, sub, des } = req.body;
pool.query(
"INSERT INTO tasks (user_id,sub,des) VALUES ($1,$2,$3) RETURNING *",
[user_id, sub, des],
(err, result) => {
if (err) res.json("Not Authorized");
if (result) res.json("New Task Added");
},
);
});
//sending all the tasks
app.get("/getTask/:id", (req, res) => {
const id = req.params.id;
pool.query("SELECT * FROM tasks WHERE user_id=$1", [id], (err, task) => {
if (err) res.json("Not Authorized");
if (task) res.json(task.rows);
});
});
//sending a single task
app.get("/getSingle/:id", (req, res) => {
const id = req.params.id;
pool.query("SELECT * FROM tasks WHERE task_id=$1", [id], (err, task) => {
if (task) res.json(task.rows[0]);
});
});
//updating a single task
app.put("/updateTask/:task_id/:sub/:des", (req, res) => {
const { task_id, sub, des } = req.params;
pool.query(
"UPDATE tasks SET sub=$1,des=$2 WHERE task_id=$3",
[sub, des, task_id],
(err, result) => {
if (err) throw err;
if (result) res.json("Task Updated SuccessFully");
},
);
});
//deleting a single Task
app.delete("/deleteTask/:taskId", (req, res) => {
const { taskId } = req.params;
pool.query("DELETE FROM tasks WHERE task_id=$1", [taskId], (err, result) => {
if (err) throw err;
if (result) res.json("Task Deleted SuccessFully");
});
});
//---------------------------------------------------------- GRADES --------------------------------------------------------
//adding a new subject and exams for that particular subject
app.post("/newSub", (req, res) => {
const { user_id, subject } = req.body;
pool.query(
"INSERT INTO subjects (user_id,sub) VALUES ($1,$2) RETURNING *",
[user_id, subject],
(err, result) => {
if (err) throw err;
if (result) res.json("New Subject Added");
},
);
});
//adding a new exam to a particular subject
app.post("/newExam", (req, res) => {
const { subject, exam_name, max_marks, scored } = req.body;
pool.query(
"INSERT INTO exams VALUES($1,$2,$3,$4) RETURNING *",
[subject, exam_name, max_marks, scored],
(err, result) => {
if (err) throw err;
if (result) res.json("Exam Info Added");
},
);
});
//getting all the subjects
app.get("/getSubs/:id", (req, res) => {
const { id } = req.params;
pool.query("SELECT sub FROM subjects WHERE user_id=$1", [id], (err, sub) => {
if (err) {
res.json("Not Authorized");
} else if (sub) {
res.json(sub.rows);
}
});
});
//getting exam records
app.get("/getExamInfo/:sub", (req, res) => {
const { sub } = req.params;
pool.query("SELECT * FROM exams WHERE sub=$1", [sub], (err, exam) => {
if (err) throw err;
if (exam) res.json(exam.rows);
});
});
//update exam details
app.put("/updateInfo", (req, res) => {
const { prev_name, exam_name, max_marks, scored, sub } = req.body;
pool.query(
"UPDATE exams SET exam_name=$1,max_marks=$2,scored=$3 WHERE sub=$4 AND exam_name=$5",
[exam_name, max_marks, scored, sub, prev_name],
(err, result) => {
if (err) throw err;
if (result) res.json("Exam Info Updated");
},
);
});
//deleting the subject and all its relevant exams
app.delete("/deleteSub/:sub", (req, res) => {
const { sub } = req.params;
pool.query("DELETE FROM exams WHERE sub=$1", [sub], (err, result) => {
if (err) throw err;
if (result) {
pool.query("DELETE FROM subjects WHERE sub=$1", [sub], (err, del) => {
if (err) throw err;
if (del) res.json("Exam Info And Subject Deleted");
});
}
});
});
//---------------------------------------------------------- ATTENDANCE ------------------------------------------------------
//adding new subject to attendance column
app.post("/newAtt", (req, res) => {
const { user_id, sub_att } = req.body;
pool.query(
"INSERT INTO attendance VALUES ($1,$2) RETURNING *",
[user_id, sub_att],
(err, result) => {
if (err) throw err;
if (result) res.json("New Class Added");
},
);
});
//adding attendance records to each subject
app.post("/subAtt", (req, res) => {
const { sub_att, attended, tot_classes } = req.body;
pool.query(
"INSERT INTO att VALUES ($1,$2,$3) RETURNING *",
[sub_att, attended, tot_classes],
(err, result) => {
if (err) throw err;
},
);
});
//displaying the subjects
app.get("/getAtt/:id", (req, res) => {
const { id } = req.params;
pool.query("SELECT * FROM attendance WHERE user_id=$1", [id], (err, subs) => {
if (err) throw err;
if (subs) res.json(subs.rows);
});
});
//displaying attendace records
app.get("/getSubAtt/:sub", (req, res) => {
const { sub } = req.params;
pool.query("SELECT * FROM att WHERE sub_att=$1", [sub], (err, att) => {
if (err) throw err;
if (att) res.json(att.rows);
});
});
//update the records
app.put("/updateAtt", (req, res) => {
const { sub_att, attended, tot_classes } = req.body;
pool.query(
"UPDATE att SET attended=$1,tot_classes=$2 WHERE sub_att=$3",
[attended, tot_classes, sub_att],
(err, result) => {
if (err) throw err;
if (result) res.json("Record Set");
},
);
});
//incrementing attended classes and total classes
app.put("/attended", (req, res) => {
const { sub_att } = req.body;
pool.query(
"UPDATE att SET attended=attended+1,tot_classes=tot_classes+1 WHERE sub_att=$1",
[sub_att],
(err, result) => {
if (err) throw err;
},
);
});
//inrementing only total classes
app.put("/missed", (req, res) => {
const { sub_att } = req.body;
pool.query(
"UPDATE att SET tot_classes=tot_classes+1 WHERE sub_att=$1",
[sub_att],
(err, result) => {
if (err) throw err;
},
);
});
//deleting records and the subject
app.delete("/delete/:sub", (req, res) => {
// const { sub } = req.params;
const { sub } = req.params;
pool.query("DELETE FROM att WHERE sub_att=$1", [sub], (err, result) => {
if (err) throw err;
if (result) {
pool.query(
"DELETE FROM attendance WHERE sub_att=$1",
[sub],
(err, done) => {
if (err) throw err;
if (done) res.json("Attendance Record and Subject Deleted");
},
);
}
});
});
//----------------------------------------------------------------------------------------------------------------------------
//catch-all method for client side rendering
app.get("/*", (req, res) => {
try {
res.sendFile(path.join(__dirname, "client/build/index.html"));
} catch (error) {
res.send(500).send(error);
}
});
//port
app.listen(PORT, () => {
console.log(`server started on ${PORT}`);
});