notices.js 1.06 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
const express = require('express');
const Notice = require('../models/notice');

const router = express.Router();

router.get('/', function (req, res, next) {
Yoon, Daeki's avatar
Yoon, Daeki committed
7
8
9
10
11
12
  Notice.find({}).sort({ post_date: -1 })
    .then((notices) => {
      res.status(201).json(notices);
    })
    .catch((err) => {
      console.error(err);
Yoon, Daeki's avatar
Yoon, Daeki committed
13

Yoon, Daeki's avatar
Yoon, Daeki committed
14
15
16
      next(err);
    });
  // res.status(404).json({error:"없음."})
Yoon, Daeki's avatar
Yoon, Daeki committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  // res.status(201).json(notices);
});

router.get('/:id', function (req, res, next) {
  Notice.findOne({ _id: req.params.id }, function (err, notice) {
      if (err) return res.status(500).json({ error: err });
      res.status(201).json(notice);
  })
});

router.delete('/:id', function (req, res, next) {
  Notice.findOne({ _id: req.params.id }, function (err, notice) {
      if (err) return res.status(500).json({ error: err });
      notice.remove()
          .then(() => {
              console.log();
              res.status(201).json();
          })
          .catch((err) => {
              console.error(err);
              next(err);
          });
  })
Yoon, Daeki's avatar
Yoon, Daeki committed
40
41
42
});

module.exports = router;