api.js
12.7 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const XMLHttpRequest = require('xhr2');
mongoose.connect('mongodb+srv://choieunseok:uA3mhjPcB3DwsuuD@cluster0.2gsua4u.mongodb.net/?retryWrites=true&w=majority');
const db = mongoose.connection
db.on('error', console.error)
db.once('open', () => {
console.log('Connected to mongodb Server')
});
const dayPostList = mongoose.Schema({
date: 'string',
idArray: [{ type: String }]
});
const dayPostListModel = mongoose.model('dayPostList', dayPostList);
const post = mongoose.Schema({
date: 'string',
time: 'string',
title: 'string',
content: 'string',
password: 'string'
});
const postModel = mongoose.model('post', post);
const waiting = mongoose.Schema({
date: 'string',
waiting: []
});
const waitingModel = mongoose.model('waiting', waiting);
// router.get('/api', (req, res) => {
// res.send({ test: "hi" });
// });
function getKSTDate(){
const curr = new Date();
const utc = curr.getTime() + (curr.getTimezoneOffset() * 60 * 1000);
const KR_TIME_DIFF = 9 * 60 * 60 * 1000;
const kr_curr = new Date(utc + (KR_TIME_DIFF));
return kr_curr;
}
function getCurrentDate(originDate) {
var date;
if (originDate == null) date = getKSTDate();
else date = new Date(originDate);
var year = date.getFullYear().toString();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month.toString() : month.toString();
var day = date.getDate();
day = day < 10 ? '0' + day.toString() : day.toString();
return year + '-' + month + '-' + day;
}
function getCurrentTime() {
var date = getKSTDate();
var hour = date.getHours();
hour = hour < 10 ? '0' + hour.toString() : hour.toString();
var minute = date.getMinutes();
minute = minute < 10 ? '0' + minute.toString() : minute.toString();
return hour + ":" + minute;
}
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
router.get('/api/getList', async (req, res) => { // 오늘 게시물들의 아이디 표시
try {
const today = getCurrentDate();
var testDayPostList = await dayPostListModel.findOne({ date: today });
if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
res.send(testDayPostList.idArray);
}
catch (err) {
res.send(err.message);
}
});
router.get('/api/getList/:date', async (req, res) => { // 특정 날자의 게시물들의 아이디 표시
try {
const today = getCurrentDate(req.params.date);
var testDayPostList = await dayPostListModel.findOne({ date: today });
if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
res.send(testDayPostList.idArray);
}
catch (err) {
res.send(err.message);
}
});
router.post('/api/get', async (req, res) => { // 특정 id(여러개)의 게시물 내용 요약 불러오기
try {
const idArray = req.body.idArray;
var resultArray = [];
for (const id of idArray) {
const onePost = await postModel.findById(id);
var tempJSON = {};
tempJSON.id = onePost.id;
tempJSON.time = onePost.time;
tempJSON.title = onePost.title;
tempJSON.content = onePost.content;
tempJSON.content = tempJSON.content.replace(/(?:\r\n|\r|\n| )/g, '');
const sliceLength = 10;
if (tempJSON.title.length > sliceLength) tempJSON.title = tempJSON.title.slice(0, sliceLength) + "...";
if (tempJSON.content.length > sliceLength) tempJSON.content = tempJSON.content.slice(0, sliceLength) + "...";
resultArray.push(tempJSON);
}
res.send(resultArray);
}
catch (err) {
res.send(err.message);
}
});
router.get('/api/get/:id', async (req, res) => { // 특정 id의 게시물 불러오기
try {
const currentPost = await postModel.findById(req.params.id);
res.send({ date: currentPost.date, time: currentPost.time, title: currentPost.title, content: currentPost.content });
}
catch (err) {
res.send(err.message);
}
});
router.post('/api/isPassEqual', async (req, res) => { // 암호가 같으면 success, 아니면 failed
try {
const currentPost = await postModel.findById(req.body.id);
if (currentPost.password == req.body.password) res.send("success");
else res.send("failed");
}
catch (err) {
res.send("failed");
}
});
router.post('/api/postSave', async (req, res) => { // 오늘 게시물 작성
try {
var isFirst = false;
const today = getCurrentDate();
const time = getCurrentTime();
var testDayPostList = await dayPostListModel.findOne({ date: today });
if (testDayPostList == null) {
testDayPostList = new dayPostListModel({ date: today, idArray: [] });
isFirst = true;
}
var postListArr = testDayPostList.idArray;
var newPost = new postModel({ date: today, time:time, title: req.body.title, content: req.body.content, password: req.body.password });
var newPostData = await newPost.save();
postListArr.push(newPostData._id.toString());
if (isFirst) await testDayPostList.save();
else await dayPostListModel.updateOne({ date: today }, { idArray: postListArr });
res.send("success");
}
catch (err) {
res.send(err.message);
}
});
router.put('/api/edit/:id', async (req, res) => { // 게시물 수정
try {
const id = req.params.id;
const title = req.body.title;
const content = req.body.content;
await postModel.findByIdAndUpdate(id, { title: title, content: content });
res.send("success");
}
catch (err) {
res.send(err.message);
}
});
router.delete('/api/delete/:id', async (req, res) => { // 게시물 삭제
try {
const id = req.params.id;
const list = await dayPostListModel.find();
for (const dayList of list) {
var newArray = dayList.idArray.filter((data) => { return data != id; })
if (!arrayEquals(dayList.idArray, newArray)) {
await dayPostListModel.findByIdAndUpdate(dayList._id.toString(), { idArray: newArray });
}
}
await postModel.findByIdAndDelete(id);
res.send("success");
}
catch (err) {
res.send(err.message);
}
});
router.get('/api/waiting', async (req, res) => { // 오늘의 대기시간 목록 가져오기
try {
const today = getCurrentDate();
var waitingList = await waitingModel.findOne({ date: today });
if (waitingList == null) waitingList = []
else waitingList = waitingList.waiting;
const date = getKSTDate();
var hour = date.getHours();
hour = hour < 10 ? '0' + hour.toString() : hour.toString();
var minute = date.getMinutes();
minute = minute < 10 ? '0' + minute.toString() : minute.toString();
const time = Number(hour + minute);
var result = [];
for (const waiting of waitingList) {
if (time - Number(waiting.time.replace(':', '')) <= 100) {
result.push(waiting)
}
}
res.send(result);
}
catch (err) {
res.send(err.message);
}
});
router.post('/api/waiting', async (req, res) => { // 오늘의 대기시간 목록 추가
try {
const today = getCurrentDate();
var waitingList = await waitingModel.findOne({ date: today });
const time = getCurrentTime();
const newWaiting = { value: req.body.value, time: time };
if (waitingList == null) await waitingModel({ date: today, waiting: [newWaiting] }).save();
else {
waitingList.waiting.push(newWaiting);
await waitingModel.findOneAndUpdate({ date: today }, { waiting: waitingList.waiting });
}
var waitingListResult = await waitingModel.findOne({ date: today });
res.send(waitingListResult);
}
catch (err) {
res.send(err.message);
}
});
function setting(resultJson) {
var result = {}
result.fo_date = [resultJson.fo_date1, resultJson.fo_date2, resultJson.fo_date3, resultJson.fo_date4, resultJson.fo_date5];
result.fo_menu_lun = [resultJson.fo_menu_lun1, resultJson.fo_menu_lun2, resultJson.fo_menu_lun3, resultJson.fo_menu_lun4, resultJson.fo_menu_lun5];
result.fo_menu_eve = [resultJson.fo_menu_eve1, resultJson.fo_menu_eve2, resultJson.fo_menu_eve3, resultJson.fo_menu_eve4, resultJson.fo_menu_eve5];
return result;
}
router.get('/api/menuList', async (req, res) => { // 제2기숙사 학식 메뉴 일주일치 불러오기
try {
let newRequest = new XMLHttpRequest();
newRequest.onreadystatechange = () => {
if (newRequest.status == 200 && newRequest.readyState == 4) {
var dt = getKSTDate();
if (dt.getDay() == 0 || dt.getDay() == 6) {
resJSON0 = JSON.parse(newRequest.responseText).root[0].LASTNEXT[0]
resJSON1 = JSON.parse(newRequest.responseText).root[0].LASTNEXT[1]
if (resJSON0.go === "next_mon") {
let nextRequest = new XMLHttpRequest();
nextRequest.onreadystatechange = () => {
if (nextRequest.status == 200 && nextRequest.readyState == 4) {
newResJSON = JSON.parse(nextRequest.responseText).root[0].WEEKLYMENU[0]
res.send(setting(newResJSON));
}
}
nextRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc')
nextRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
nextRequest.send("locgbn=K1&sch_date=" + resJSON0.mon_date + "&fo_gbn=stu")
} else if (resJSON1.go === "next_mon") {
let nextRequest = new XMLHttpRequest();
nextRequest.onreadystatechange = () => {
if (nextRequest.status == 200 && nextRequest.readyState == 4) {
newResJSON = JSON.parse(nextRequest.responseText).root[0].WEEKLYMENU[0]
res.send(setting(newResJSON));
}
}
nextRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc')
nextRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
nextRequest.send("locgbn=K1&sch_date=" + resJSON1.mon_date + "&fo_gbn=stu")
}
}
else {
resJSON = JSON.parse(newRequest.responseText).root[0].WEEKLYMENU[0]
res.send(setting(resJSON));
}
}
}
newRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc')
newRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
newRequest.send("locgbn=K1&sch_date=&fo_gbn=stu")
}
catch (err) {
res.send(err.message);
}
});
router.get('/api/todayMenu', async (req, res) => { // 제2기숙사 학식 메뉴 하루치 불러오기
try {
let newRequest = new XMLHttpRequest();
newRequest.onreadystatechange = () => {
if (newRequest.status == 200 && newRequest.readyState == 4) {
var dt = getKSTDate();
if (dt.getDay() > 0 && dt.getDay() < 6) {
resJSON = JSON.parse(newRequest.responseText).root[0].WEEKLYMENU[0];
const weekMenu = setting(resJSON);
const todayMenu = [weekMenu.fo_menu_lun[dt.getDay() - 1], weekMenu.fo_menu_eve[dt.getDay() - 1]];
res.send(todayMenu);
} else res.send("weekend");
}
}
newRequest.open('POST', 'https://dorm2.khu.ac.kr/food/getWeeklyMenu.kmc')
newRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
newRequest.send("locgbn=K1&sch_date=&fo_gbn=stu")
}
catch (err) {
res.send(err.message);
}
});
// router.get('/api/testSave', async (req, res) => {
// var isFirst = false;
// var testDayPostList = await dayPostListModel.findOne({ date: '2022-05-30' });
// if (testDayPostList == null) {
// testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] });
// isFirst = true;
// }
// var postListArr = testDayPostList.idArray;
// var newPost = new postModel({ date: '2022-05-30', title: '테스트 제목', content: '테스트 내용', password: 'password' });
// var newPostData = await newPost.save();
// postListArr.push(newPostData._id.toString());
// if (isFirst) await testDayPostList.save();
// else await dayPostListModel.updateOne({ date: '2022-05-30' }, { idArray: postListArr });
// res.send("test");
// });
module.exports = router;