This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
222 lines (183 loc) · 5.45 KB
/
app.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
const port = process.env.PORT || 3000;
const moment = require("moment");
// DB PART
require("./db/connect");
const User = require("./models/user");
const Client = require("./models/client");
//routing part
const express = require("express");
const app = express();
const path = require("path");
//all static work
app.use(express.static("public"));
// app.use('/css', express.static(__dirname + '/public/css'));
app.use(express.static("public/css"));
app.use(express.static("public/wow"));
app.use(express.static("public/assets"));
app.use("/scripts", express.static(__dirname + "/public/scripts"));
app.use("/img", express.static(__dirname + "/public/img"));
//ejs and REST part
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set("views", path.join(__dirname, "/views"));
app.set("view engine", "ejs");
//url handler
app.get("", (req, res) => {
res.render("home");
});
app.get("/start", (req, res) => {
res.render("start");
});
app.get("/usrlogin", (req, res) => {
res.render("usrlogin");
});
app.post("/usrlogin", (req, res) => {
let { username, gender, avatarCode } = req.body;
console.log(avatarCode);
username = username.toLowerCase();
res.redirect(`/${username}/make?gender=${gender}&avc=${avatarCode}`);
});
app.get("/:id/make", (req, res) => {
const { id: username } = req.params;
const { gender } = req.query;
const { avc: avatarCode } = req.query;
const date = moment(new Date()).format("YYYY-MM-DD");
res.render("make", { username, gender, date, avatarCode });
});
app.post("/make", async (req, res) => {
const body = req.body;
const username = body.username;
const avatarCode = body.avatarCode;
console.log(body);
const filter = { username };
const update = body;
let doc = await User.findOneAndUpdate(filter, update, {
new: true,
upsert: true,
})
.then(() => console.log("successfully Uploaded User QuestionSet"))
.catch((e) => console.log(e));
res.send({
message: "we received your cooked questions",
redirect: `/${username}/make/share?avc=${avatarCode}`,
});
});
app.get("/:id/make/share", (req, res) => {
const { id } = req.params;
const { avc } = req.query;
// res.send(req.path);
res.render("share", { id, avc });
});
app.get("/:id/validate", async (req, res) => {
const { id } = req.params;
const temp = await User.findOne({ username: id });
if (temp == null) res.send({ userExists: false });
else
res.send({
userExists: true,
redirect: `/${id}/play`,
avatarCode: temp.avatarCode,
});
});
app.get("/:id/play", async (req, res) => {
const { id } = req.params;
const temp = await User.findOne({ username: id });
if (temp == null) res.render("expired", { id });
else res.render("play", { id });
});
app.post("/:id/play", async (req, res) => {
const { id } = req.params;
console.log("found the user from the DB");
const usr = await User.findOne({ username: id });
const { clientName, avatarCode } = req.body;
res.render("playscr", { quesArr: usr.cookedQs, clientName, id, avatarCode });
});
app.post("/:id/playscr", async (req, res) => {
const { id } = req.params;
const body = req.body;
const { clientName, score, avatarCode } = body;
console.log("received answersheet");
const filter = { clientName: body.clientName };
const update = body;
let doc = await Client.findOneAndUpdate(filter, update, {
new: true,
upsert: true,
})
.then(() => console.log("successfully saved"))
.catch((e) => console.log(e));
res.send({
message: "we received your response",
redirect: `/${id}/score?clientName=${clientName}&scr=${score}&avc=${avatarCode}`,
});
});
app.get("/:id/score", (req, res) => {
const { id } = req.params;
const { clientName } = req.query;
const { scr: score } = req.query;
const { avc: avatarCode } = req.query;
res.render("scoreDisp", {
userName: id,
clientName,
score: score,
avatarCode,
});
});
app.get("/:id/response/:clientName", async (req, res) => {
const { id } = req.params;
const { clientName } = req.params;
const client = await Client.findOne({ clientName }, (err, obj) => {
if (err) throw err;
})
.clone()
.catch((err) => console.log(err));
const user = await User.findOne({ username: id }, (err, obj) => {
if (err) throw err;
})
.clone()
.catch((err) => console.log(err));
res.render("responseDetails", {
userName: id,
clientName,
quesSheet_objs_arr: user.cookedQs,
answerSheet: client.answerSheet,
score: client.score,
gender: user.gender,
clientAvatarCode: client.avatarCode,
userAvatarCode: user.avatarCode,
});
});
app.get("/:username/response/", async (req, res) => {
const { username } = req.params;
const user = await User.findOne({ username }, (err, obj) => {
if (err) throw err;
})
.clone()
.catch((err) => console.log(err));
const clients = await Client.find({ username }, (err, obj) => {
if (err) throw err;
})
.clone()
.catch((err) => console.log(err));
console.log([...clients]);
res.render("response", {
clients: [...clients],
clientCount: clients.length,
username,
userAvatarCode: user.avatarCode,
});
});
app.get("/devInfo", (req, res) => {
res.render("devInfo");
});
app.get("/instructions", (req, res) => {
res.render("instructions");
});
app.get("/t&c", (req, res) => {
res.render("t&c");
});
app.get("/404", (req, res) => {
res.render("404");
});
app.listen(port, () => {
console.log(`listening on port ${port}`);
});