-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
66 lines (56 loc) · 1.7 KB
/
api.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
const express = require('express');
const router = express.Router();
const Student = require('./student.js');
// all students
router.get('/', function(req, res, next){
Student.find().then(function(student){
console.log(student);
res.send(student);
}).catch(next);
});
// search by reg_id
router.get('/reg_id/:id', function(req, res, next){
console.log("reg test");
Student.find({reg_id: req.params.id}).then(function(student){
res.send(student);
});
});
// search by roll_no
router.get('/roll_no/:id', function(req, res, next){
Student.find({roll_no: req.params.id}).then(function(student){
res.send(student);
});
});
// search by name
router.get('/name/:id', function(req, res, next){
Student.find({name: req.params.id}).then(function(student){
res.send(student);
});
});
// search by class
router.get('/class/:id', function(req, res, next){
Student.find({class: req.params.id}).then(function(student){
res.send(student);
});
});
// add student
router.post('/', function(req, res, next){
Student.create(req.body).then(function(student){
res.send(student);
}).catch(next);
});
// update (search by reg_id; cannot update reg_id)
router.put('/reg_id/:id', function(req, res, next){
Student.findOneAndUpdate({reg_id: req.params.id}, req.body).then(function(){
Student.findOne({reg_id: req.params.id}).then(function(student){
res.send(student);
});
});
});
// delete student (search by reg_id)
router.delete('/reg_id/:id', function(req, res, next){
Student.findOneAndRemove({reg_id: req.params.id}).then(function(student){
res.send(student);
});
});
module.exports = router;