reviews.js 1.66 KB
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('/lecturename/:lecturename', (req, res) => {
  Review.findBylecturename(req.params.lecturename)
    .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));
});

router.get('/proname/:proname', (req, res) => {
  Review.findByproname(req.params.proname)
    .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 lecturename
router.put('/lecturename/:lecturename', (req, res) => {
  Review.updateBylecturename(req.params.lecturename, req.body)
    .then(Review => res.send(Review))
    .catch(err => res.status(500).send(err));
});

// Delete by lecturename
router.delete('/lecturename/:lecturename', (req, res) => {
  Review.deleteBylecturename(req.params.lecturename)
    .then(() => res.sendStatus(200))
    .catch(err => res.status(500).send(err));
});

module.exports = router;