최은석

finish api about post board

1 { 1 {
2 "scripts": { 2 "scripts": {
3 "server": "cd server && nodemon server", 3 "server": "cd server && nodemon server",
4 - "client": "cd client && npm start --port", 4 + "client": "cd client && export PORT=8080 && set PORT=8080 && npm start --port",
5 "start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"" 5 "start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\""
6 }, 6 },
7 "dependencies": { 7 "dependencies": {
......
...@@ -29,7 +29,7 @@ const postModel = mongoose.model('post', post); ...@@ -29,7 +29,7 @@ const postModel = mongoose.model('post', post);
29 29
30 function getCurrentDate(originDate) { 30 function getCurrentDate(originDate) {
31 var date; 31 var date;
32 - if(originDate == null) date = new Date(); 32 + if (originDate == null) date = new Date();
33 else date = new Date(originDate); 33 else date = new Date(originDate);
34 var year = date.getFullYear().toString(); 34 var year = date.getFullYear().toString();
35 35
...@@ -39,72 +39,141 @@ function getCurrentDate(originDate) { ...@@ -39,72 +39,141 @@ function getCurrentDate(originDate) {
39 var day = date.getDate(); 39 var day = date.getDate();
40 day = day < 10 ? '0' + day.toString() : day.toString(); 40 day = day < 10 ? '0' + day.toString() : day.toString();
41 41
42 - return year + '-'+ month + '-'+ day ; 42 + return year + '-' + month + '-' + day;
43 } 43 }
44 44
45 -router.get('/api/getList', async(req, res) => { 45 +function arrayEquals(a, b) {
46 - const today = getCurrentDate(); 46 + return Array.isArray(a) &&
47 - var testDayPostList = await dayPostListModel.findOne({ date: today }); 47 + Array.isArray(b) &&
48 - if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] }); 48 + a.length === b.length &&
49 - res.send(testDayPostList.idArray); 49 + a.every((val, index) => val === b[index]);
50 -}); 50 +}
51 51
52 -router.get('/api/getList/:date', async(req, res) => { 52 +router.get('/api/getList', async (req, res) => { // 오늘 게시물들의 아이디 표시
53 - const today = getCurrentDate(req.params.date); 53 + try {
54 - var testDayPostList = await dayPostListModel.findOne({ date: today }); 54 + const today = getCurrentDate();
55 - if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] }); 55 + var testDayPostList = await dayPostListModel.findOne({ date: today });
56 - res.send(testDayPostList.idArray); 56 + if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
57 + res.send(testDayPostList.idArray);
58 + }
59 + catch (err) {
60 + res.send(err.message);
61 + }
57 }); 62 });
58 63
59 -router.get('/api/get', async(req, res) => { 64 +router.get('/api/getList/:date', async (req, res) => { // 특정 날자의 게시물들의 아이디 표시
60 - const idArray = req.body.idArray; 65 + try {
61 - var resultArray = []; 66 + const today = getCurrentDate(req.params.date);
62 - for (const id of idArray){ 67 + var testDayPostList = await dayPostListModel.findOne({ date: today });
63 - const onePost = await postModel.findById(id); 68 + if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
64 - var tempJSON = {}; 69 + res.send(testDayPostList.idArray);
65 - tempJSON.id = onePost.id; 70 + }
66 - tempJSON.title = onePost.title; 71 + catch (err) {
67 - tempJSON.content = onePost.content; 72 + res.send(err.message);
68 - tempJSON.content = tempJSON.content.replace(/(?:\r\n|\r|\n)/g, ''); 73 + }
69 - const sliceLength = 10;
70 - if(tempJSON.content.length > sliceLength) tempJSON.content = tempJSON.content.slice(0,sliceLength) + "...";
71 - resultArray.push(tempJSON);
72 - }
73 - res.send(resultArray);
74 }); 74 });
75 75
76 -router.get('/api/get/:id', async(req, res) => { 76 +router.get('/api/get', async (req, res) => { // 특정 id(여러개)의 게시물 내용 요약 불러오기
77 - const currentPost = await postModel.findById(req.params.id); 77 + try {
78 - res.send({ title: currentPost.title, content: currentPost.content }); 78 + const idArray = req.body.idArray;
79 + var resultArray = [];
80 + for (const id of idArray) {
81 + const onePost = await postModel.findById(id);
82 + var tempJSON = {};
83 + tempJSON.id = onePost.id;
84 + tempJSON.title = onePost.title;
85 + tempJSON.content = onePost.content;
86 + tempJSON.content = tempJSON.content.replace(/(?:\r\n|\r|\n)/g, '');
87 + const sliceLength = 10;
88 + if (tempJSON.content.length > sliceLength) tempJSON.content = tempJSON.content.slice(0, sliceLength) + "...";
89 + resultArray.push(tempJSON);
90 + }
91 + res.send(resultArray);
92 + }
93 + catch (err) {
94 + res.send(err.message);
95 + }
79 }); 96 });
80 97
81 -router.post('/api/isPassEqual', async(req, res) => { 98 +router.get('/api/get/:id', async (req, res) => { // 특정 id의 게시물 불러오기
82 - const currentPost = await postModel.findById(req.body.id); 99 + try {
83 - if (currentPost.password == req.body.password) res.send("success"); 100 + const currentPost = await postModel.findById(req.params.id);
84 - else res.send("failed"); 101 + res.send({ title: currentPost.title, content: currentPost.content });
102 + }
103 + catch (err) {
104 + res.send(err.message);
105 + }
85 }); 106 });
86 107
87 -router.post('/api/postSave', async (req, res) => { 108 +router.post('/api/isPassEqual', async (req, res) => { // 암호가 같으면 success, 아니면 failed
88 - var isFirst = false; 109 + try {
89 - const today = getCurrentDate(); 110 + const currentPost = await postModel.findById(req.body.id);
111 + if (currentPost.password == req.body.password) res.send("success");
112 + else res.send("failed");
113 + }
114 + catch (err) {
115 + res.send("failed");
116 + }
117 +});
90 118
91 - var testDayPostList = await dayPostListModel.findOne({ date: today }); 119 +router.post('/api/postSave', async (req, res) => { // 오늘 게시물 작성
92 - if (testDayPostList == null) { 120 + try {
93 - testDayPostList = new dayPostListModel({ date: today, idArray: [] }); 121 + var isFirst = false;
94 - isFirst = true; 122 + const today = getCurrentDate();
123 +
124 + var testDayPostList = await dayPostListModel.findOne({ date: today });
125 + if (testDayPostList == null) {
126 + testDayPostList = new dayPostListModel({ date: today, idArray: [] });
127 + isFirst = true;
128 + }
129 + var postListArr = testDayPostList.idArray;
130 + var newPost = new postModel({ date: today, title: req.body.title, content: req.body.content, password: req.body.password });
131 + var newPostData = await newPost.save();
132 + postListArr.push(newPostData._id.toString());
133 +
134 + if (isFirst) await testDayPostList.save();
135 + else await dayPostListModel.updateOne({ date: today }, { idArray: postListArr });
136 +
137 + res.send("success");
95 } 138 }
96 - var postListArr = testDayPostList.idArray; 139 + catch (err) {
97 - var newPost = new postModel({ date: today, title: req.body.title, content: req.body.content, password: req.body.password }); 140 + res.send(err.message);
98 - var newPostData = await newPost.save(); 141 + }
99 - postListArr.push(newPostData._id.toString()); 142 +});
100 143
101 - if (isFirst) await testDayPostList.save(); 144 +router.put('/api/edit/:id', async (req, res) => { // 게시물 수정
102 - else await dayPostListModel.updateOne({ date: today }, { idArray: postListArr }); 145 + try {
146 + const id = req.params.id;
147 + const title = req.body.title;
148 + const content = req.body.content;
149 + await postModel.findByIdAndUpdate(id, { title: title, content: content });
150 + res.send("success");
151 + }
152 + catch (err) {
153 + res.send(err.message);
154 + }
155 +});
103 156
104 - res.send(newPostData); 157 +router.delete('/api/delete/:id', async (req, res) => { // 게시물 삭제
158 + try {
159 + const id = req.params.id;
160 + const list = await dayPostListModel.find();
161 + for (const dayList of list) {
162 + var newArray = dayList.idArray.filter((data)=>{return data != id;})
163 + if(!arrayEquals(dayList.idArray, newArray)){
164 + await dayPostListModel.findByIdAndUpdate(dayList._id.toString(), {idArray: newArray});
165 + }
166 + }
167 + await postModel.findByIdAndDelete(id);
168 + res.send("success");
169 + }
170 + catch (err) {
171 + res.send(err.message);
172 + }
105 }); 173 });
106 174
107 -// 게시물 저장에 성공 실패 메시지만 표시, 게시물 수정, 삭제 추가예정 --------------------------------------------------------------------------------------------------------------------------------------- 175 +// 대기시간 관련 디비 수정 부분 추가 ----------------------------------------------------------------------------------------------------------------------------------------------------------------
176 +// 학식 일주일치 불러오는 부분 추가 -----------------------------------------------------------------------------------------------------------------------------------------------------------------
108 177
109 // router.get('/api/testSave', async (req, res) => { 178 // router.get('/api/testSave', async (req, res) => {
110 // var isFirst = false; 179 // var isFirst = false;
......