-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (163 loc) · 4.13 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
//PROJECT NODEJS (Mohamed Jassem Debbich && Mouadh Abdellatif -----GLSI-A)
const express = require('express')
const app = express()
const port = 3000
app.listen(port, () => {
console.log(`Le serveur est en écoute sur le port ${port}`)
})
var bodyParser = require('body-parser')
app.use(bodyParser.json())
var fs = require('fs')
const { get } = require('http')
const req = require('express/lib/request')
const res = require('express/lib/response')
const file = __dirname+"/students.json"
// we tried to insert joi to validate the entry data but it's not working :')
//const studentsSchema = Joi.object({
//nom : Joi.string().required(),
//classe : Joi.string().min(3).max(10).required(),
//module : Joi.string().min(3).max(10).required(),
//note : Joi.string().min(0).max(20).required(),
//})
//const { error, value} = studentsSchema.validate(student, {
//abortEarly : false,
//});
//if (error){
// console.log(error);
//return res.send(error.details)
//}
//api crud
// post an student
app.post('/add', (req,res)=> {
var students = getStudentsData()
var student = req.body
calculmoy(student,(moyen)=>{
console.log(student)
console.log(moyen)
const Id = Math.floor(100000 + Math.random() * 900000)
student.moyenne = moyen
students[Id] = student
saveStudentData(students)
res.send("student saved")
})
})
//read data from json
const getStudentsData = () => {
const jsonData = fs.readFileSync(file)
return JSON.parse(jsonData)
}
app.get('/', (req, res) => {
var students = getStudentsData()
console.log(typeof(students))
console.log(students)
res.send(students)
})
//delete student
const deleteStudentById = (Id,cb) => {
let delet = false
students = getStudentsData()
console.log(students)
for (let idd in students) {
if (idd == Id ) {
delete students[idd]
saveStudentData(students)
delet = true
}
}
cb(delet)
}
app.delete('/delete/:Id', (req, res) => {
const Id = req.params.Id
console.log(Id)
deleteStudentById(Id,(data)=>{
if (data == true) {
res.send("student name =" + Id+"delete succesfully")
}else {
res.send("student name =" + Id+"delete failed")
}
})
})
// save after changes
const saveStudentData = (data) => {
const stringifyData = JSON.stringify(data)
fs.writeFileSync(file, stringifyData)
}
//update students
app.put('/update/:Id', (req, res) => {
var students = getStudentsData()
var Id = req.params.Id
console.log(students[Id])
var student = req.body
if (students[Id] == undefined) {
res.send("failed")
}else {
delete students[Id]
calculmoy(student, (moyen)=>{
student.moyenne = moyen
students[Id] = student
saveStudentData(students)
res.send("success")
})
}
})
//fonction pour calculer la moyenne automatique
const calculmoy = (student,cb) => {
var modules = student.modules
let i = 0
let somme = 0
modules.forEach( module =>{
console.log(module)
somme += module.note
i++;
})
let moyen = somme / i;
cb(moyen)
}
//output the best and the worst note for each student
let affichebyStudent=(students,cb)=>{
var listt=[]
for (let i in students){
var objet ={}
student = students[i]
objet.nom = student.nom
var best = 0 ;
var worst = 20;
student.modules.forEach((module)=>{
if (module.note > best ){
best = module.note
objet.best_module = module
}
if (module.note < worst){
worst = module.note
objet.worst_module=module
}
})
listt.push(objet)
}
cb(listt)
}
//output name and moyenne of students
let affiche_name_moyen = (students,cb)=> {
var name_moyen = {}
var rep = []
for (let j in students){
var student = students[j]
name_moyen.nom=student["nom"];
name_moyen.moyenne=student["moyenne"];
rep.push(name_moyen)
cb(rep)
}
}
app.get('/afficher', (req, res)=>{
const students = getStudentsData()
affiche_name_moyen(students,(rep) =>{
res.send(rep);
})
}
)
app.get('/affiche-bystudent',(req,res) => {
const students = getStudentsData()
affichebyStudent(students,(listt)=>{
res.send(listt)
})
})