reserves.js 4.77 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
    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);
                });
Choi Ga Young's avatar
Choi Ga Young committed
38
        };
39
        const reserveArr = reserves.map(item => (
Choi Ga Young's avatar
Choi Ga Young committed
40
41
42
            (reserve.start >= item.start && reserve.start < item.end) ||
                (reserve.end > item.start && reserve.end <= item.end) ?
                "item" : null
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        ))
        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: "다른 시간을 선택해주세요." })
    })

59
60
});

61
62
63
64
65
66
67
68
69
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);
    })
})

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

        if (!reserves) {
Choi Ga Young's avatar
Choi Ga Young committed
86
87
            console.log("no신청") 
            res.status(404).json({ error: "신청내역이 없습니다." })
CHAERIN KIM's avatar
CHAERIN KIM committed
88
        }
Choi Ga Young's avatar
Choi Ga Young committed
89
        console.log("reserves", reserves)
CHAERIN KIM's avatar
CHAERIN KIM committed
90
        res.status(201).json(reserves);
91
92
93
    })
});

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

103
104
105
106
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 });
107

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

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

138
module.exports = router;