reserves.js 4.64 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,
CHAERIN KIM's avatar
CHAERIN KIM committed
20
21
        approve: true,
        check: true,
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
39
40
41
    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 => (
42
43
            (strt >= new Date(item.start) && strt < new Date(item.end)) ||
                (endt > new Date(item.start) && endt <= new Date(item.end)) ?
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
                "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: "다른 시간을 선택해주세요." })
    })

62
63
});

64
65
66
67
68
69
70
71
72
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);
    })
})

73
// router.get('/:_id', verifyToken, function (req, res, next) {
CHAERIN KIM's avatar
CHAERIN KIM committed
74
router.get('/:_id', verifyToken, function (req, res, next) {
75
    console.log('/reserves get req.params', req.params)
76
    Reserve.find({ user: req.params._id }, function (err, reserve) {
77
        if (err) return res.status(500).json({ error: err });
CHAERIN KIM's avatar
CHAERIN KIM committed
78
79
80
81
82
83
84
85
86
87
88
        // console.log(reserve, Date.now())
        const reserves = reserve.map(item => (
            new Date(item.end) >= Date.now() ? item : ""
        ));

        if (!reserves) {
            console.log("no신청")
            res.status(404).json({error: "신청내역이 없습니다."})
        }
        console.log("reserves",reserves)
        res.status(201).json(reserves);
89
90
91
    })
});

CHAERIN KIM's avatar
CHAERIN KIM committed
92
router.get('/admin/:_id', verifyToken, function (req, res, next) {
93
    console.log('/reserves/admin get req.params', req.params)
94
95
96
97
98
99
100
    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);
    })
});

101
102
103
104
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 });
105

106
107
108
109
110
111
112
113
114
115
116
117
        reserve.remove()
            .then(() => {
                console.log();
                res.status(201).json();
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    })
});

118
119
router.put('/:id', function (req, res, next) {
    console.log('/reserves put req.body', req.params)
120
    Reserve.findOne({ _id: req.params.id }, 'check approve', function (err, reserve) {
121
        if (err) return res.status(500).json({ error: err });
122
123
        reserve.check = true;
        reserve.approve = req.body.approve;
124
125
126
127
128
129
130
131
132
133
134
135
        reserve.save()
            .then((result) => {
                console.log(result);
                res.status(201).json(result);
            })
            .catch((err) => {
                console.error(err);
                next(err);
            });
    });
});

136
module.exports = router;