최은석

/api/getList, /api/getList/:date, /api/get, /api/get/:id, /api/isPassEqual, /api/postSave

...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 "": { 6 "": {
7 "dependencies": { 7 "dependencies": {
8 "axios": "^0.27.2", 8 "axios": "^0.27.2",
9 + "body-parser": "^1.20.0",
9 "concurrently": "^7.2.1", 10 "concurrently": "^7.2.1",
10 "express": "^4.18.1", 11 "express": "^4.18.1",
11 "http-proxy-middleware": "^2.0.6", 12 "http-proxy-middleware": "^2.0.6",
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 }, 6 },
7 "dependencies": { 7 "dependencies": {
8 "axios": "^0.27.2", 8 "axios": "^0.27.2",
9 + "body-parser": "^1.20.0",
9 "concurrently": "^7.2.1", 10 "concurrently": "^7.2.1",
10 "express": "^4.18.1", 11 "express": "^4.18.1",
11 "http-proxy-middleware": "^2.0.6", 12 "http-proxy-middleware": "^2.0.6",
......
...@@ -27,36 +27,104 @@ const postModel = mongoose.model('post', post); ...@@ -27,36 +27,104 @@ const postModel = mongoose.model('post', post);
27 // res.send({ test: "hi" }); 27 // res.send({ test: "hi" });
28 // }); 28 // });
29 29
30 -router.get('/api/getAll', (req, res) => { 30 +function getCurrentDate(originDate) {
31 - dayPostListModel.find(function (error, dayPostLists) { 31 + var date;
32 - console.log('--- Read all ---'); 32 + if(originDate == null) date = new Date();
33 - if (error) { 33 + else date = new Date(originDate);
34 - res.send(error); 34 + var year = date.getFullYear().toString();
35 - } else { 35 +
36 - res.send(dayPostLists); 36 + var month = date.getMonth() + 1;
37 + month = month < 10 ? '0' + month.toString() : month.toString();
38 +
39 + var day = date.getDate();
40 + day = day < 10 ? '0' + day.toString() : day.toString();
41 +
42 + return year + '-'+ month + '-'+ day ;
43 +}
44 +
45 +router.get('/api/getList', async(req, res) => {
46 + const today = getCurrentDate();
47 + var testDayPostList = await dayPostListModel.findOne({ date: today });
48 + if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
49 + res.send(testDayPostList.idArray);
50 +});
51 +
52 +router.get('/api/getList/:date', async(req, res) => {
53 + const today = getCurrentDate(req.params.date);
54 + var testDayPostList = await dayPostListModel.findOne({ date: today });
55 + if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
56 + res.send(testDayPostList.idArray);
57 +});
58 +
59 +router.get('/api/get', async(req, res) => {
60 + const idArray = req.body.idArray;
61 + var resultArray = [];
62 + for (const id of idArray){
63 + const onePost = await postModel.findById(id);
64 + var tempJSON = {};
65 + tempJSON.id = onePost.id;
66 + tempJSON.title = onePost.title;
67 + tempJSON.content = onePost.content;
68 + tempJSON.content = tempJSON.content.replace(/(?:\r\n|\r|\n)/g, '');
69 + const sliceLength = 10;
70 + if(tempJSON.content.length > sliceLength) tempJSON.content = tempJSON.content.slice(0,sliceLength) + "...";
71 + resultArray.push(tempJSON);
37 } 72 }
38 - }) 73 + res.send(resultArray);
74 +});
75 +
76 +router.get('/api/get/:id', async(req, res) => {
77 + const currentPost = await postModel.findById(req.params.id);
78 + res.send({ title: currentPost.title, content: currentPost.content });
39 }); 79 });
40 80
41 -router.get('/api/testSave', async(req, res) => { 81 +router.post('/api/isPassEqual', async(req, res) => {
82 + const currentPost = await postModel.findById(req.body.id);
83 + if (currentPost.password == req.body.password) res.send("success");
84 + else res.send("failed");
85 +});
86 +
87 +router.post('/api/postSave', async (req, res) => {
42 var isFirst = false; 88 var isFirst = false;
89 + const today = getCurrentDate();
43 90
44 - var testDayPostList = await dayPostListModel.findOne({date: '2022-05-30'}); 91 + var testDayPostList = await dayPostListModel.findOne({ date: today });
45 - if(testDayPostList == null){ 92 + if (testDayPostList == null) {
46 - testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] }); 93 + testDayPostList = new dayPostListModel({ date: today, idArray: [] });
47 isFirst = true; 94 isFirst = true;
48 } 95 }
49 var postListArr = testDayPostList.idArray; 96 var postListArr = testDayPostList.idArray;
50 - 97 + var newPost = new postModel({ date: today, title: req.body.title, content: req.body.content, password: req.body.password });
51 - var newPost = new postModel({ date: '2022-05-30', title: '테스트 제목', age: '테스트 내용', password: 'password' });
52 var newPostData = await newPost.save(); 98 var newPostData = await newPost.save();
53 postListArr.push(newPostData._id.toString()); 99 postListArr.push(newPostData._id.toString());
54 100
55 - if(isFirst) await testDayPostList.save(); 101 + if (isFirst) await testDayPostList.save();
56 - else await dayPostListModel.updateOne({date: '2022-05-30'},{idArray: postListArr}); 102 + else await dayPostListModel.updateOne({ date: today }, { idArray: postListArr });
57 103
58 - res.send("test"); 104 + res.send(newPostData);
59 }); 105 });
60 106
107 +// 게시물 수정, 삭제 추가예정 ---------------------------------------------------------------------------------------------------------------------------------------
108 +
109 +// router.get('/api/testSave', async (req, res) => {
110 +// var isFirst = false;
111 +
112 +// var testDayPostList = await dayPostListModel.findOne({ date: '2022-05-30' });
113 +// if (testDayPostList == null) {
114 +// testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] });
115 +// isFirst = true;
116 +// }
117 +// var postListArr = testDayPostList.idArray;
118 +
119 +// var newPost = new postModel({ date: '2022-05-30', title: '테스트 제목', content: '테스트 내용', password: 'password' });
120 +// var newPostData = await newPost.save();
121 +// postListArr.push(newPostData._id.toString());
122 +
123 +// if (isFirst) await testDayPostList.save();
124 +// else await dayPostListModel.updateOne({ date: '2022-05-30' }, { idArray: postListArr });
125 +
126 +// res.send("test");
127 +// });
128 +
61 module.exports = router; 129 module.exports = router;
62 130
......
1 const express = require('express'); 1 const express = require('express');
2 const app = express(); 2 const app = express();
3 const api = require('./Router/api'); 3 const api = require('./Router/api');
4 +let bodyParser = require('body-parser');
5 +app.use(bodyParser.urlencoded({ extended: false }));
6 +app.use(bodyParser.json());
4 7
5 app.use('/', api); 8 app.use('/', api);
6 9
......