-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
449 lines (408 loc) · 14.8 KB
/
app.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { mongoose } from "mongoose";
import dotenv from "dotenv";
import express from "express";
import cors from "cors";
import { connectDB } from "./MongoModule/mongoConnect.js";
import { insertStationList, insertRoot, insertBlock } from "./MongoModule/insertDocument.js";
import { updateStationList, updateRoot, updateBlock } from "./MongoModule/updateDocument.js";
import { getStationInfo } from "./OpenAPI/stationInfoAPI.js";
import { findStationList, findBlockByPlace } from "./MongoModule/findDocument.js";
import express_session from "express-session";
import MongoStore from "connect-mongo";
dotenv.config();
const port = 8000;
const { MONGODB_URI, AUTHENTICATION_KEY, APP_KEY } = process.env;
const app = express();
app.use(cors());
app.use(express.json());
app.use(
express_session({
secret: AUTHENTICATION_KEY,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: MONGODB_URI,
}),
cookie: { maxAge: 3.6e6 * 24 },
})
);
app.post("/station/getStationList", async (req, res) => {
let stationInfo = {
stationName: req.body.stationName,
collectionID: "",
LineCnt: 0,
Line: [],
returnValue: false,
};
if (req.body.stationName == "서울") {
req.body.stationName = "서울역";
} else if (req.body.stationName == "서울역") {
stationInfo.stationName = "서울";
}
await getStationInfo(req.body.stationName)
.then((result) => {
if (result == undefined) {
res.status(400).json({ returnValue: false, errMsg: "역 이름이 아닙니다" });
} else if (result.list_total_count._text == 1) {
res.status(400).json({ returnValue: false, errMsg: "환승역이 아닙니다" });
} else {
stationInfo.LineCnt = result.list_total_count._text;
stationInfo.returnValue = true;
stationInfo.collectionID = "no" + result.row[0].STATION_CD._text;
for (let i = 0; i < stationInfo.LineCnt; i++) {
stationInfo.Line.push(result.row[i].LINE_NUM._text.replace("0", ""));
}
console.log("------------------------------------");
console.log("[/station/getLine] success!");
console.log(stationInfo);
console.log("------------------------------------");
req.session.collectionID = stationInfo.collectionID; // session 설정
req.session.line = stationInfo.Line;
req.session.stationName = stationInfo.stationName;
req.session.APP_KEY = req.body.APP_KEY;
res.json(stationInfo);
}
})
.catch((err) => {
console.log("------------------------------------");
console.log("[/station/getLine] error!");
console.log(err);
console.log("------------------------------------");
res.status(400).json({
returnValue: false,
errMsg: "API에 조회가 되지 않습니다.",
});
});
});
app.post("/root/getRoot", async (req, res) => {
// startAt이랑 endAt을 한번 더 검사하는 부분 추가했으면 좋겠음
if (req.session.APP_KEY != APP_KEY) {
res.status(400).json({
returnValue: false,
errMsg: "APP_KEY가 일치하지 않습니다.",
});
return;
}
let { stationName, collectionID, startAt, endAt, line } = req.body;
console.log(req.session);
if (Object.keys(req.session).length > 1) {
collectionID = req.session.collectionID;
line = req.session.line;
stationName = req.session.stationName;
}
let response = {
collectionID: collectionID,
rootID: "",
returnValue: true,
};
let stationInfo;
let rootInfo = {
startAt: startAt,
endAt: endAt,
};
let flag = true;
if (
collectionID == undefined ||
line == undefined ||
stationName == undefined ||
startAt == undefined ||
endAt == undefined
) {
res.status(404).json({
returnValue: false,
errMsg: "Incorrect Query!",
});
return;
}
let result_startAt, result_endAt;
let startAt_flag = true,
endAt_flag = true;
if (startAt.next == "서울") startAt.next = "서울역";
if (endAt.next == "서울") endAt.next = "서울역";
await getStationInfo(startAt.next)
.then((result) => {
result_startAt = result;
})
.catch((err) => {});
await getStationInfo(endAt.next)
.then((result) => {
result_endAt = result;
})
.catch((err) => {});
if (result_startAt == undefined || result_endAt == undefined) {
res.status(404).json({
returnValue: false,
errMsg: "Not exist station",
});
return;
}
if (result_startAt.list_total_count._text == "1") {
console.log(result_startAt.row.STATION_NM._text);
if (
!(
result_startAt.row.STATION_NM._text == startAt.next &&
result_startAt.row.LINE_NUM._text.replace("0", "") == startAt.line
)
) {
res.status(404).json({
returnValue: false,
errMsg: "Incorrect startAt.next station",
});
return;
}
} else {
for (let i = 0; i < result_startAt.list_total_count._text; i++) {
if (
result_startAt.row[i].STATION_NM._text == startAt.next &&
result_startAt.row[i].LINE_NUM._text.replace("0", "") == startAt.line
) {
startAt_flag = false;
break;
}
}
if (startAt_flag) {
res.status(404).json({
returnValue: false,
errMsg: "Incorrect startAt.next station",
});
return;
}
}
if (result_endAt.list_total_count._text == "1") {
if (!(result_endAt.row.STATION_NM._text == endAt.next && result_endAt.row.LINE_NUM._text.replace("0", "") == endAt.line)) {
res.status(404).json({
returnValue: false,
errMsg: "Incorrect endAt.next station",
});
return;
}
} else {
for (let i = 0; i < result_endAt.list_total_count._text; i++) {
if (
result_endAt.row[i].STATION_NM._text == endAt.next &&
result_endAt.row[i].LINE_NUM._text.replace("0", "") == endAt.line
) {
endAt_flag = false;
break;
}
}
if (endAt_flag) {
res.status(404).json({
returnValue: false,
errMsg: "Incorrect endAt.next station",
});
return;
}
}
/* stationList에서 해당 역이 있는 없는지를 판단 */
await findStationList(mongoose, stationName)
.then((result) => {
stationInfo = result;
})
.catch((err) => {
console.log("------------------------------------");
console.log("[/root/getRoot] error!");
console.log(err);
console.log("------------------------------------");
});
let newStationInfo;
if (stationInfo == null) {
console.log("stationList에 해당 역이 존재하지 않는다.");
// stationList에 해당 역이 존재하지 않는다.
newStationInfo = {
stationName: stationName,
collectionID: response.collectionID,
lineInfo: line,
rootInfo: [
{
startAt: startAt,
endAt: endAt,
rootID: "",
},
],
};
} else {
// stationList에 해당 역이 존재한다.
console.log("stationList에 해당 역이 존재한다.");
for (let i = 0; i < stationInfo.rootInfo.length; i++) {
if (
stationInfo.rootInfo[i].startAt.next == startAt.next &&
stationInfo.rootInfo[i].endAt.next == endAt.next
) {
// stationList에 해당 역의 데이터에, 해당 루트도 존재한다.
response.rootID = stationInfo.rootInfo[i].rootID;
flag = false;
}
}
}
if (response.rootID == "") {
console.log("stationList에 해당 역의 데이터에, 해당 루트가 존재하지 않는다.");
// stationList에 해당 역의 데이터에, 해당 루트가 존재하지 않는다.
await insertRoot(mongoose, rootInfo, response.collectionID).then(
// 루트를 만들어준다.
(result) => {
console.log(result);
response.rootID = result._id;
}
);
}
if (newStationInfo != undefined) {
console.log("stationList에 해당 역의 데이터가 없으니 추가해준다. -> insert");
// stationList에 해당 역의 데이터가 없으니 추가해준다. -> insert
newStationInfo.rootInfo[0].rootID = response.rootID;
console.log(newStationInfo);
insertStationList(mongoose, newStationInfo);
} else if (flag) {
console.log(
"stationList에 해당 역에 대한 데이터는 있으나, 해당 루트에 대한 데이터는 존재하지 않는다. -> update"
);
// stationList에 해당 역에 대한 데이터는 있으나, 해당 루트에 대한 데이터는 존재하지 않는다. -> update
let newRootInfo = {
startAt: startAt,
endAt: endAt,
rootID: response.rootID,
};
updateStationList(mongoose, newRootInfo, stationName);
}
console.log("------------------------------------");
console.log("[/root/getRoot]");
console.log(response);
console.log("------------------------------------");
req.session.rootID = response.rootID; // session 설정
req.session.blockID_List = [];
res.json(response);
});
app.post("/block/getBlock", async (req, res) => {
if (req.session.APP_KEY != APP_KEY) {
res.status(400).send({
returnValue: false,
errMsg: "APP_KEY가 일치하지 않습니다.",
});
return;
}
let { collectionID, from, to, content } = req.body;
if (Object.keys(req.session).length > 1) {
collectionID = req.session.collectionID;
}
let response = {
blockID: "",
originContent: [],
returnValue: true,
};
let block = await findBlockByPlace(mongoose, collectionID, from, to);
if (block == null) {
block = await insertBlock(mongoose, from, to, content, collectionID);
response.blockID = block._id;
} else {
response.blockID = block._id;
response.originContent = block.content;
}
console.log("------------------------------------");
console.log("[/block/getBlock]");
console.log(response);
console.log("------------------------------------");
req.session.blockID = response.blockID; // session 설정
console.log(req.session);
if (!req.session.blockID_List) {
req.session.blockID_List = [response.blockID];
} else {
req.session.blockID_List.push(response.blockID);
}
res.json(response);
});
app.patch("/block/patchBlock", async (req, res) => {
if (req.session.APP_KEY != APP_KEY) {
res.status(400).send({
returnValue: false,
errMsg: "APP_KEY가 일치하지 않습니다.",
});
return;
}
let { collectionID, blockID, content } = req.body;
if (Object.keys(req.session).length > 1) {
collectionID = req.session.collectionID;
blockID = req.session.blockID;
}
let response = { returnValue: true };
await updateBlock(mongoose, collectionID, blockID, content)
.then((result) => {
console.log("------------------------------------");
console.log("[/block/patchBlock]");
console.log(response);
console.log("------------------------------------");
res.json(response);
})
.catch((err) => {
response.returnValue = false;
response.errMsg = "Block 업데이트 과정에 에러가 발생했습니다.";
console.log("------------------------------------");
console.log("[/block/patchBlock]");
console.log(response);
console.log(err);
console.log("------------------------------------");
res.status(400).json(response);
});
});
app.patch("/root/patchRoot", async (req, res) => {
if (req.session.APP_KEY != APP_KEY) {
res.status(400).send({
returnValue: false,
errMsg: "APP_KEY가 일치하지 않습니다.",
});
return;
}
let { collectionID, rootID, blockID_List } = req.body;
console.log(req.session);
if (Object.keys(req.session).length > 1) {
collectionID = req.session.collectionID;
rootID = req.session.rootID;
blockID_List = req.session.blockID_List;
}
let response = { returnValue: true };
await updateRoot(mongoose, collectionID, rootID, blockID_List)
.then((result) => {
console.log("------------------------------------");
console.log("[/block/patchRoot]");
console.log(response);
console.log("------------------------------------");
req.session.destroy((err) => {
if (err) {
console.log("[block/patchRoot] session error!");
console.log(err);
} else {
console.log("[block/patchRoot] session successfully destroyed");
}
});
res.status(400).json(response);
})
.catch((err) => {
response.returnValue = false;
response.errMsg = "Root 업데이트 과정에 에러가 발생했습니다.";
console.log("------------------------------------");
console.log("[/block/patchRoot]");
console.log(response);
console.log(err);
console.log("------------------------------------");
res.status(400).json(response);
});
});
app.post("/cookie", (req, res) => {
console.log(req.session);
console.log(req.body.test);
res.json({
cookie: req.session,
tt: Object.keys(req.session).length,
});
});
app.post("/remove", (req, res) => {
res.json({
test: req.session.collectionID,
});
req.session.destroy((err) => {
console.log(err);
});
});
app.listen(port, () => {
console.log(`port is listening at ${port}`);
});
connectDB(mongoose, MONGODB_URI);