Skip to content

Commit 7ea253a

Browse files
committed
feat: 좋아요 로직 수정
1 parent 6f27e06 commit 7ea253a

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/post-like/post-like.service.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,31 @@ export class PostLikeService {
8585
throw DataNotFoundException('게시글을 찾을 수 없습니다.');
8686
}
8787

88-
const likeData = await this.postLikeRepository.find({
89-
where: { post: { id: postId }, status: 'activated' },
88+
const likeData = await this.postLikeRepository.findOne({
89+
where: {
90+
post: { id: postId },
91+
user: { id: userId },
92+
},
9093
relations: ['user', 'post'],
9194
});
9295

93-
const existingLike = likeData.filter((like) => like.user.id === userId);
96+
const allLikesData = await this.postLikeRepository.find({
97+
where: { post: { id: postId }, status: 'activated' },
98+
});
9499

95-
if (existingLike.length > 0) {
100+
if (likeData) {
96101
// 좋아요 actiavated인 경우 -> 좋아요 취소
97102
// 좋아요 deactivated인 경우 -> 다시 좋아요 누름
98-
existingLike[0].status =
99-
existingLike[0].status === 'deactivated' ? 'activated' : 'deactivated';
100-
existingLike[0].updatedAt = new Date();
101-
await this.postLikeRepository.save(existingLike);
103+
likeData.status =
104+
likeData.status === 'deactivated' ? 'activated' : 'deactivated';
105+
await this.postLikeRepository.save(likeData);
102106
return {
103-
id: existingLike[0].post.id,
104-
isPostLike: existingLike[0].status === 'activated',
107+
id: likeData.post.id,
108+
isPostLike: likeData.status === 'activated',
105109
postLikesCount:
106-
existingLike[0].status === 'activated'
107-
? likeData.length + 1
108-
: likeData.length - 1,
110+
likeData.status === 'activated'
111+
? allLikesData.length + 1
112+
: allLikesData.length - 1,
109113
};
110114
} else {
111115
// 좋아요 생성 (처음 좋아요 눌림)
@@ -118,7 +122,7 @@ export class PostLikeService {
118122
return {
119123
id: newLike.post.id,
120124
isPostLike: newLike.status === 'activated',
121-
postLikesCount: likeData.length + 1,
125+
postLikesCount: allLikesData.length + 1,
122126
};
123127
}
124128
}

0 commit comments

Comments
 (0)