-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·322 lines (293 loc) · 8.95 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
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
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = express();
var mongoose = require('mongoose');
var db = mongoose.connection;
var users = require('./models/user');
var courses = require('./models/course');
app.set('view engine', 'pug')
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
db.on('error', console.error);
db.once('open', function() {
console.log("Connected to database");
});
mongoose.connect('mongodb://localhost/project', {useNewUrlParser: true});
app.get('/', function(req, res) {
var id = req.cookies.student_id;
res.render('human-index', {student_id: id});
});
app.get('/natural', function(req, res) {
var id = req.cookies.student_id;
res.render('natural-index', {student_id: id});
});
app.get('/human', function(req, res) {
var id = req.cookies.student_id;
res.render('human-index', {student_id: id});
});
app.post('/register', function(req, res) {
console.log("Register request: { student_id: " + req.body.student_id + ", password:", req.body.password + ", ... }");
users.findOne({student_id: req.body.student_id}, function(err, result) {
if(err != null) {
res.send({result: "failed"});
console.error("Database read failed");
return;
}
if(result == null) {
var student = new users({
student_id: req.body.student_id,
password: req.body.password,
name: req.body.name,
birth: req.body.birth,
department: req.body.department,
credit: 18,
bag: [],
register: []
});
student.save(function(err) {
if(err) {
res.send({result: "failed"});
console.error("Database write failed");
} else {
res.send({result: "success"});
console.log("Registeration success");
}
});
} else {
res.send({result: "duplicate"});
console.log("Registeration failed (Duplicated ID)");
}
});
});
app.post('/login', function(req, res) {
console.log("Login request: { student_id: " + req.body.student_id + ", password:", req.body.password + " }");
users.findOne({$and:[{student_id: req.body.student_id}, {password: req.body.password}]}, function(err, result) {
if(err != null) {
console.error("Database access failed");
return;
}
if(result != null) {
res.cookie('student_id', result.student_id);
res.send({result: "success", student_id: result.student_id});
console.log("Login success");
} else {
res.send({result: "failed"});
console.log("Login failed");
}
});
});
app.get('/logout', function(req, res) {
var id = req.cookies.student_id;
console.log("Logout request: { student_id: " + id + " }");
res.cookie('student_id', '');
res.send();
console.log("Logout success");
});
app.get('/gls', function(req, res) {
var id = req.cookies.student_id;
users.findOne({student_id: id}, function(err, student) {
if(err != null) {
res.send("Access denied");
console.error("Database read failed");
return;
}
courses.find(function(err, course) {
if(err != null || student == null) {
res.send("Access denied");
console.error("Database read failed");
return;
}
if(student.student_id == "admin" && student.password == "1234") {
res.render('gls_main', {type: "admin", course: course});
console.log("GLS request: { student_id: " + id + ", type: admin }");
} else {
student.birth_string = student.birth.getFullYear()+'-'+(student.birth.getMonth()+1)+'-'+student.birth.getDate();
res.render('gls_main', {type: "student", student: student, course: course});
console.log("GLS request: { student_id: " + id + ", type: student }");
}
});
});
});
app.post('/addcourse', function(req, res) {
console.log("Add course request: { class_id: " + req.body.class_id + ", class_name:", req.body.class_name + ", ... }");
courses.findOne({class_id: req.body.class_id}, function(err, result) {
if(err != null) {
res.send({result: "failed"});
console.error("Database read failed");
return;
}
if(result == null) {
var course = new courses({
class_id: req.body.class_id,
class_name: req.body.class_name,
professor: req.body.professor,
max_students: req.body.max_students,
credit: req.body.credit,
registered: 0
});
course.save(function(err) {
if(err) {
res.send({result: "failed"});
console.error("Database write failed");
} else {
res.send({result: "success"});
console.log("Add course success");
}
});
} else {
res.send({result: "duplicate"});
console.log("Add course failed (Duplicated ID)");
}
});
});
app.post('/coursebag/putinbag', function(req, res) {
console.log("Put in bag request: { student_id: " + req.body.student_id + ", class_id: " + req.body.class_id + " }");
users.findOne({student_id: req.body.student_id}, function(err, student) {
if(err != null) {
res.send({result: "failed"});
console.error("Database read failed");
return;
}
var exist = false;
for(var i = 0; i < student.bag.length; i++) {
if(student.bag[i] == req.body.class_id) {
exist = true;
break;
}
}
if(exist) {
res.send({result: "failed"});
} else {
student.bag.push(req.body.class_id);
student.save();
res.send({result: "success"});
}
});
});
app.post('/coursebag/putoutbag', function(req, res) {
console.log("Put out bag request: { student_id: " + req.body.student_id + ", class_id: " + req.body.class_id + "}");
users.findOne({student_id: req.body.student_id}, function(err, student) {
if(err != null) {
res.send({result: "failed"});
console.error("Database read failed");
return;
}
var exist = false;
for(var i = 0; i < student.bag.length; i++) {
if(student.bag[i] == req.body.class_id) {
exist = true;
break;
}
}
if(exist) {
student.bag.pull(req.body.class_id);
student.save();
res.send({result: "success"});
} else {
res.send({result: "failed"});
}
});
});
app.get('/sugang', function(req, res) {
var id = req.cookies.student_id;
users.findOne({student_id: id}, function(err, student) {
if(err != null || student == null) {
res.send("Access denied");
console.log("Sugang access denied");
return;
}
if(student.student_id == "admin" && student.password == "1234") {
console.log("Sugang request redirected: { student_id: " + id + ", type: admin }");
res.redirect('/gls');
} else {
courses.find(function(err, course) {
if(err != null)
console.error("Database read failed");
res.render('sugang', {student: student, course: course});
console.log("Sugang request: { student_id: " + id + " }");
});
}
});
});
app.post('/sugang/cancel', function(req, res) {
console.log("Sugang cancel request: {student_id: " + req.body.student_id + ", class_id: " + req.body.class_id + " }");
users.findOne({student_id: req.body.student_id}, function(err, student) {
if(err != null) {
res.send({result: "failed"});
console.log("Sugang cancel failed");
}
var exist = false;
for(var i = 0; i < student.register.length; i++) {
if(student.register[i] == req.body.class_id) {
exist = true;
break;
}
}
if(exist) {
courses.findOne({class_id: req.body.class_id}, function(err, course) {
if(err != null || course == null) {
res.send({result: "failed"});
console.log("Sugang cancel failed");
}
student.register.pull(req.body.class_id);
student.credit += course.credit;
student.save();
course.registered--;
course.save();
res.send({result: "success"});
console.log("Sugang cancel success");
});
} else {
res.send({result: "failed"});
console.log("Sugang cancel failed");
}
});
});
app.post('/sugang/register', function(req, res) {
console.log("Sugang register request: {student_id: " + req.body.student_id + ", class_id: " + req.body.class_id + " }");
users.findOne({student_id: req.body.student_id}, function(err, student) {
if(err != null) {
res.send({result: "failed"});
console.log("Sugang register failed");
}
var exist = false;
for(var i = 0; i < student.register.length; i++) {
if(student.register[i] == req.body.class_id) {
exist = true;
break;
}
}
if(!exist) {
courses.findOne({class_id: req.body.class_id}, function(err, course) {
if(err != null || course == null) {
res.send({result: "failed"});
console.log("Sugang register failed");
}
if(course.max_students == course.registered || student.credit - course.credit < 0) {
if(student.credit - course.credit < 0)
res.send({result: "credit_limit"});
else
res.send({result: "seat_limit"});
console.log("Sugang register failed");
} else {
student.register.push(req.body.class_id);
student.credit -= course.credit;
student.save();
course.registered++;
course.save();
res.send({result: "success"});
console.log("Sugang register success");
}
});
} else {
res.send({result: "duplicate"});
console.log("Sugang register failed");
}
});
});
app.listen(8000, function() {
console.log("Server ready (port: 8000)");
});