-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (202 loc) · 5.89 KB
/
index.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
//jshint esversion:6
import express from "express";
import bodyParser from "body-parser";
import { config } from "dotenv";
import cookieSession from "cookie-session";
config();
import {
verifyUser,
getUserData,
getUserID,
checkUserExists,
createUser,
} from "./public/data/user.js";
import {
getClassData,
updateClass,
createClass,
deleteClass,
} from "./public/data/class.js";
import {
setAttendance,
getAttendance,
deleteAttendance,
} from "./public/data/attendance.js";
// express
const app = express();
//body-parser
app.use(bodyParser.urlencoded({ extended: true }));
// public folder
app.use(express.static("public"));
//path
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// use ejs
app.set("view engine", "ejs");
// use a session
app.use(
cookieSession({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
secure: !process.env.DEVELOPMENT, // Set secure to true if using HTTPS
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days in milliseconds
},
})
);
// Routes
// home
app.get("/", async function (req, res) {
console.log(req.session.userID);
if (req.session.userID) {
console.log("inside home");
const userID = req.session.userID.UserID;
console.log("userID: ", userID);
const userData = await getUserData(userID);
console.log("userData: ", userData);
const classNames = await getClassData(userID);
const attendance = await getAttendance(userID);
res.render("home", {
user: userData,
classes: classNames,
attendance: attendance,
});
} else res.redirect("login");
});
// login
app.get("/login", function (req, res) {
if (req.session.userID) res.redirect("/");
else res.render("login");
});
// post -> get data from login
app.post("/login", async function (req, res) {
console.log("req.body", req.body);
const { email, password } = req.body;
if ((await checkUserExists(email)) && (await verifyUser(email, password))) {
console.log("getUserID", await getUserID(email));
req.session.userID = await getUserID(email);
console.log(req.session.userID);
res.redirect("/");
} else {
res.redirect("/login");
}
});
// register
app.get("/register", function (req, res) {
if (req.session.userID) res.redirect("/");
else res.render("register");
});
// post -> get data from register
app.post("/register", async function (req, res) {
console.log("req.body", req.body);
const { username, email, password, firstName, lastName } = req.body;
console.log(
"register info: ",
username,
email,
password,
firstName,
lastName
);
const user = await createUser(username, email, password, firstName, lastName);
console.log("register done: ", user);
// save user id in session cookie
req.session.userID = await getUserID(email);
res.redirect("/");
});
// post -> get form input for classNames and process it
app.post("/attendance-form", async function (req, res) {
const checked = [];
console.log("attendance body: ", req.body);
const status = req.body["status"];
const skipReason =
req.body["skipReason"] === "" ? null : req.body["skipReason"];
console.log("attendance status: ", req.body);
for (const key in req.body) {
if (req.body[key] === "on") checked.push(key);
}
console.log("Checked checkboxes: ", checked);
const userID = req.session.userID.UserID;
for (let item in checked) {
// change totalAttended and totalTaken in Classes table
let updated = await updateClass(checked[item], status);
console.log(updated);
console.log("item: ", item);
// create attendance
let created = await setAttendance(
checked[item],
userID,
status,
skipReason
);
console.log(created);
}
res.redirect("/");
});
// logout
app.get("/logout", (req, res) => {
console.log("logout");
req.session = null;
res.redirect("/login");
});
// profile
app.get("/profile", async function (req, res) {
if (typeof req.session.userID == "undefined") {
res.redirect("/");
} else {
console.log("inside profile: ", req.session.userID);
const userID = req.session.userID.UserID;
const userData = await getUserData(userID);
const classes = await getClassData(userID);
console.log("userData: ", userData);
res.render("profile", { user: userData, classes: classes });
}
});
// timetable
app.get("/timetable", async function (req, res) {
if (typeof req.session.userID == "undefined") {
res.redirect("/");
} else {
console.log("inside timetable: ", req.session.userID);
const userID = req.session.userID.UserID;
const classNames = await getClassData(userID);
console.log("classNames: ", classNames);
res.render("timetable", { classes: classNames });
}
});
app.post("/create-class", async function (req, res) {
const userID = req.session.userID.UserID;
const { className, totalClassAttended, totalClassTaken, StartDate } =
req.body;
console.log("create-class: ", req.body);
await createClass(
userID,
className,
totalClassAttended,
totalClassTaken,
StartDate
);
res.redirect("/timetable");
});
app.post("/delete-class", async function (req, res) {
console.log("delete-class: ", req.body);
const { classID } = req.body;
console.log("classID: ", classID);
let deleteResult = await deleteClass(classID);
if (deleteResult == "no delete") console.log(deleteResult);
res.redirect("/timetable");
});
app.post("/delete-attendance", async function (req, res) {
console.log("delete-attendance: ", req.body);
const { attendanceID } = req.body;
console.log("attendanceID: ", attendanceID);
let deleteResult = await deleteAttendance(attendanceID);
if (deleteResult == "no delete") console.log(deleteResult);
res.redirect("/");
});
app.listen(3000, function () {
console.log("App listening on port 3000!");
});