개발일지
[NestJS] 댓글 좋아요 및 취소 본문
앞서 했었던 투표 글 좋아요 및 취소 구현과 구현 방식이 같으므로 설명은 생략하겠습니다.
modeling
model VoteComments {
id Int @id @default(autoincrement())
likedUsers Users[] @relation("LikedVoteUsers")
...
}
model Users {
id Int @id @default(autoincrement())
likedVoteComments VoteComments[] @relation("LikedVoteCommentUsers")
}
controller
@Post(':commentId/like')
async likeVoteComment(@Param('commentId', ParseIntPipe) commentId: number) {
const userId = 1;
const data: LikesVoteCommentDto = {
commentId,
userId,
};
await this.commentsService.likeVoteComment(data);
}
@Post(':commentId/cancle/likes')
async cancleLikedVoteComment(
@Param('voteId', ParseIntPipe) commentId: number,
) {
const userId = 1;
const data: LikesVoteCommentDto = {
commentId,
userId,
};
await this.commentsService.cancleLikedVoteComment(data);
}
service
async likeVoteComment(data: LikesVoteCommentDto) {
await this.commentsRepository.createLikedUser(data);
}
async cancleLikedVoteComment(data: LikesVoteCommentDto) {
await this.commentsRepository.deleteLikedUser(data);
}
repository
async createLikedUser(data: LikesVoteCommentDto) {
return await this.prisma.voteComments.update({
where: {
id: data.commentId,
},
data: {
likedUsers: {
connect: {
id: data.userId,
},
},
},
});
}
async deleteLikedUser(data: LikesVoteCommentDto) {
return await this.prisma.voteComments.update({
where: {
id: data.commentId,
},
data: {
likedUsers: {
disconnect: {
id: data.userId,
},
},
},
});
}
'NestJS, Node.js > #01 Project - 투표 커뮤니티' 카테고리의 다른 글
[NestJS] 마이페이지 - 프로필, 작성한 투표글, 댓글 조회 (0) | 2022.11.09 |
---|---|
[NestJS] 댓글 조회, 수정, 삭제 구현 (0) | 2022.11.09 |
[NestJS] 댓글 작성 구현 (0) | 2022.11.09 |
[NestJS] 투표 글 수정 및 삭제 구현 - Utility Types (0) | 2022.11.09 |
[NestJS] 투표 글 조회 구현 (0) | 2022.11.07 |