-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
207 lines (168 loc) · 5.84 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const express = require('express'),
app = express(),
mongodb = require('mongodb'),
mongoClient = mongodb.MongoClient,
bodyParser = require('body-parser'),
parseJson = bodyParser.json(),
dbUri = 'mongodb://root:1@ds046667.mlab.com:46667/room-reservation';
/* date */
let date = {
now: () => {
let date = new Date();
let now = date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear() + ' - ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
return now;
}
}
/* router */
let router = (app, db) => {
const collection = db.collection('days');
/* rest api for events */
/* get day data */
app.get('/api/', (req, res) => {
if (req.query.day) {
collection.find(
{
"day": req.query.day
}
).toArray((err, result) => {
(err) ? res.status(500).send(err) : res.status(200).send(result);
});
} else {
res.status(500).send('err');
}
});
/* insert new day */
app.post('/api/', parseJson, (req, res) => {
checkTimes(req.body).then((value) => {
if (value) {
const day = {
"day": req.body.day,
"title": req.body.title,
"room_id": req.body.room_id,
"who": req.body.who,
"start_time": req.body.start_time,
"end_time": req.body.end_time
};
collection.insert(day, (err, result) => {
(err) ? res.status(500).send(err) : res.status(200).send(result);
});
} else {
res.status(500).end();
}
});
});
/* insert new data in day
or edit old data */
app.put('/api/', parseJson, (req, res) => {
checkTimes(req.body).then((value) => {
if (value) {
collection.update(
{_id: new mongodb.ObjectID(req.body._id)},
{
$set: {
"day": req.body.day,
"title": req.body.title,
"room_id": req.body.room_id,
"who": req.body.who,
"start_time": req.body.start_time,
"end_time": req.body.end_time
}
}
,(err, result) => {
(err) ? res.status(500).send(err) : res.status(200).send(result);
});
} else {
res.status(500).end();
}
});
});
app.delete('/api/:id', (req, res) => {
if (req.params.id) {
collection.remove({_id: new mongodb.ObjectID(req.params.id)}, (err, result) => {
(err) ? res.status(500).send(err) : res.status(200).send(result);
});
} else {
res.status(500).send('error');
}
});
/* rest api for rooms */
app.get('/api/rooms/:id', (req, res) => {
if (req.params.id) {
collection.findOne(
{
"type": req.params.id
},
(err, result) => {
(err) ? res.status(500).send(err) : res.status(200).send(result);
});
} else {
res.status(500).send('error');
}
});
app.put('/api/rooms', parseJson, (req, res) => {
if (req.body._id) {
collection.update(
{_id: new mongodb.ObjectID(req.body._id)},
{
$set: {
"data": req.body.data
}
}
,(err, result) => {
// collection.remove({"room.id": req.body.data.length ... }) if need;
(err) ? res.status(500).send(err) : res.status(200).send(result);
});
} else {
res.status(500).end();
}
});
/* check selected tiems in db */
let checkTimes = (body) => {
return new Promise ((resolve, reject) => {
if (!body.allowedDates) {
resolve(false);
} else {
let allowedDates = body.allowedDates;
/* edit */
if (body._id) {
collection.findOne(
{_id: new mongodb.ObjectID(body._id)},
(err, result) => {
for(let i = result.start_time; i <= result.end_time; i++) {
allowedDates[i] = true;
}
if (check(body, allowedDates)) {
resolve(true);
} else {
resolve(false);
}
});
} else {
/* create */
if (check(body, allowedDates)) {
resolve(true);
} else {
resolve(false);
}
}
}
});
}
}
let check = (body, allowedDates) => {
let check = true;
for(let i = body.start_time; i <= body.end_time; i++) {
if (!allowedDates[i]) {
check = false;
}
}
return check;
}
/* db connect */
mongoClient.connect(dbUri, (err, db) => {
router(app, db);
});
/* start server */
app.listen(8890, () => {
console.log('listen 8890'+ ' at ' + date.now());
});