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

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

module.exports = router;