diff --git a/src/controller/historyController.js b/src/controller/historyController.js index 71ce186..657393f 100644 --- a/src/controller/historyController.js +++ b/src/controller/historyController.js @@ -111,6 +111,7 @@ export const getAllHistories = async(req, res, next) => { return res.send(response(status.SUCCESS, result)); } catch (err) { + console.log(err); return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); } }; @@ -136,6 +137,7 @@ export const getHistory = async(req, res, next) => { return res.send(response(status.SUCCESS, history)); } catch (err) { + console.log(err); return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); } }; @@ -204,6 +206,7 @@ export const updateHistory = async(req, res, next) => { return res.send(response(status.SUCCESS, updatedHistory)); } catch (err) { + console.log(err); return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); } }; @@ -260,6 +263,7 @@ export const deleteHistory = async(req, res, next) => { return res.send(response(status.SUCCESS, "헌혈 정보를 삭제했습니다.")); } catch (err) { + console.log(err); return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); } }; @@ -355,4 +359,36 @@ export const imageToText = async(req, res, next) => { console.log(err); return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); } +}; + +/* + * API No. 7 + * API Name : 전혈 가능 날짜 조회 + * [GET] /history/date + */ +export const getAvailableDate = async(req, res, next) => { + const { _id, email } = req.user; + + try { + // 최신 헌혈 정보 조회 + const lastDonation = await History.findOne({ user: _id, type: 'WB' }).sort({ created_at: -1 }); + + if (!lastDonation) { + return res.send(errResponse(status.HISTORY_NOT_FOUND)); + } + + // 전혈 헌혈 가능 날짜 계산 + const nextDonation = new Date(lastDonation.created_at); + nextDonation.setDate(nextDonation.getDate() + 56); + + // 디데이로 표시 + const today = new Date(); + const timeDifference = nextDonation - today; + const d_day = Math.ceil(timeDifference / (1000 * 60 * 60 * 24)); + + return res.send(response(status.SUCCESS, d_day)); + } catch (err) { + console.log(err); + return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); + } }; \ No newline at end of file diff --git a/src/controller/postController.js b/src/controller/postController.js index 70e5a44..07bc233 100644 --- a/src/controller/postController.js +++ b/src/controller/postController.js @@ -45,24 +45,27 @@ export const getGalleryPosting = async (req, res, next) => { try { const posttype = req.query.posttype; // [FREE, HONOR, INFO, CERTIFY] const sorttype = req.query.sorttype; // [created_at, likes] - const bestPost = await Post.find({ types: posttype, likes: { $gte: 10 }, status: true }, { writer: true, title: true, image: true, content: true, created_at: true }) .sort({ likes: -1 }).limit(3); // 공감이 10개 이상인 게시글 중 상위 3개를 선택 + + const postCount = await Post.countDocuments({ types: posttype, status: true }); + let totalPage = Math.ceil(postCount / perPage); // 페이지 추가 + if (sorttype === 'created_at') { // 최신순 정렬 const post = await Post.find({ types: posttype, status: true }, { writer: true, title: true, image: true, content: true, created_at: true }).sort({ created_at: -1 }).skip((page - 1) * perPage).limit(perPage); // sorttype const Page = [bestPost, post]; - return res.send(response(status.SUCCESS, Page)); + return res.send(response(status.SUCCESS, { totalPage, Page })); } else { // 공감순 정렬 const post = await Post.find({ types: posttype, status: true }, { writer: true, title: true, image: true, content: true, created_at: true }).sort({ likes: -1 }).skip((page - 1) * perPage).limit(perPage); // sorttype const Page = [bestPost, post]; - return res.send(response(status.SUCCESS, Page)); + return res.send(response(status.SUCCESS, { totalPage, Page })); } } catch ( error ) { return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); @@ -72,9 +75,19 @@ export const getGalleryPosting = async (req, res, next) => { // 게시글 상세조회와 게시글 조회수 증가, 해당 게시글에 작성된 댓글도 같이 가져오기 export const viewPost = async (req, res, next) => { try { + const { _id, email } = req.user; const postId = req.params.id; const post = await Post.findOneAndUpdate({ _id: postId }, { $inc: { watch_count: +1 } }, { new: true }); - return res.send(response(status.SUCCESS, post)); + let userLiked + + if (post.like_users.includes(_id)) { // 이미 좋아요를 누른 사람인 경우 + let userLiked = true; + return res.send(response(status.SUCCESS, { userLiked, post })); + } + else { + let userLiked = false; + return res.send(response(status.SUCCESS, {userLiked, post })); + } } catch ( error ) { return res.send(errResponse(status.INTERNAL_SERVER_ERROR)); } diff --git a/src/router/historyRouter.js b/src/router/historyRouter.js index 4b9f070..59611ec 100644 --- a/src/router/historyRouter.js +++ b/src/router/historyRouter.js @@ -6,6 +6,7 @@ import { updateHistory, deleteHistory, imageToText, + getAvailableDate, } from "../controller/historyController.js"; import { authenticateUser } from "../middleware/authMiddleware.js"; import { uploadSome } from "../middleware/imageMiddleware.js"; @@ -14,7 +15,8 @@ export const historyRouter = express.Router(); historyRouter.post("", authenticateUser, postHistory); // 헌혈 증서 등록 historyRouter.get("", authenticateUser, getAllHistories); // 헌혈 정보 전체 조회 -historyRouter.get("/:historyId", authenticateUser, getHistory); // 헌혈 정보 상세 조회 +historyRouter.get("/detail/:historyId", authenticateUser, getHistory); // 헌혈 정보 상세 조회 historyRouter.patch("/:historyId", authenticateUser, updateHistory); // 헌혈 정보 수정 historyRouter.delete("/:historyId", authenticateUser, deleteHistory); // 헌혈 정보 삭제 -historyRouter.post("/image", authenticateUser, uploadSome, imageToText); // 헌혈 증서 이미지 텍스트 추출 \ No newline at end of file +historyRouter.post("/image", authenticateUser, uploadSome, imageToText); // 헌혈 증서 이미지 텍스트 추출 +historyRouter.get("/date", authenticateUser, getAvailableDate); // 전혈 가능 날짜 조회 \ No newline at end of file