const express = require('express'); const User = require('../schemas/user'); 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({ user: req.body._id, date: req.body.date, 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`, room: req.body.room, reason: req.body.reason, students: req.body.students, approve: true, check: true, num: req.body.students.length + 1, }); 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 reserveArr = reserves.map(item => ( (reserve.start >= item.start && reserve.start < item.end) || (reserve.end > item.start && reserve.end <= item.end) ? "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: "다른 시간을 선택해주세요." }) }) }); 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); }) }) // router.get('/:_id', verifyToken, function (req, res, next) { router.get('/:_id', verifyToken, function (req, res, next) { console.log('/reserves get req.params', req.params) Reserve.find({ user: req.params._id }, function (err, reserve) { if (err) return res.status(500).json({ error: err }); console.log('/change date', new Date()) const reserves = reserve.map(item => ( //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 : "" )); if (!reserves) { console.log("no신청") res.status(404).json({ error: "신청내역이 없습니다." }) } console.log("reserves", reserves) res.status(201).json(reserves); }) }); router.get('/admin/:_id', verifyToken, function (req, res, next) { console.log('/reserves/admin get req.params', req.params) 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); }) }); 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); }); }) }); router.put('/:id', function (req, res, next) { console.log('/reserves put req.body', req.params) Reserve.findOne({ _id: req.params.id }, 'check approve', function (err, reserve) { if (err) return res.status(500).json({ error: err }); reserve.check = true; reserve.approve = req.body.approve; reserve.save() .then((result) => { console.log(result); res.status(201).json(result); }) .catch((err) => { console.error(err); next(err); }); }); }); module.exports = router;