reserves.js 2.87 KB
Newer Older
1
const express = require('express');
2
const User = require('../schemas/user');
3
4
5
6
7
8
9
10
11
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({
12
        user: req.body._id,
13
        date: req.body.date,
14
15
16
17
        starttime: Number(req.body.starttime),
        usetime: Number(req.body.usetime),
        start: `${req.body.date}T`+`${req.body.starttime}:00:00`,
        end: `${req.body.date}T`+`${Number(req.body.starttime)+Number(req.body.usetime)}:00:00`,
18
19
        room: req.body.room,
        reason: req.body.reason,
CHAERIN KIM's avatar
CHAERIN KIM committed
20
        students: req.body.students,
21
        approve: req.body.approve,
CHAERIN KIM's avatar
CHAERIN KIM committed
22
        num: req.body.students.length + 1,
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    });

    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)
39
    Reserve.find({ user: req.params._id }, function (err, reserve) {
40
        console.log('id.name',reserve)
41
        if (err) return res.status(500).json({ error: err });
42
        console.log('reserve list', reserve)
43
44
45
46
47
        res.status(201).json(reserve);

    })
});

48
router.get('/admin/:_id', function (req, res, next) {
49
    console.log('/reserves/admin get req.params', req.params)
50
51
52
53
54
55
56
    Reserve.find({ approve: false }).populate('user').exec(function (err, reserve) {
        if (err) return res.status(500).json({ error: err });
        console.log('reserve list', reserve)
        res.status(201).json(reserve);
    })
});

57
58
59
60
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 });
61

62
63
64
65
66
67
68
69
70
71
72
73
        reserve.remove()
            .then(() => {
                console.log();
                res.status(201).json();
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    })
});

74
75
router.put('/:id', function (req, res, next) {
    console.log('/reserves put req.body', req.params)
76
    Reserve.findOne({ _id: req.params.id }, 'approve', function (err, reserve) {
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        if (err) return res.status(500).json({ error: err });
        reserve.approve = true;
        reserve.save()
            .then((result) => {
                console.log(result);
                res.status(201).json(result);
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    });
});

91
module.exports = router;