-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
321 lines (284 loc) · 8.39 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
let path = require('path')
var express = require('express')
var app = express()
app.use(express.static(path.join(__dirname, 'public')))
var bodyparsser = require('body-parser')
var parser = bodyparsser.urlencoded({ extended: true })
app.set('view engine', 'ejs')
app.set('views', 'views')
const sessions = require('express-session');
const store = new sessions.MemoryStore();
const oneDay = 1000 * 60 * 60 * 24;
app.use(sessions({
secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
saveUninitialized: true,
cookie: { maxAge: oneDay },
resave: true,
store
}));
/////////////////////////////////////////////////////////////////
const multer = require('multer')
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, "excelFiles"))
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname)
}
})
const excelFilter = (req, file, cb) => {
if (file.mimetype.includes("excel") || file.mimetype.includes("spreadsheetml")) {
cb(null, true);
}
else {
cb("Please upload only excel file.", false);
}
};
let uploadFile = multer({ storage: storage, fileFilter: excelFilter });
//////////////////////////////////////////////////////////////////////////////////////////////
var db = require('./model/db')
//================================================================
const readXlsxFile = require('xlsx');
const { table } = require('console')
//================================================================
app.get('/', (req, res) => {
var s = req.session
if (s.username) {
res.redirect("/homepage")
}
else {
res.redirect('/login')
}
})
app.get('/login', (req, res) => {
res.render("login")
})
app.post("/login", parser, function (req, res) {
var user = req.body.username
var pass = req.body.password
var q = 'select * from account where username =? and password=?'
db.query(q, [user, pass], (err, result,) => {
if (err) {
res.send(err.message)
}
else {
if (result.length == 0) {
res.redirect('/login')
}
else {
req.session.username = user
req.session.name = result[0]['name']
res.redirect('/homepage')
}
}
})
})
app.get('/homepage', function (req, res) {
if (req.session.username) {
user = req.session.username
q = 'select * from filepath where username=(?)';
db.query(q, [user], function (err, resultes) {
if (err) {
console.log('error in setting methode in select our file');
res.send(err.message);
}
else {
let restlist = []
let preReqlist = [];
resultes.forEach(row => {
if (row.type === 'P') {
preReqlist.push(row);
}
else {
restlist.push(row);
}
});
res.render('homepage',
{
restFiles: restlist,
preReqFiles: preReqlist,
name: req.session.name
});
}
})
}
else {
res.redirect('/login')
}
})
// طلب لحذف الملفات بالجداول
app.get('/deletefiles', (req, res) => {
q = 'DELETE FROM filepath WHERE (id = ?);'
id = req.query.id
db.query(q, [id], (err, resultes) => {
if (err) {
res.send(err.message)
}
else {
res.redirect('/homepage')
}
})
})
// طلب لاضافة الملف الخاص بالمواد المتبقية للطلاب
app.post('/ChoosefileRest', uploadFile.single('rest_stu'), (req, res) => {
q = 'INSERT INTO filepath(username,type,path)VALUES(?,?,?);'
db.query(q, [req.session.username, "R", req.file.filename], (err, result) => {
if (err) {
console.log(err)
res.send(err.message)
}
else {
res.redirect('/homepage')
}
})
})
// طلب خاص لاضافة الملف الخاص بالمتطلبات السابقة للمواد
app.post('/ChoosefilePre', uploadFile.single('pre_req'), (req, res) => {
q = 'INSERT INTO filepath(username,type,path)VALUES(?,?,?);'
db.query(q, [req.session.username, "P", req.file.filename], (err, result) => {
if (err) {
console.log(err)
res.send(err.message)
}
else {
res.redirect('/homepage')
}
})
})
// طلب لاختيار الملفين وهما المملف الخاص بالمتطلبات السابقة
// والملف الخاص بالمواد المتبقية
// وذلك حتى يختارهم مدير القسم قبل ان يتم عرض النتيجة
// my algorithim
app.post('/send', parser, (req, res) => {
var PreReqFile = req.body.PreReqFile
var RestStuFile = req.body.RestStuFile
var cumulativehours=req.body.cumulativehours
// read pre requisst file
let workbook = readXlsxFile.readFile(path.join(__dirname, "excelFiles", PreReqFile), { encoding: 'utf8' });
// get name the first sheet
let sheetName = workbook.SheetNames[0];
// get the first sheet
let worksheet = workbook.Sheets[sheetName];
// read first sheet as json
let data = readXlsxFile.utils.sheet_to_json(worksheet, { header: 1 });
// order the column in preRequset file :
/// index 0 : رمز المادة
// index 1: اسم المادة
// index 2 : اسم المادة السابقة
// index 3 : رمز االمادة السابقة
// builde map to store corse_code as key and all pre_request for this course in the set
// ITEMATH : ( ITEDFGH,ITEFGH,ITESSS)
var preReq = {}
data.forEach((row) => {
if (preReq[row[0]]) {
preReq[row[0]].add(row[3])
}
else {
preReq[row[0]] = new Set([row[3]])
}
})
workbook = readXlsxFile.readFile(path.join(__dirname, "excelFiles", RestStuFile), { encoding: 'utf8' });
sheetName = workbook.SheetNames[0];
worksheet = workbook.Sheets[sheetName];
data = readXlsxFile.utils.sheet_to_json(worksheet, { header: 1 });
// index 0 : course name
// index 1 : student id
// index 2 : course code
// index 3 : student name
// index 4 : year level for student
// index 5 :
// index 6 : number hours registered this semestrial
// index 7 : cumulative hours
// index 8 : gpa for student
// index 9 : department
// student_id :( ITEDFGH,ITEFGH,ITESSS) المقررات الباقية لهاد لطالب
let student = {}
data.forEach((row) => {
if (student[row[1]]) {
student[row[1]].add(row[2])
} else {
student[row[1]] = new Set([row[2]])
}
})
course={}
data.forEach((row) => {
// first we will test if student take all preRequest course ( intersection )
intersection = new Set()
if (preReq[row[2]]) // row[2]=coursecode preReq[row[2]]= preReq courses for coursecode
{
intersection = new Set(Array.from(preReq[row[2]]).filter(i => student[row[1]].has(i)))
}
if (intersection.size == 0) // if true the student can register this course
{
if(!course[row[2]]) // if course not found in map
{
course[row[2]]= {'g':new Set(),'n':new Set(),'courseName':row[0]}
}
if ( parseInt(row[7])>= parseInt(cumulativehours)) { // if he is gradu student
course[row[2]]['g'].add(row[3]+'-'+row[1])
}
else{
course[row[2]]['n'].add(row[3]+'-'+row[1])
}
}
})
sortArray=Array.from( Object.entries(course))
sortArray.sort((a,b)=>{
if(a[1]['g'].size<b[1]['g'].size){
return 1;
}
else if(a[1]['g'].size==b[1]['g'].size){
if(a[1]['n'].size<b[1]['n'].size){
return 1;
}
else{
return -1;
}
}
else{
return -1;
}
})
sortMap=Object.fromEntries(sortArray)
res.render('table',
{
course:sortMap
})
})
app.get('/table', (req, res) => {
if (!req.session.username) {
res.redirect('/login')
}
else {
let q = 'select c.id,c.name from curriculum c join department_manager d on c.dep_manager_id= d.id join account a on d.account_id=a.id where a.id=?'
db.query(q, [req.session.a_id], (err, result) => {
if (!err) {
db.query('select * from level;', (err, result1) => {
if (!err) {
res.render("table", { c: result, l: result1 })
}
else {
res.send(err)
}
})
} else {
res.send(err)
}
})
}
})
app.get('/students', (req, res) => {
if (!req.session.username) {
res.redirect('/login')
}
else {
res.render('students')
}
})
app.get("/logout",(req,res)=>{
req.session.destroy()
res.redirect("/login")
})
app.listen(5000, () => {
console.log('listening on http://localhost:5000')
})