Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ public CommentLikeIdResponse createCommentLike(Long commentId, Long userId) {
if(commentLikeRepository.existsByCommentIdAndUserId(commentId, userId)){
throw new BadRequestException(ErrorCode.COMMENT_LIKE_NOT_FOUND);
}
return new CommentLikeIdResponse(commentLikeRepository.save(CommentLike.create(commentId, userId)).getId());

return new CommentLikeIdResponse(
commentLikeRepository.save(CommentLike.create(commentId, userId)).getId(),
commentLikeRepository.countByCommentId(commentId)
);
}

public void deleteCommentLike(Long commentLikeId, Long userId) {
public CommentLikeIdResponse deleteCommentLike(Long commentId, Long commentLikeId, Long userId) {
CommentLike commentLike = commentLikeRepository.findById(commentLikeId)
.orElseThrow(() -> new BadRequestException(ErrorCode.COMMENT_LIKE_NOT_FOUND));
if(!commentLike.getUserId().equals(userId)){
throw new BadRequestException(ErrorCode.NOT_COMMENT_LIKE_AUTHOR);
}
commentLikeRepository.delete(commentLike);
return new CommentLikeIdResponse(
null,
commentLikeRepository.countByCommentId(commentId)
);
}

public void deleteCommentLikeByCommentId(Long commentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CommentLikeIdResponse createCommentLike(Long commentId, Long userId) {
}

@Transactional
public void deleteCommentLike(Long commentLikeId, Long userId) {
commentLikeCommandService.deleteCommentLike(commentLikeId, userId);
public CommentLikeIdResponse deleteCommentLike(Long commentId, Long commentLikeId, Long userId) {
return commentLikeCommandService.deleteCommentLike(commentId, commentLikeId, userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ SELECT cl.commentId AS commentId, COUNT(cl) AS likeCount
WHERE cl.commentId = :commentId
""")
void deleteByCommentId(@Param("commentId") Long commentId);

Integer countByCommentId(Long commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public ResponseEntity<CommentLikeIdResponse> createCommentLike(
return ResponseEntity.ok(commentLikeService.createCommentLike(commentId, userInfo.userId()));
}

@DeleteMapping("/{commentLikeId}")
public ResponseEntity<Void> deleteCommentLike(
@DeleteMapping("/{commentId}/{commentLikeId}")
public ResponseEntity<CommentLikeIdResponse> deleteCommentLike(
@PathVariable("commentId") Long commentId,
@PathVariable("commentLikeId") Long commentLikeId,
@AuthenticationPrincipal UserInfo userInfo
) {
commentLikeService.deleteCommentLike(commentLikeId, userInfo.userId());
return ResponseEntity.noContent().build();
return ResponseEntity.ok(commentLikeService.deleteCommentLike(commentId, commentLikeId, userInfo.userId()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.chooz.commentLike.presentation.dto;

import software.amazon.awssdk.services.s3.endpoints.internal.Value;

public record CommentLikeIdResponse (
Long commentLikeId
Long commentLikeId,
Integer likeCount
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void deleteCommentLike() {
CommentLike commentLike = createAndGetSavedCommentLike();

// when
commentLikeService.deleteCommentLike(commentLike.getId(), commentLike.getUserId());
commentLikeService.deleteCommentLike(commentLike.getCommentId(),commentLike.getId(), commentLike.getUserId());

// then
assertThat(commentLikeRepository.existsById(commentLike.getId())).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class CommentLikeControllerTest extends RestDocsTest {
@DisplayName("댓글 좋아요 생성")
void createCommentLike() throws Exception {
//given
CommentLikeIdResponse commentLikeIdResponse = new CommentLikeIdResponse(commentLikeId);
CommentLikeIdResponse commentLikeIdResponse =
new CommentLikeIdResponse(commentLikeId, 11);
given(commentLikeService.createCommentLike(commentId, userId))
.willReturn(commentLikeIdResponse);

Expand All @@ -42,6 +43,9 @@ void createCommentLike() throws Exception {
pathParameters(parameterWithName("commentId").description("댓글 ID")),
responseFields(
fieldWithPath("commentLikeId")
.type(JsonFieldType.NUMBER)
.description("댓글좋아요 ID"),
fieldWithPath("likeCount")
.type(JsonFieldType.NUMBER)
.description("댓글좋아요 ID")
)
Expand All @@ -52,16 +56,27 @@ void createCommentLike() throws Exception {
@WithMockUserInfo
@DisplayName("댓글 좋아요 삭제")
void deleteCommentLike() throws Exception {
//when
doNothing().when(commentLikeService).deleteCommentLike(commentLikeId, userId);
//given
mockMvc.perform(delete("/comment-likes/{commentLikeId}", commentLikeId)
CommentLikeIdResponse commentLikeIdResponse =
new CommentLikeIdResponse(commentLikeId, 10);
given(commentLikeService.deleteCommentLike(commentId, commentLikeId, userId))
.willReturn(commentLikeIdResponse);
//when then
mockMvc.perform(delete("/comment-likes/{commentId}/{commentLikeId}", commentId, commentLikeId)
.header(HttpHeaders.AUTHORIZATION, "Bearer token"))
.andExpect(status().isNoContent())
.andExpect(status().isOk())
.andDo(restDocs.document(
requestHeaders(authorizationHeader()),
pathParameters(
parameterWithName("commentLikeId").description("댓글 좋아요 ID")
parameterWithName("commentId").description("댓글 ID"),
parameterWithName("commentLikeId").description("댓글 좋아요 ID")),
responseFields(
fieldWithPath("commentLikeId")
.type(JsonFieldType.NUMBER)
.description("댓글좋아요 ID"),
fieldWithPath("likeCount")
.type(JsonFieldType.NUMBER)
.description("댓글좋아요 ID")
)
));
}
Expand Down