Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diary #26

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/controllers/boardController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const {response} = require('../config/response.js');
const status = require('../config/responseStatus.js');
const { getBoardType, getBoardId, countLike, countAllLike, getAllComment} = require('../providers/boardProvider.js');
const { getAllBoardType, getBoardType, getBoardId, countLike, countAllLike, getAllComment} = require('../providers/boardProvider.js');
const { writeBoard, modifyBoard, eraseBoard, postLikeUp, deleteLike, addScrape, subScrape, writeComment, modifyComment, eraseComment, postLikeComment, deleteLikeComment } = require('../services/boardService.js')


//게시판
const getAllType = async(req,res) => {
try{
res.send(response(status.SUCCESS, await getAllBoardType(req.query.offset)))
}catch (error) {res.send(response(status.INTERNAL_SERVER_ERROR,{}))}
}
const getBoard = async(req,res) => {
try{
const boardTypeData = await getBoardType(req.params.boardType, req.query.offset);
Expand Down Expand Up @@ -108,4 +113,4 @@ const likeDownComment = async(req, res) => {
res.send(response(status.SUCCESS, await deleteLikeComment(req.params.commentId, req.user_id) ))
}catch (error) {res.send(response(status.INTERNAL_SERVER_ERROR,{}))}
}
module.exports = { getBoard, getOneBoard, postBoard, putBoard, deleteBoard, likeUp, likeDown, getLike, getAllLikeBoard, postScrape, cancelScrape, getComment, postComment, putComment, deleteComment, likeUpComment, likeDownComment };
module.exports = { getAllType, getBoard, getOneBoard, postBoard, putBoard, deleteBoard, likeUp, likeDown, getLike, getAllLikeBoard, postScrape, cancelScrape, getComment, postComment, putComment, deleteComment, likeUpComment, likeDownComment };
139 changes: 47 additions & 92 deletions src/controllers/diaryController.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,39 @@
const { response } = require("../config/response.js");
const status = require("../config/responseStatus.js");
const {
writeDiary,
modifyDiary,
eraseDiary,
diaryLikeAdd,
diaryLikeSub,
} = require("../services/diaryService.js");
const {
privateDiary,
publicDiary,
OneDiary,
} = require("../providers/diaryProvider.js");
const {response} = require('../config/response.js');
const status = require('../config/responseStatus.js');
const { writeDiary, modifyDiary, eraseDiary, diaryLikeAdd, diaryLikeSub } = require('../services/diaryService.js')
const { privateDiary, publicDiary, ImageList, OneDiary } = require('../providers/diaryProvider.js');

const getPrivateDiary = async(req,res) => {
try{
res.send(response(status.SUCCESS, await privateDiary(req.user_id, req.query.offset)));
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}//param 추가 필요(date)
}
const getPublicDiary = async(req,res) => {
try{
res.send(response(status.SUCCESS, await publicDiary(req.query.offset)));
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}
const getImage = async(req,res) => {
try{
res.send(response(status.SUCCESS, await ImageList(req.user_id, req.params.year, req.params.month)));
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}
const getOneDiary = async(req,res) => {
try{
res.send(response(status.SUCCESS, await OneDiary(req.user_id, req.params.diaryDate))); // diaryId -> date
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}
const postDiary = async(req,res) => {
try{
res.send(response(status.SUCCESS, await writeDiary(req.params.diaryDate, req.user_id, req.body)));
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}
const putDiary = async(req,res) => {
try{
res.send(response(status.SUCCESS, await modifyDiary(req.params.diaryDate, req.body))); //diaryId -> date
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}

const getPrivateDiary = async (req, res) => {
res.send(
response(status.SUCCESS, await privateDiary(req.user_id, req.query.offset))
);
};
const getPublicDiary = async (req, res) => {
res.send(response(status.SUCCESS, await publicDiary(req.query.offset)));
};
const getOneDiary = async (req, res) => {
try {
res.send(
response(
status.SUCCESS,
await OneDiary(req.user_id, req.params.diaryDate)
)
); // diaryId -> date
} catch (error) {
res.send(response(status.ARTICLE_NOT_FOUND, {}));
}
};
const postDiary = async (req, res) => {
try {
res.send(
response(
status.SUCCESS,
await writeDiary(req.params.diaryDate, req.user_id, req.body)
)
);
} catch (error) {
res.send(response(status.ARTICLE_NOT_FOUND, {}));
}
};
const putDiary = async (req, res) => {
try {
res.send(
response(
status.SUCCESS,
await modifyDiary(req.params.diaryDate, req.body)
)
); //diaryId -> date
} catch (error) {
res.send(response(status.ARTICLE_NOT_FOUND, {}));
}
};

const deleteDiary = async (req, res) => {
try {
Expand All @@ -66,37 +43,15 @@ const deleteDiary = async (req, res) => {
}
};

const diaryLikeUp = async (req, res) => {
try {
res.send(
response(
status.SUCCESS,
await diaryLikeAdd(req.params.diaryId, req.user_id)
)
);
} catch (error) {
res.send(response(status.ARTICLE_NOT_FOUND, {}));
}
};
const diaryLikeDown = async (req, res) => {
try {
res.send(
response(
status.SUCCESS,
await diaryLikeSub(req.params.diaryId, req.user_id)
)
);
} catch (error) {
res.send(response(status.ARTICLE_NOT_FOUND, {}));
}
};
module.exports = {
getPrivateDiary,
getPublicDiary,
getOneDiary,
postDiary,
putDiary,
deleteDiary,
diaryLikeUp,
diaryLikeDown,
};
const diaryLikeUp = async(req,res) => {
try{
res.send(response(status.SUCCESS, await diaryLikeAdd(req.params.diaryId, req.user_id)));
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}
const diaryLikeDown =async(req,res) => {
try{
res.send(response(status.SUCCESS, await diaryLikeSub(req.params.diaryId, req.user_id)));
}catch (error) {res.send(response(status.ARTICLE_NOT_FOUND,{}))}
}

module.exports = { getPrivateDiary, getPublicDiary, getImage, getOneDiary, postDiary, putDiary, deleteDiary, diaryLikeUp, diaryLikeDown}
2 changes: 1 addition & 1 deletion src/controllers/mypageController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const response = require('../config/response.js');
const {response} = require('../config/response.js');
const status = require('../config/responseStatus.js');
const { MyBoard, MyComment, MyScrape, MyPage } = require('../providers/mypageProvider.js');
const Profile = require('../services/mypageService.js')
Expand Down
23 changes: 22 additions & 1 deletion src/dtos/diaryDTO.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,25 @@ const oneDiaryDTO = (data) => {

return diaries
}
module.exports = {diaryDTO, oneDiaryDTO};

const imageDTO = (data) => {
const lastdate = data[0];
const images = data[1];

const dates = [];
for (let i = 1; i <= lastdate; i++) {
dates.push(i);
}

const mappedObjects = [];
dates.forEach(date => {
const matchingImage = images.find(image => image.diary_date === date);
if (matchingImage) {
mappedObjects.push({ diary_date: date, image_url: matchingImage.image_url });
} else {
mappedObjects.push({ diary_date: date, image_url: null });
}
});
return mappedObjects
}
module.exports = {diaryDTO, oneDiaryDTO, imageDTO};
21 changes: 19 additions & 2 deletions src/models/boardDAO.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
const {response} = require('../config/response.js');
const status = require('../config/responseStatus.js');
const pool = require('../config/database.js');
const { getAllData, getOneData, getBoardImage, getAllDataLike, getAllDataComment ,getAllDataScrape, oneBoardImage, getOneDataLike, getOneDataComment, getOneDataScrape, compareUser, insertData, searchData, changeData, deleteData, existBoard, insertLike, deleteLike, countLike, getAllLike, insertScrape, deleteScrape, getCommentData, insertComment, changeComment, deleteCommentData, boardComment, insertCommentLike, deleteCommentLike, countCommentLike, getAllCommentDataLike } = require('./boardSQL.js');
const { getNoTypeData, getAllData, getOneData, getBoardImage, getAllDataLike, getAllDataComment ,getAllDataScrape, oneBoardImage, getOneDataLike, getOneDataComment, getOneDataScrape, compareUser, insertData, searchData, changeData, deleteData, existBoard, insertLike, deleteLike, countLike, getAllLike, insertScrape, deleteScrape, getCommentData, insertComment, changeComment, deleteCommentData, boardComment, insertCommentLike, deleteCommentLike, countCommentLike, getAllCommentDataLike } = require('./boardSQL.js');

//게시판
const getAllBoardData = async(data) => {
try {
const conn = await pool.getConnection();
const allData = await pool.query(getNoTypeData, parseInt(data.offset));
const allData1 = await pool.query(getAllDataLike);
const allData2 = await pool.query(getAllDataComment);
const allData3 = await pool.query(getAllDataScrape);
const allData4 = await pool.query(getBoardImage);
conn.release();

resultBoard = [allData[0], allData1[0], allData2[0], allData3[0], allData4[0]];

return resultBoard;
}
catch (err) { throw response(status.INTERNAL_SERVER_ERROR,{}); }
}

const getBoardData = async(data) => {
try {
const conn = await pool.getConnection();
Expand Down Expand Up @@ -242,4 +259,4 @@ const countLikeCommentData = async(data) => {
catch (err) { throw response(status.INTERNAL_SERVER_ERROR,{});}
}

module.exports = { getBoardData, getOneBoardData, writeBoardData, returnWriteBoardData, modifyBoardData, eraseBoardData, postLikeData, deleteLikeData, countLikeData, allLikeData, addScrapeData, subScrapeData, allCommentData, writeCommentData, modifyCommentData, eraseCommentData, postLikeCommentData, deleteLikeCommentData, countLikeCommentData };
module.exports = { getAllBoardData, getBoardData, getOneBoardData, writeBoardData, returnWriteBoardData, modifyBoardData, eraseBoardData, postLikeData, deleteLikeData, countLikeData, allLikeData, addScrapeData, subScrapeData, allCommentData, writeCommentData, modifyCommentData, eraseCommentData, postLikeCommentData, deleteLikeCommentData, countLikeCommentData };
4 changes: 3 additions & 1 deletion src/models/boardSQL.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const getNoTypeData = "SELECT * FROM board ORDER BY created_at DESC LIMIT ?, 10;"

const getAllData = "SELECT * FROM board WHERE board_type = ? ORDER BY created_at DESC LIMIT ?, 10;";

const getBoardImage = "SELECT board_id, image_url FROM image_board ORDER BY board_id;"
Expand Down Expand Up @@ -60,4 +62,4 @@ const countCommentLike = "SELECT COUNT(*) FROM like_comment WHERE comment_id = ?

const getAllCommentDataLike = "SELECT comment_id, COUNT(*) AS likecount FROM like_comment GROUP BY comment_id;"

module.exports = {getAllData, getOneData, getBoardImage, getAllDataLike, getAllDataComment, getAllDataScrape, oneBoardImage, getOneDataLike, getOneDataComment, getOneDataScrape, compareUser, insertData, searchData, changeData, deleteData, existBoard, insertLike, deleteLike, countLike, getAllLike, insertScrape, deleteScrape, getCommentData, insertComment, changeComment, deleteCommentData, boardComment, insertCommentLike, deleteCommentLike, countCommentLike, getAllCommentDataLike };
module.exports = {getNoTypeData, getAllData, getOneData, getBoardImage, getAllDataLike, getAllDataComment, getAllDataScrape, oneBoardImage, getOneDataLike, getOneDataComment, getOneDataScrape, compareUser, insertData, searchData, changeData, deleteData, existBoard, insertLike, deleteLike, countLike, getAllLike, insertScrape, deleteScrape, getCommentData, insertComment, changeComment, deleteCommentData, boardComment, insertCommentLike, deleteCommentLike, countCommentLike, getAllCommentDataLike };
26 changes: 20 additions & 6 deletions src/models/diaryDAO.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const response = require('../config/response.js');
const {response} = require('../config/response.js');
const status = require('../config/responseStatus.js');
const pool = require('../config/database.js');
const { getPrivate, getPublic, getDiaryImage, oneDiary,searchDiaryId, oneDiaryImage, compareDiaryUser, oneDiaryLike, getDiaryLike, insertDiary, changeDiary, deleteDiaryData, countDiary, insertDiaryLike, deleteDiaryLike } = require('../models/diarySQL.js');
const { getPrivate, getPublic, getDiaryImage, getImageList, oneDiary,searchDiaryId, oneDiaryImage, compareDiaryUser, oneDiaryLike, getDiaryLike, insertDiary, changeDiary, deleteDiaryData, countDiary, insertDiaryLike, deleteDiaryLike } = require('../models/diarySQL.js');

const privateDiaryData = async(data) => {
try {
Expand Down Expand Up @@ -32,6 +32,22 @@ const publicDiaryData = async(data) => {
catch (err) { throw response(status.INTERNAL_SERVER_ERROR,{});}
}

const ImageListData = async(data) => {
try {
const conn = await pool.getConnection();
const diaryImageList = await pool.query(getImageList, [data.user_id, data.diary_year, data.diary_month]);
conn.release();

const getDaysInMonth = (year, month) => { return new Date(year, month, 0).getDate()};
year = data.diary_year;
month = data.diary_month;
const daylist = getDaysInMonth(year,month)
return [daylist, diaryImageList[0]]

}
catch (err) { throw response(status.INTERNAL_SERVER_ERROR,{});}
}

const oneDiaryData = async(data) => {
try {
const conn = await pool.getConnection();
Expand All @@ -42,15 +58,13 @@ const oneDiaryData = async(data) => {
const DiaryLike = await pool.query(oneDiaryLike, diaryId.diary_id);
const DiaryImage = await pool.query(oneDiaryImage, diaryId.diary_id);
const btnAdd = await pool.query(compareDiaryUser, diaryId.diary_id);
await console.log(btnAdd)
//await console.log(btnAdd)
conn.release();

if(btnAdd[0][0].user_id == data.user_id){ //user_id와 작성자가 같으면 true값돌려줘서 버튼 보이게
console.log('a')
return [DiaryData[0][0], DiaryLike[0], DiaryImage[0], true];
}
else{
console.log('a')
return [DiaryData[0][0], DiaryLike[0], DiaryImage[0], false];
}
}
Expand Down Expand Up @@ -130,4 +144,4 @@ const subLikeData = async(data) => {
}


module.exports = { privateDiaryData, publicDiaryData, oneDiaryData, writeDiaryData, modifyDiaryData, eraseDiaryData, countDiaryLike, addLikeData, subLikeData}
module.exports = { privateDiaryData, publicDiaryData, ImageListData, oneDiaryData, writeDiaryData, modifyDiaryData, eraseDiaryData, countDiaryLike, addLikeData, subLikeData}
4 changes: 3 additions & 1 deletion src/models/diarySQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const getDiaryLike = "SELECT diary_id, COUNT(*) AS count FROM like_diary GROUP B

const getDiaryImage = "SELECT diary_id, image_url FROM image_diary ORDER BY diary_id;"

const getImageList = "SELECT DAYOFMONTH(d.diary_date) AS diary_date, i.image_url FROM diary d JOIN image_diary i ON d.diary_id = i.diary_id WHERE d.diary_id IN (SELECT diary_id FROM diary WHERE user_id = ? AND YEAR(diary_date)=? AND MONTH(diary_date) = ?);"

const oneDiary = "SELECT * FROM diary WHERE diary_date = ?"

const searchDiaryId = "SELECT diary_id FROM diary WHERE diary_date = ?";
Expand All @@ -31,4 +33,4 @@ const insertDiaryLike = "INSERT INTO like_diary (user_id, diary_id, created_at)

const deleteDiaryLike = "DELETE FROM like_diary WHERE user_id = ? AND diary_id = ? ;"

module.exports = { getPrivate, getPublic, getDiaryLike, getDiaryImage, oneDiary, searchDiaryId, oneDiaryImage, compareDiaryUser, oneDiaryLike, insertDiary, changeDiary, deleteDiaryData, countDiary, insertDiaryLike, deleteDiaryLike}
module.exports = { getPrivate, getPublic, getDiaryLike, getImageList, getDiaryImage, oneDiary, searchDiaryId, oneDiaryImage, compareDiaryUser, oneDiaryLike, insertDiary, changeDiary, deleteDiaryData, countDiary, insertDiaryLike, deleteDiaryLike}
2 changes: 1 addition & 1 deletion src/models/mypageDAO.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const response = require('../config/response.js');
const {response} = require('../config/response.js');
const status = require('../config/responseStatus.js');
const pool = require('../config/database.js');
const { selectBoard, selectComment, selectScrape, countBoard, countComment, countScrape, changeProfile } = require('../models/mypageSQL.js');
Expand Down
13 changes: 11 additions & 2 deletions src/providers/boardProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const { getBoardData, getOneBoardData, countLikeData, allLikeData, allCommentData } = require('../models/boardDAO.js')
const { getAllBoardData, getBoardData, getOneBoardData, countLikeData, allLikeData, allCommentData } = require('../models/boardDAO.js')
const { boardDTO, oneBoardDTO, boardCommentDTO } = require("../dtos/boardDTO.js");

//게시판 가져오기
const getAllBoardType = async(query) => {
try{
allboardresult = await getAllBoardData({
"offset" : query
})
if (allboardresult == -1){throw console.log('error');}
else{ return await boardDTO(allboardresult);}
}catch (error) { throw error; }};

const getBoardType = async(param, query) => {
try{
boardresult = await getBoardData({
Expand Down Expand Up @@ -48,4 +57,4 @@ const getAllComment = async(param,query) => {
else{return await boardCommentDTO(comments);}
}catch (error) { throw error; }
};
module.exports = {getBoardType, getBoardId, countLike, countAllLike, getAllComment};
module.exports = {getAllBoardType, getBoardType, getBoardId, countLike, countAllLike, getAllComment};
17 changes: 14 additions & 3 deletions src/providers/diaryProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { diaryDTO, oneDiaryDTO } = require('../dtos/diaryDTO.js');
const { privateDiaryData, publicDiaryData, oneDiaryData } = require('../models/diaryDAO.js')
const { diaryDTO, oneDiaryDTO, imageDTO } = require('../dtos/diaryDTO.js');
const { privateDiaryData, publicDiaryData, ImageListData, oneDiaryData } = require('../models/diaryDAO.js')
const response = require('../config/response.js');
const status = require('../config/responseStatus.js');

Expand All @@ -24,6 +24,17 @@ const publicDiary = async(query) => {
return await diaryDTO(resultpublicDiary);
}
}
const ImageList = async(user, year, month) => {
try{
resultImageList = await ImageListData({
"user_id" : user,
"diary_year" : year,
"diary_month" : month
});
if (resultImageList == -1){throw console.log('error');}
else{return await imageDTO(resultImageList);}
}catch (error) { throw error;}
}

const OneDiary = async(user, param) => {
try{
Expand All @@ -36,4 +47,4 @@ const OneDiary = async(user, param) => {
}catch (error) { throw error;}
}

module.exports = { privateDiary, publicDiary, OneDiary }
module.exports = { privateDiary, publicDiary, ImageList, OneDiary }
Loading