-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
535 lines (468 loc) · 18.4 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//import { Subject, Category, Resource, UserAdmin } from './model';
const { Subject, Category, Resource, UserAdmin } = require('./models');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
require('dotenv').config()
const app = express();
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors());
// connection to the database
//mongoose.connect("mongodb://localhost:27017/yonebi")
try {
mongoose.connect("mongodb+srv://mayel15:[email protected]/?retryWrites=true&w=majority")
} catch (err) {
console.log("connection to the database failed")
}
// listening server
app.listen(process.env.PORT, () => {
console.log(`Server Started at http://localhost:${process.env.PORT}`)
})
/**
* POST
* */
// add a new resource : OK
app.post('/api/resources/add', (req, res) => {
var newResource, newSubject, newCategory;
Subject.findOne({ name: req.body.subject }).then((subject) => {
if (!subject) {
newSubject = new Subject({ name: req.body.subject });
newSubject.save().then(() => {
console.log({ message: "new subject added successfully", })
})
} else {
newSubject = subject;
console.log(newSubject)
}
Category.findOne({ name: req.body.category }).then((category) => {
if (!category) {
newCategory = new Category({ name: req.body.category, subject: newSubject.name });
newCategory.save().then(() => {
console.log({ message: "new category added successfully" })
})
} else {
newCategory = category;
console.log(newCategory)
}
Resource.findOne({ title: req.body.title })
.then((resource) => {
if (process.env.PASSWORDSECURITY === req.body.passwordSecurity) {
if(!resource){
newResource = new Resource({
title: req.body.title,
description: req.body.description,
url: req.body.url,
authors: req.body.authors,
subject: newSubject.name,
category: newCategory.name
})
newResource.save().then(() => {
return res.send(newResource)
})
}
else {
return res.send({ message: "error :( a resource with the same name exists " })
}
}else{
return res.send({ message: "invalid password security" })
}
}).catch(() => res.status(404).send({ message: 'error' }))
})
})
})
// add a user admin with a sign up : OK
app.post('/api/useradmin/signup', (req, res) => {
UserAdmin.findOne({username: req.body.username, password: req.body.password}).then((userAdmin) => {
if (!userAdmin && process.env.PASSWORDSECURITY === req.body.passwordSecurity){
const newUser = new UserAdmin(req.body)
newUser.save()
return res.status(200).send(newUser)
}
else{
return res.status(200).send({message: "error :( the user already exists"})
}
})
})
// login with an account : OK
app.post('/api/useradmin/login', (req, res) => {
UserAdmin.findOne({username: req.body.username, password: req.body.password}).then((userAdmin) => {
return (!userAdmin)
? res.status(404).send({ message: "bad credentials" })
: res.status(200).send({message: 'is connected'})
})
})
/**
* GET
* */
// get the homepage of the server : OK
app.get('/', (req, res) => {
res.status(200).send({ message: "Bienvenue dans le serveur de Yonebi" })
})
// get all the resource : OK
app.get('/api/resources', (req, res) => {
Resource.find().then((resource) => {
return (!resource)
? res.status(404).send({ message: "resource not found" })
: res.status(200).send(resource)
})
})
// get a particular resource : OK
app.get('/api/resources/:id', (req, res) => {
Resource.findById(req.params.id).then((resource) => {
return (!resource)
? res.status(404).send({ message: "resource not found" })
: res.status(200).send(resource)
})
})
// get the resource knowing the subject and category as parameters : OK
app.get('/api/:subject/:category/resources', (req, res) => {
Subject.findOne({ name: req.params.subject })
.then((subject) => {
if (!subject) {
return res.status(404).send('Subject not found');
}
Category.findOne({ name: req.params.category })
.then((category) => {
if (!category) {
return res.status(404).send('Category not found');
}
Resource.find({ subject: subject.name, category: category.name })
.then((resources) => {
return res.send(resources);
})
.catch((error) => {
console.log(error);
return res.status(500).send('Internal server error');
});
})
.catch((error) => {
console.log(error);
return res.status(500).send('Internal server error');
});
})
.catch((error) => {
console.log(error);
return res.status(500).send('Internal server error');
});
});
// get a particular resource knowing the subject, category and its id as parameters : OK
app.get('/api/:subject/:category/resources/:id', (req, res) => {
Subject.findOne({ name: req.params.subject })
.then((subject) => {
if (!subject) {
return res.status(404).send('Subject not found');
}
Category.findOne({ name: req.params.category })
.then((category) => {
if (!category) {
return res.status(404).send('Category not found');
}
Resource.findById(req.params.id)
.then((resource) => {
return res.send(resource);
})
.catch((error) => {
console.log(error);
return res.status(500).send('Internal server error');
});
})
.catch((error) => {
console.log(error);
return res.status(500).send('Internal server error');
});
})
.catch((error) => {
console.log(error);
return res.status(500).send('Internal server error');
});
})
// get the resources knowing a category as parameter
app.get('/api/:category/resources', (req, res) => {
Category.find({name: req.params.category}).then((category_) => {
if (!category_) {
return res.status(404).send({ message: "category not found" })
}
Resource.find({ category: category_.name }).then((resource) => {
return (!resource)
? res.status(400).send({ message: "resource not found" })
: res.status(200).send(resource)
})
})
})
// get all the categories : OK
app.get('/api/categories', (req, res) => {
Category.find().then((category) => {
return (!category)
? res.status(404).send({ message: "category not found" })
: res.status(200).send(category)
})
})
// get all the categories of a subject as a parameter : NO OK a revoir
/*app.get('/api/:subject/categories', (req, res) => {
Subject.findOne({ name: req.params.subject }).then((subject) => {
if (!subject) {
return res.status(404).send({ message: "subject not found" })
}
Category.find({ subject: subject }).then((category) => {
return (!category)
? res.status(404).send({ message: "category not found" })
: res.status(200).send(category)
})
})
})*/
// get all the subjects : OK
app.get('/api/subjects', (req, res) => {
Subject.find().then((subject) => {
return (!subject)
? res.status(404).send({ message: "subject not found" })
: res.status(200).send(subject)
})
})
// get a particular subject with id as parameter : OK
app.get('/api/subjects/:id', (req, res) => {
Subject.findById(req.params.id).then((subject) => {
return (!subject)
? res.status(404).send({ message: "subject not found" })
: res.status(200).send(subject)
})
})
// get a particular category with id as parameter : OK
app.get('/api/categories/:id', (req, res) => {
Category.findById(req.params.id).then((category) => {
return (!category)
? res.status(404).send({ message: "category not found" })
: res.status(200).send(category)
})
})
// get all the user admin : OK
app.get('/api/useradmin', (req, res) => {
UserAdmin.find().then((userAdmin) => {
return (!userAdmin)
? res.status(404).send({ message: "user admin not found" })
: res.status(200).send(userAdmin)
})
})
// get a particular user admin with id as parameter : OK
app.get('/api/useradmin/:id', (req, res) => {
UserAdmin.findById(req.params.id).then((userAdmin) => {
return (!userAdmin)
? res.status(404).send({ message: "user admin not found" })
: res.status(200).send(userAdmin)
})
})
/**
* PUT
* */
// update a particular knowing the subject, category and id as parameters : OK
app.put('/api/:subject/:category/resources/:id', (req, res) => {
Subject.findOne({ name: req.params.subject })
.then((subject) => {
if (!subject) {
return res.status(404).send('subject not found');
}
Category.findOne({ name: req.params.category })
.then((category) => {
if (!category) {
return res.status(404).send('sategory not found');
}
Resource.findById(req.params.id)
.then((resource) => {
if (!resource) {
return res.status(404).send('resource not found')
}
else {
resource.title = req.body.title
resource.description = req.body.description
resource.url = req.body.url
resource.category = req.body.category
resource.subject = req.body.subject
resource.authors = req.body.authors
resource.save()
return res.status(200).send({ message: "updated successfully" })
}
})
.catch((error) => {
console.log(error);
return res.status(404).send('Internal server error')
})
})
.catch((error) => {
console.log(error);
return res.status(404).send('Internal server error')
})
})
.catch((error) => {
console.log(error);
return res.status(404).send('Internal server error')
})
})
// update a particular resource knowing its id as a parameter : OK
app.put('/api/resources/:id', (req, res) => {
var newSubject, newCategory;
Subject.findOne({ name: req.body.subject }).then((subject) => {
if (!subject) {
newSubject = new Subject({ name: req.body.subject });
newSubject.save().then(() => {
console.log({ message: "new subject added successfully", })
})
} else {
newSubject = subject;
console.log(newSubject)
}
Category.findOne({ name: req.body.category }).then((category) => {
if (!category) {
newCategory = new Category({ name: req.body.category, subject: newSubject.name });
newCategory.save().then(() => {
console.log({ message: "new category added successfully" })
})
} else {
newCategory = category;
console.log(newCategory)
}
Resource.findById(req.params.id).then((resource) => {
if (!resource) {
return res.status(404).send({ message: "resource not found" })
}
else {
resource.title = req.body.title
resource.description = req.body.description
resource.url = req.body.url
resource.category = newCategory.name
resource.subject = newSubject.name
resource.authors = req.body.authors
resource.save()
return res.status(200).send({ message: "updated successfully" })
}
})
})
})
})
// update a particular category knowing its is as parameter : OK
app.put('/api/categories/:id', (req, res) => {
Category.findById(req.params.id).then((category_) => {
if (!category_) {
return res.status(404).send({ message: "category not found" })
}
else {
category_.name = req.body.name
category_.save()
Resource.find({ category: category_.name}).then((resource)=>{
resource.json().forEach((r)=>{
r.category = req.body.name
r.save()
})
})
return res.status(200).send({ message: "updated successfully" })
}
})
})
// update a particular category knowing its is as parameter : OK
app.put('/api/subjects/:id', (req, res) => {
Subject.findById(req.params.id).then((subject_) => {
if (!subject_) {
return res.status(404).send({ message: "subject not found" })
}
else {
subject_.name = req.body.name
subject_.save()
Category.find({ subject: subject_.name}).then((subjectCat)=>{
subjectCat.json().forEach((sc)=>{
sc.subject = req.body.name
sc.save()
})
Resource.find({ subject: subject_.name}).then((resource)=>{
resource.json().forEach((r)=>{
r.subject = req.body.name
r.save()
})
})
})
return res.status(200).send({ message: "updated successfully" })
}
})
})
// update a user admin with id given in parameter : OK
app.put('/api/useradmin/:id', (req, res) => {
UserAdmin.findById(req.params.id).then((userAdmin) => {
if (!userAdmin){
return res.status(404).send({ message: "user admin not found" })
}
else{
userAdmin.username = req.body.username
userAdmin.password = req.body.password
userAdmin.save()
return res.status(200).send({message: "updated successfully"})
}
})
})
/**
* DELETE
* */
// delete a particular subject : OK
app.delete('/api/subjects/:id', (req, res) => {
Subject.findByIdAndDelete(req.params.id).then((subject) => {
return (!subject)
? res.status(404).send({ message: "subject not found" })
: res.status(200).send({ message: "deleted successfully" })
})
})
// delete a particular category : OK
app.delete('/api/categories/:id', (req, res) => {
Category.findByIdAndDelete(req.params.id).then((category) => {
return (!category)
? res.status(404).send({ message: "category not found" })
: res.status(200).send({ message: "deleted successfully" })
})
})
/*// delete the categories knowing the subject as parameter : a revoir
app.delete('/api/:subject/categories', (req, res) => {
Subject.findOne({name: req.params.subject}).then((subject) => {
if (!subject) {
return res.status(404).send({ message: "subject not found" })
}
Category.find({ subject: subject }).then((category) => {
if (!category) {
return res.status(404).send({ message: "category not found" })
}
else {
Category.findByIdAndDelete(category.id)
return res.status(200).send({ message: "deleted successfully" })
}
})
})
})
// delete the resources knowing a category as parameter : a revoir
app.delete('/api/:category/resources', (req, res) => {
Category.find({ name: req.params.category }).then((category) => {
if (!category) {
return res.status(404).send({ message: "category not found" })
}
Resource.find({ category: category }).then((resource) => {
if (!resource) {
return res.status(400).send({ message: "resource not found" })
}
else {
resource.delete()
return res.status(200).send({ message: "deleted successfully" })
}
})
})
})*/
// delete a particumlar resource knowing its id : OK
app.delete('/api/resources/:id', (req, res) => {
Resource.findByIdAndDelete(req.params.id).then((resource) => {
return (!resource)
? res.status(404).send({ message: "resourcee not found" })
: res.status(200).send({ message: "deleted successfully" })
})
})
// delete a particular user admin : OK
app.delete('/api/useradmin/:id', (req, res) => {
UserAdmin.findByIdAndDelete(req.params.id).then((userAdmin) => {
return (!userAdmin)
? res.status(404).send({ message: "user admin not found" })
: res.status(200).send({message: `${userAdmin.username} is deleted successfully`})
})
})