reviews.js
1.32 KB
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
const router = require('express').Router();
const Review = require('../models/review');
// Find All
router.get('/', (req, res) => {
Review.findAll()
.then((reviews) => {
if (!reviews.length) return res.status(404).send({ err: 'Review not found' });
res.send(`find successfully: ${reviews}`);
})
.catch(err => res.status(500).send(err));
});
// Find One by reviewid
router.get('/reviewid/:reviewid', (req, res) => {
Review.findOneByreviewid(req.params.reviewid)
.then((review) => {
if (!review) return res.status(404).send({ err: 'Review not found' });
res.send(`findOne successfully: ${review}`);
})
.catch(err => res.status(500).send(err));
});
// Create new review document
router.post('/', (req, res) => {
console.log(req.body)
Review.create(req.body)
.then(review => res.send(review))
.catch(err => res.status(500).send(err));
});
// Update by reviewid
router.put('/reviewid/:reviewid', (req, res) => {
Review.updateByreviewid(req.params.reviewid, req.body)
.then(review => res.send(review))
.catch(err => res.status(500).send(err));
});
// Delete by reviewid
router.delete('/reviewid/:reviewid', (req, res) => {
Review.deleteByreviewid(req.params.reviewid)
.then(() => res.sendStatus(200))
.catch(err => res.status(500).send(err));
});
module.exports = router;