최은석

finish api about post board

{
"scripts": {
"server": "cd server && nodemon server",
"client": "cd client && npm start --port",
"client": "cd client && export PORT=8080 && set PORT=8080 && npm start --port",
"start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\""
},
"dependencies": {
......
......@@ -29,7 +29,7 @@ const postModel = mongoose.model('post', post);
function getCurrentDate(originDate) {
var date;
if(originDate == null) date = new Date();
if (originDate == null) date = new Date();
else date = new Date(originDate);
var year = date.getFullYear().toString();
......@@ -39,72 +39,141 @@ function getCurrentDate(originDate) {
var day = date.getDate();
day = day < 10 ? '0' + day.toString() : day.toString();
return year + '-'+ month + '-'+ day ;
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);
});
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/: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/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/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/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.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/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.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);
}
catch (err) {
res.send(err.message);
}
});
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.get('/api/get/:id', async (req, res) => { // 특정 id의 게시물 불러오기
try {
const currentPost = await postModel.findById(req.params.id);
res.send({ title: currentPost.title, content: currentPost.content });
}
catch (err) {
res.send(err.message);
}
});
router.post('/api/postSave', async (req, res) => {
var isFirst = false;
const today = getCurrentDate();
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");
}
});
var testDayPostList = await dayPostListModel.findOne({ date: today });
if (testDayPostList == null) {
testDayPostList = new dayPostListModel({ date: today, idArray: [] });
isFirst = true;
router.post('/api/postSave', async (req, res) => { // 오늘 게시물 작성
try {
var isFirst = false;
const today = getCurrentDate();
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, 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");
}
var postListArr = testDayPostList.idArray;
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());
catch (err) {
res.send(err.message);
}
});
if (isFirst) await testDayPostList.save();
else await dayPostListModel.updateOne({ date: today }, { idArray: postListArr });
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);
}
});
res.send(newPostData);
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/testSave', async (req, res) => {
// var isFirst = false;
......