reserves.js 1.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
const express = require('express');
const Reserve = require('../schemas/reserve');
const { verifyToken } = require('./middlewares');

const router = express.Router();

router.post('/', function (req, res, next) {
    console.log('/reserve post req.body', req.body)

    const reserve = new Reserve({
CHAERIN KIM's avatar
CHAERIN KIM committed
11
        id: req.body._id,
12
13
14
15
16
        date: req.body.date,
        time: req.body.time,
        room: req.body.room,
        name: req.body.name,
        reason: req.body.reason,
CHAERIN KIM's avatar
CHAERIN KIM committed
17
        students: req.body.students,
18
        approve: req.body.approve,
CHAERIN KIM's avatar
CHAERIN KIM committed
19
        num: req.body.students.length + 1,
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    });

    reserve.save()
        .then((result) => {
            console.log(result);
            res.status(201).json(result);
        })
        .catch((err) => {
            console.error(err);
            next(err);
        });
});

// router.get('/:_id', verifyToken, function (req, res, next) {
router.get('/:_id', function (req, res, next) {
    console.log('/reserves get req.params', req.params)
    Reserve.find({ id: req.params._id }, function (err, reserve) {
        if (err) return res.status(500).json({ error: err });
        console.log('reserve list',reserve)
        res.status(201).json(reserve);

    })
});

router.delete('/:_id', function (req, res, next) {
    console.log('/reserves delete req.params', req.params)
    Reserve.findOne({ _id: req.params._id }, function (err, reserve) {
        if (err) return res.status(500).json({ error: err });
        
        reserve.remove()
            .then(() => {
                console.log();
                res.status(201).json();
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    })
});

module.exports = router;