Skip to content

Commit

Permalink
feat(#103): History API 작성
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zyanlog authored Feb 18, 2024
2 parents 61062af + a027623 commit a19ea8e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
36 changes: 36 additions & 0 deletions src/controller/historyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
Expand All @@ -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));
}
};
Expand Down Expand Up @@ -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));
}
};
Expand Down Expand Up @@ -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));
}
};
Expand Down Expand Up @@ -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));
}
};
21 changes: 17 additions & 4 deletions src/controller/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
Expand Down
6 changes: 4 additions & 2 deletions src/router/historyRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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); // 헌혈 증서 이미지 텍스트 추출
historyRouter.post("/image", authenticateUser, uploadSome, imageToText); // 헌혈 증서 이미지 텍스트 추출
historyRouter.get("/date", authenticateUser, getAvailableDate); // 전혈 가능 날짜 조회

0 comments on commit a19ea8e

Please sign in to comment.