-
Notifications
You must be signed in to change notification settings - Fork 1
/
mahasiswaRouter.js
76 lines (71 loc) · 1.23 KB
/
mahasiswaRouter.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
var router = require('express').Router();
var data = require('./data');
var Mahasiswa= require('./db').Mahasiswa;
module.exports = router;
router.get('/',(req,res,next)=>{
Mahasiswa.findAll()
.then(res.send.bind(res))
.catch(next);
});
router.put('/:id',(req,res,next)=>{
Mahasiswa.update(
{
title : req.body.title,
body : req.body.body
},{
where : {
id : req.params.id
}
}
)
.then((mahasiswa)=>{
res.send(mahasiswa);
})
.catch(next);
});
router.delete('/:id',(req,res,next)=>{
Mahasiswa.destroy({
where :{
id : req.params.id
}
})
.then((data)=>{
if(data === 1){
res.send({
success: true,
});
}
})
.catch(next);
});
router.get('/:id',(req,res,next)=>{
var id = req.params.id;
console.log(req.body);
res.send(data[id]);
Mahasiswa.findOne({
where :{
id : req.params.id
}
})
.then((mahasiswa)=>{
if(!mahasiswa){
res.send('Data Tidak Ada');
}else{
res.send(mahasiswa);
}
})
.catch(next);
});
router.post('/',(req,res,next)=>{
Mahasiswa.create(req.body)
.then((mahasiswa)=>{
res.send(mahasiswa);
})
.catch((error)=>{
/*var getError = {
error.path : error.message
};*/
res.send(error.errors[0]);
})
.catch(next);
});