최은석

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

......@@ -6,6 +6,7 @@
"": {
"dependencies": {
"axios": "^0.27.2",
"body-parser": "^1.20.0",
"concurrently": "^7.2.1",
"express": "^4.18.1",
"http-proxy-middleware": "^2.0.6",
......
......@@ -6,6 +6,7 @@
},
"dependencies": {
"axios": "^0.27.2",
"body-parser": "^1.20.0",
"concurrently": "^7.2.1",
"express": "^4.18.1",
"http-proxy-middleware": "^2.0.6",
......
......@@ -27,36 +27,104 @@ const postModel = mongoose.model('post', post);
// res.send({ test: "hi" });
// });
router.get('/api/getAll', (req, res) => {
dayPostListModel.find(function (error, dayPostLists) {
console.log('--- Read all ---');
if (error) {
res.send(error);
} else {
res.send(dayPostLists);
function getCurrentDate(originDate) {
var date;
if(originDate == null) date = new Date();
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 ;
}
router.get('/api/getList', async(req, res) => {
const today = getCurrentDate();
var testDayPostList = await dayPostListModel.findOne({ date: today });
if (testDayPostList == null) testDayPostList = new dayPostListModel({ date: today, idArray: [] });
res.send(testDayPostList.idArray);
});
router.get('/api/getList/:date', async(req, res) => {
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);
});
router.get('/api/get', async(req, res) => {
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.title = onePost.title;
tempJSON.content = onePost.content;
tempJSON.content = tempJSON.content.replace(/(?:\r\n|\r|\n)/g, '');
const sliceLength = 10;
if(tempJSON.content.length > sliceLength) tempJSON.content = tempJSON.content.slice(0,sliceLength) + "...";
resultArray.push(tempJSON);
}
})
res.send(resultArray);
});
router.get('/api/get/:id', async(req, res) => {
const currentPost = await postModel.findById(req.params.id);
res.send({ title: currentPost.title, content: currentPost.content });
});
router.get('/api/testSave', async(req, res) => {
router.post('/api/isPassEqual', async(req, res) => {
const currentPost = await postModel.findById(req.body.id);
if (currentPost.password == req.body.password) res.send("success");
else res.send("failed");
});
router.post('/api/postSave', async (req, res) => {
var isFirst = false;
const today = getCurrentDate();
var testDayPostList = await dayPostListModel.findOne({date: '2022-05-30'});
if(testDayPostList == null){
testDayPostList = new dayPostListModel({ date: '2022-05-30', idArray: [] });
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: '2022-05-30', title: '테스트 제목', age: '테스트 내용', password: 'password' });
var newPost = new postModel({ date: today, 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: '2022-05-30'},{idArray: postListArr});
if (isFirst) await testDayPostList.save();
else await dayPostListModel.updateOne({ date: today }, { idArray: postListArr });
res.send("test");
res.send(newPostData);
});
// 게시물 수정, 삭제 추가예정 ---------------------------------------------------------------------------------------------------------------------------------------
// 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;
......
const express = require('express');
const app = express();
const api = require('./Router/api');
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', api);
......