Skip to content

Commit

Permalink
feauture(#165) 10분 단위 조회수 증가 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
GoosMos committed Feb 18, 2024
1 parent a027623 commit a1b4c42
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/controller/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { status } from "../config/responseStatus.js";
import { customErrResponse, errResponse, response } from '../config/response.js';
import { deleteImage } from '../middleware/imageMiddleware.js';

const viewObj = new Object();


// 줄글 형식으로 게시글을 확인하기
export const getLinePosting = async (req, res, next) => {
Expand Down Expand Up @@ -77,16 +79,31 @@ 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 });
let userLiked
// const post = await Post.findOneAndUpdate({ _id: postId }, { $inc: { watch_count: +1 } }, { new: true });
const post = await Post.findById({ _id: postId });

if (!viewObj[postId]) viewObj[postId] = []; // 리스트를 생성
if (viewObj[postId].indexOf(_id) == -1) {
viewObj[postId].push(_id);
post.watch_count++;
await post.save();
setTimeout(() => {
viewObj[postId].splice(viewObj[postId].indexOf(_id), 1)
}, 600000);
}

for (let i in viewObj) {
if (i.length == 0) delete viewObj.i;
}


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 }));
return res.send(response(status.SUCCESS, { userLiked, post }));
}
} catch ( error ) {
return res.send(errResponse(status.INTERNAL_SERVER_ERROR));
Expand Down

0 comments on commit a1b4c42

Please sign in to comment.