reserves.js 4.26 KB
Newer Older
1
const express = require('express');
2
const User = require('../schemas/user');
3
4
5
6
7
8
9
10
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({
11
        user: req.body._id,
12
        date: req.body.date,
13
14
        starttime: Number(req.body.starttime),
        usetime: Number(req.body.usetime),
15
16
        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`,
17
18
        room: req.body.room,
        reason: req.body.reason,
CHAERIN KIM's avatar
CHAERIN KIM committed
19
        students: req.body.students,
20
        approve: req.body.approve,
CHAERIN KIM's avatar
CHAERIN KIM committed
21
        num: req.body.students.length + 1,
22
23
    });

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    Reserve.find({ room: req.body.room, approve: true }, function (err, reserves) {
        if (err) return res.status(500).json({ error: err });

        if (!reserves) {
            reserve.save()
                .then((result) => {
                    console.log(result);
                    res.status(201).json(result);
                })
                .catch((err) => {
                    console.error(err);
                    next(err);
                });
        }
        const strt = new Date(reserve.start)
        const endt = new Date(reserve.end)
        const reserveArr = reserves.map(item => (
41
42
            (strt >= new Date(item.start) && strt < new Date(item.end)) ||
                (endt > new Date(item.start) && endt <= new Date(item.end)) ?
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
                "item" :
                null
        ))
        console.log("array", reserveArr)
        if (!reserveArr.includes("item")) {
            reserve.save()
                .then((result) => {
                    console.log(result);
                    res.status(201).json(reserves);
                })
                .catch((err) => {
                    console.error(err);
                    next(err);
                });
        }
        else return res.status(404).json({ error: "다른 시간을 선택해주세요." })
    })

61
62
});

63
64
65
66
67
68
69
70
71
router.get('/room/:room', function (req, res, next) {
    console.log('reserves get room req.params', req.params)
    Reserve.find({ room: req.params.room, approve: true }, function (err, reserve) {
        if (err) return res.status(500).json({ error: err });
        console.log('reserve room list', reserve);
        res.status(201).json(reserve);
    })
})

72
73
74
// router.get('/:_id', verifyToken, function (req, res, next) {
router.get('/:_id', function (req, res, next) {
    console.log('/reserves get req.params', req.params)
75
    Reserve.find({ user: req.params._id }, function (err, reserve) {
76
77
78
79
80
        if (err) return res.status(500).json({ error: err });
        res.status(201).json(reserve);
    })
});

81
router.get('/admin/:_id', function (req, res, next) {
82
    console.log('/reserves/admin get req.params', req.params)
83
84
85
86
87
88
89
    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);
    })
});

90
91
92
93
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 });
94

95
96
97
98
99
100
101
102
103
104
105
106
        reserve.remove()
            .then(() => {
                console.log();
                res.status(201).json();
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    })
});

107
108
router.put('/:id', function (req, res, next) {
    console.log('/reserves put req.body', req.params)
109
    Reserve.findOne({ _id: req.params.id }, 'check approve', function (err, reserve) {
110
        if (err) return res.status(500).json({ error: err });
111
112
        reserve.check = true;
        reserve.approve = req.body.approve;
113
114
115
116
117
118
119
120
121
122
123
124
        reserve.save()
            .then((result) => {
                console.log(result);
                res.status(201).json(result);
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    });
});

125
module.exports = router;