lectures.js 1.36 KB
const router = require('express').Router();
const Lecture = require('../models/lecture');

// Find All
router.get('/', (req, res) => {
  Lecture.findAll()
    .then((lectures) => {
      if (!lectures.length) return res.status(404).send({ err: 'Lecture not found' });
      res.send(`find successfully: ${lectures}`);
    })
    .catch(err => res.status(500).send(err));
});

// Find One by lectureid
router.get('/lectureid/:lectureid', (req, res) => {
  Lecture.findOneBylectureid(req.params.lectureid)
    .then((lecture) => {
      if (!lecture) return res.status(404).send({ err: 'Lecture not found' });
      res.send(`findOne successfully: ${lecture}`);
    })
    .catch(err => res.status(500).send(err));
});

// Create new lecture document
router.post('/', (req, res) => {
  console.log(req.body)
  Lecture.create(req.body)
    .then(lecture => res.send(lecture))
    .catch(err => res.status(500).send(err));
});

// Update by lectureid
router.put('/lectureid/:lectureid', (req, res) => {
  Lecture.updateBylectureid(req.params.lectureid, req.body)
    .then(lecture => res.send(lecture))
    .catch(err => res.status(500).send(err));
});

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

module.exports = router;