-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
333 lines (300 loc) · 13.8 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
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
326
327
328
329
330
331
332
333
let a = require('mysql')
let exp = require("express")
const bodyParser = require('body-parser');
// const uploadProgress = require('express-upload-progress');
const fs = require('fs');
const { error } = require('console');
let email = undefined;
let first_name = undefined
let last_name = undefined
let password = undefined
let id = undefined ;
let codeverif;
query_result = undefined
let dark_mode = false;
const defaultimage = fs.readFileSync('public/images/defualt.png');
db = a.createConnection({host :"sql11.freesqldatabase.com" , user: "sql11692837" , password:"ESZ2YTvzKy", port:"3306"})
db.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
db.query("USE sql11692837" , function(err){
if (err) throw err;
})
const app = exp();
app.use(exp.json({ limit: "200mb" }));
app.use(exp.urlencoded({ extended: true, limit: "200mb" }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(exp.static(__dirname + '/public'));
app.use(exp.static('public'));
app.set("view engine","ejs");
// parse application/json
app.use(bodyParser.json());
app.get('/' ,(req,res) => {
email = undefined;
first_name = undefined
last_name = undefined
password = undefined
id = undefined ;
query_result = undefined
dark_mode = false;
db.query("SELECT count(DISTINCT user_id) as nbr_users FROM Users",function(err,result){
db.query("SELECT count(DISTINCT user_id) as nbr_profs FROM ProfessorEnrollments",function(err,result2){
db.query("SELECT count(DISTINCT course_id) as nbr_courses FROM Courses ",function(err,result3){
res.render("main",{checksignin:false,students:result[0].nbr_users,profs:result2[0].nbr_profs,courses:result3[0].nbr_courses})
})
})
})
})
app.get('/login' ,(req,res) => {
res.render("index",{checknotlogin:false});
})
app.get('/home' ,(req,res) => {
res.sendFile("./Project/homeaftersignin.html",{root : __dirname})
})
app.post('/home/me' ,(req,res) => {
if (req.body.submit === "verify"){
if (req.body.codeinput == codeverif){
db.query(`insert into Users (email,password,first_name,last_name,photo) values (?, ?, ?, ?,BINARY(?))`, [email,password,first_name,last_name,defaultimage],
function(err){
db.query("select user_id from Users where email = ?;",[email],function(err,result){
console.log(result)
id = result[0].user_id
console.log(id)
db.query("SELECT count(DISTINCT user_id) as nbr_users FROM Users",function(err,result){
db.query("SELECT count(DISTINCT user_id) as nbr_profs FROM ProfessorEnrollments",function(err,result2){
db.query("SELECT count(DISTINCT course_id) as nbr_courses FROM Courses ",function(err,result3){
res.render("homeaftersignin",{checksignin:false,students:result[0].nbr_users,profs:result2[0].nbr_profs,courses:result3[0].nbr_courses})
})
})
})
})
})
}
else{
codeverif = Math.floor(Math.random()*1000000)
console.log(codeverif);
res.render('verificationemail',{firstname:first_name,lastname:last_name,email:email,checkcode:true,code:codeverif} )
}
}
else if (req.body.submit === "Log in"){
email = req.body.email;
db.query(`select * from Users where email = ? and password = ?`,[req.body.email,req.body.password],function(err,result){
console.log(result);
if (err) throw err;
query_result = result
if(query_result.length === 0){
res.render("index",{checknotlogin:true})
}
else{
first_name = result[0].first_name;
last_name = result[0].last_name;
db.query("select user_id from Users where email = ?;",[email],function(err,result){
id = result[0].user_id
}
)
db.query("SELECT count(DISTINCT user_id) as nbr_users FROM Users",function(err,result){
db.query("SELECT count(DISTINCT user_id) as nbr_profs FROM ProfessorEnrollments",function(err,result2){
db.query("SELECT count(DISTINCT course_id) as nbr_courses FROM Courses ",function(err,result3){
res.render("homeaftersignin",{checksignin:false,students:result[0].nbr_users,profs:result2[0].nbr_profs,courses:result3[0].nbr_courses})
})
})
})
}
})
}
})
app.get('/inscription', (req,res) => {
if (email === undefined){
db.query("SELECT count(DISTINCT user_id) as nbr_users FROM Users",function(err,result){
db.query("SELECT count(DISTINCT user_id) as nbr_profs FROM ProfessorEnrollments",function(err,result2){
db.query("SELECT count(DISTINCT course_id) as nbr_courses FROM Courses ",function(err,result3){
res.render("main",{checksignin:true,students:result[0].nbr_users,profs:result2[0].nbr_profs,courses:result3[0].nbr_courses})
})
})
})
}
else {
res.sendFile("./Project/inscription1.html",{root : __dirname})
}
})
app.get('/inscription/etudiant' ,(req,res) => {
res.sendFile("./Project/etudianrinscri.html",{root : __dirname})
})
app.get('/inscription/prof' ,(req,res) => {
res.sendFile("./Project/profinscription.html",{root : __dirname})
})
app.get('/createaccount' ,(req,res) => {
res.render("createaccount",{checkcreataccount:false})
})
app.get('/profile' ,(req,res) => {
db.query("select status from Users where user_id = ?",[id],function(err,result){
console.log(result)
if (result[0].status == "student"){
db.query(`select * from Users,StudentEnrollments,Groupes,Courses,Services where Users.user_id = StudentEnrollments.user_id and
Users.user_id = ? and StudentEnrollments.group_id = Groupes.group_id and Groupes.course_id = Courses.course_id and
Courses.service_id = Services.service_id `,[id], function(err, result) {
if (err) throw err;
res.render("profile",{res:result});
}
)
}
else if (result[0].status == "professor"){
db.query(`select * from Users,ProfessorEnrollments,Groupes,Courses,Services where Users.user_id = ProfessorEnrollments.user_id and
Users.user_id = ? and ProfessorEnrollments.group_id = Groupes.group_id and Groupes.course_id = Courses.course_id and
Courses.service_id = Services.service_id `,[id], function(err, result) {
if (err) throw err;
res.render("profile",{res:result});
}
)
}
else{
db.query("select * from Users where user_id = ?",[id],function(err,result){
res.render("profile",{res:result});
})
}
})
})
app.get('/services' ,(req,res) => {
res.sendFile("./Project/services.html",{root : __dirname})
})
app.get('/courses' ,(req,res) => {
res.sendFile("./Project/courses.html",{root : __dirname})
})
app.post('/getelemnts',(req,res) =>{
var id_service = req.body.id_service;
console.log(id_service)
db.query("select course_id,label from Courses where service_id = ?",[id_service],function(err,result){
console.log(result)
res.json({ result });
})
})
app.post("/endinscription",(req,res) =>{
if(req.body.submit === "save inscription student"){
var service = req.body.service ;
var course = req.body.course;
var nbrmois = req.body.nbrmois;
console.log("hmm")
db.query("select * from Groupes where course_id = ? and num_students < 20;",[course],function(err,result){
let grps_result = result
if (grps_result.length >0){
db.query("Update Groupes set num_students = ? where group_id = ?",[result[0].num_students+1,result[0].group_id],function(err){
if (err) {throw err};
db.query("insert into StudentEnrollments (num_months,user_id,group_id) values (?,?,?)",[nbrmois,id,grps_result[0].group_id])
db.query("Update Users set status = ? where user_id = ?",["student",id])
res.sendFile("./Project/endinscription.html",{root : __dirname})
})
}
else {
db.query("insert into Groupes (num_students,course_id) values (?,?)",[1,course],function(err,result){
db.query("select * from Groupes where course_id = ? and num_students < 20;",[course],function(err,result){
let grps_result = result;
db.query("insert into StudentEnrollments (num_months,user_id,group_id) values (?,?,?)",[nbrmois,id,grps_result[0].group_id])
db.query("Update Users set status = ? where user_id = ?",["student",id])
db.query("select enroliment_id,waitinglist_id from ProfessorWaitingLists where course_id = ? orderby wait_date ",[course]
,function(err,result){
if (err) {throw err};
if (result.length >0){
db.query("UPDATE ProfessorEnrollments set group_id = ? where enroliment_id = ?",[grps_result[0].group_id,result[0].enroliment_id])
db.query("DELETE FROM ProfessorWaitingLists WHERE enroliment_id = ?",[result[0].waitinglist_id])
}
})
res.sendFile("./Project/endinscription.html",{root : __dirname})
})
})
}
})
}
if(req.body.submit === "save inscription prof"){
var service = req.body.service ;
var course = req.body.course;
var nbrmois = req.body.nbrmois;
const datetime = new Date();
time_enroll = datetime.toISOString().slice(0, 10)
db.query("Update Users set status = ? where user_id = ?",["professor",id])
db.query("select group_id from Groupes where course_id = ? AND group_id NOT in (SELECT DISTINCT group_id FROM ProfessorEnrollments);",[course],function(err,result){
if(result.length === 0){
db.query("INSERT INTO ProfessorEnrollments(num_months,enrollment_date,user_id) VALUES (?,?,?) ",[nbrmois,time_enroll,id])
db.query("SELECT max(enrollment_id) as max_enrollment_id FROM ProfessorEnrollments" ,function(err,result){
db.query("INSERT INTO ProfessorWaitingLists(enrollment_id,wait_date) VALUES(?,?)",[result[0].max_enrollment_id,time_enroll])
res.sendFile("./Project/endinscription.html",{root : __dirname})
}
)
}
else{
let groupe_result = result[0].group_id;
db.query("INSERT INTO ProfessorEnrollments(num_months,enrollment_date,user_id,group_id) VALUES (?,?,?,?) ",[nbrmois,time_enroll,id,groupe_result])
res.sendFile("./Project/endinscription.html",{root : __dirname})
}
}
)
}
}
)
app.post('/photourl',(req,res) =>{
const dataUrl = req.body.photo;
console.log(dataUrl)
const base64Data = dataUrl.replace(/^data:image\/\w+;base64,/, '');
const imageBuffer = Buffer.from(base64Data, 'base64');
db.query("Update Users set photo = ? where user_id = ?",[imageBuffer,id],function(err,result){
console.log(result)
})
})
app.post("/verificationemail",(req,res) =>{
db.query(`select * from Users where email = ?;`,[req.body.email], function(err, result) {
if (err) throw err;
query_result = result
if (query_result.length === 0){
email = req.body.email;
password = req.body.password
first_name = req.body.first_name
last_name = req.body.last_name
codeverif = Math.floor(Math.random()*1000000)
console.log(codeverif);
res.render('verificationemail',{firstname:req.body.first_name,lastname:req.body.last_name,email:req.body.email,checkcode:false,code:codeverif} )
}
else {
res.render("createaccount",{checkcreataccount:true})
}
})
})
app.post("/chnangepassword", (req,res) =>{
db.query("select password from Users where user_id = ?",[id],function(err,result){
if (err) throw err;
if (req.body.currentpassword == result[0].password && req.body.newpassword == req.body.confirmpassword){
db.query("Update Users set password = ? where user_id = ?",[req.body.newpassword,id],function(err,result){
db.query("select photo from Users where user_id = ?",[id],function(err,result){
console.log(result)
res.render("changepasswordwithsuccesss",{photo:result[0].photo})
})
})
}
else{
db.query("select photo from Users where user_id = ?",[id],function(err,result){
console.log(result);
res.render("changepassword",{photo:result[0].photo,passerror:true})
})
}
})
})
app.get("/verificationcodeforchangeemail",(req,res)=>{
codeverif = Math.floor(Math.random()*1000000)
console.log(codeverif)
res.render("verificationforchangepassword",{firstname:first_name,email:email,lastname:last_name,code:codeverif,checkcode:false})
})
app.post("/changewithsucces",(req,res)=>{
if(codeverif == req.body.codeinput){
db.query("select photo from Users where user_id = ?",[id],function(err,result){
console.log(result);
res.render("changepassword",{photo:result[0].photo,passerror:false})
})
}
else{
codeverif = Math.floor(Math.random()*1000000)
console.log(codeverif);
res.render('verificationforchangepassword',{firstname:first_name,lastname:last_name,email:email,checkcode:true,code:codeverif} )
}
});
// app.use(uploadProgress)
const port = process.env.PORT || 5551;
app.listen(port);