From 3dd26a83c3c21ee590e876927b95cf3e4e258deb Mon Sep 17 00:00:00 2001 From: JEON MIN JU <96719969+mingmingmon@users.noreply.github.com> Date: Fri, 20 Dec 2024 22:19:04 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=9D=B4=EB=AA=A8=EC=A7=80=EC=99=80=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EB=88=84=EB=A5=B4=EA=B8=B0/?= =?UTF-8?q?=EC=B7=A8=EC=86=8C=ED=95=98=EA=B8=B0=20API=20=EC=9D=91=EB=8B=B5?= =?UTF-8?q?=20DTO=20=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#635)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../in/web/BoardEmojiToggleController.java | 7 ++++--- .../application/dto/mapper/BoardDtoMapper.java | 1 + .../dto/mapper/BoardEmojiDtoMapper.java | 17 +++++++++++++++++ .../response/BoardEmojiToggleResponseDto.java | 13 +++++++++++++ .../port/in/ToggleBoardEmojiUseCase.java | 4 +++- .../service/BoardEmojiToggleService.java | 7 +++++-- .../in/web/CommentLikeToggleController.java | 7 ++++--- .../dto/mapper/CommentLikeDtoMapper.java | 16 ++++++++++++++++ .../response/CommentLikeToggleResponseDto.java | 15 +++++++++++++++ .../port/in/ToggleCommentLikeUseCase.java | 4 +++- .../service/CommentLikeToggleService.java | 12 +++++++++--- 11 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardEmojiDtoMapper.java create mode 100644 src/main/java/page/clab/api/domain/community/board/application/dto/response/BoardEmojiToggleResponseDto.java create mode 100644 src/main/java/page/clab/api/domain/community/comment/application/dto/mapper/CommentLikeDtoMapper.java create mode 100644 src/main/java/page/clab/api/domain/community/comment/application/dto/response/CommentLikeToggleResponseDto.java diff --git a/src/main/java/page/clab/api/domain/community/board/adapter/in/web/BoardEmojiToggleController.java b/src/main/java/page/clab/api/domain/community/board/adapter/in/web/BoardEmojiToggleController.java index 3d77c9ba6..4b402ffd1 100644 --- a/src/main/java/page/clab/api/domain/community/board/adapter/in/web/BoardEmojiToggleController.java +++ b/src/main/java/page/clab/api/domain/community/board/adapter/in/web/BoardEmojiToggleController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import page.clab.api.domain.community.board.application.dto.response.BoardEmojiToggleResponseDto; import page.clab.api.domain.community.board.application.port.in.ToggleBoardEmojiUseCase; import page.clab.api.global.common.dto.ApiResponse; @@ -22,11 +23,11 @@ public class BoardEmojiToggleController { @Operation(summary = "[U] 커뮤니티 게시글 이모지 누르기/취소하기", description = "ROLE_USER 이상의 권한이 필요함") @PreAuthorize("hasRole('USER')") @PostMapping("/{boardId}/react/{emoji}") - public ApiResponse toggleEmojiStatus( + public ApiResponse toggleEmojiStatus( @PathVariable(name = "boardId") Long boardId, @PathVariable(name = "emoji") String emoji ) { - String id = toggleBoardEmojiUseCase.toggleEmojiStatus(boardId, emoji); - return ApiResponse.success(id); + BoardEmojiToggleResponseDto responseDto = toggleBoardEmojiUseCase.toggleEmojiStatus(boardId, emoji); + return ApiResponse.success(responseDto); } } diff --git a/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardDtoMapper.java b/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardDtoMapper.java index 8ba304cd7..67f37ce0a 100644 --- a/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardDtoMapper.java +++ b/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardDtoMapper.java @@ -6,6 +6,7 @@ import page.clab.api.domain.community.board.application.dto.response.BoardCategoryResponseDto; import page.clab.api.domain.community.board.application.dto.response.BoardDetailsResponseDto; import page.clab.api.domain.community.board.application.dto.response.BoardEmojiCountResponseDto; +import page.clab.api.domain.community.board.application.dto.response.BoardEmojiToggleResponseDto; import page.clab.api.domain.community.board.application.dto.response.BoardListResponseDto; import page.clab.api.domain.community.board.application.dto.response.BoardMyResponseDto; import page.clab.api.domain.community.board.application.dto.response.WriterInfo; diff --git a/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardEmojiDtoMapper.java b/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardEmojiDtoMapper.java new file mode 100644 index 000000000..be2fb81dd --- /dev/null +++ b/src/main/java/page/clab/api/domain/community/board/application/dto/mapper/BoardEmojiDtoMapper.java @@ -0,0 +1,17 @@ +package page.clab.api.domain.community.board.application.dto.mapper; + +import org.springframework.stereotype.Component; +import page.clab.api.domain.community.board.application.dto.response.BoardEmojiToggleResponseDto; +import page.clab.api.domain.community.board.domain.BoardEmoji; + +@Component +public class BoardEmojiDtoMapper { + + public BoardEmojiToggleResponseDto toDto(BoardEmoji boardEmoji) { + return BoardEmojiToggleResponseDto.builder() + .boardId(boardEmoji.getBoardId()) + .emoji(boardEmoji.getEmoji()) + .isDeleted(boardEmoji.getIsDeleted()) + .build(); + } +} diff --git a/src/main/java/page/clab/api/domain/community/board/application/dto/response/BoardEmojiToggleResponseDto.java b/src/main/java/page/clab/api/domain/community/board/application/dto/response/BoardEmojiToggleResponseDto.java new file mode 100644 index 000000000..5ac044ac8 --- /dev/null +++ b/src/main/java/page/clab/api/domain/community/board/application/dto/response/BoardEmojiToggleResponseDto.java @@ -0,0 +1,13 @@ +package page.clab.api.domain.community.board.application.dto.response; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class BoardEmojiToggleResponseDto { + + private Long boardId; + private String emoji; + private Boolean isDeleted; +} diff --git a/src/main/java/page/clab/api/domain/community/board/application/port/in/ToggleBoardEmojiUseCase.java b/src/main/java/page/clab/api/domain/community/board/application/port/in/ToggleBoardEmojiUseCase.java index 84ad15d47..a082738e2 100644 --- a/src/main/java/page/clab/api/domain/community/board/application/port/in/ToggleBoardEmojiUseCase.java +++ b/src/main/java/page/clab/api/domain/community/board/application/port/in/ToggleBoardEmojiUseCase.java @@ -1,5 +1,7 @@ package page.clab.api.domain.community.board.application.port.in; +import page.clab.api.domain.community.board.application.dto.response.BoardEmojiToggleResponseDto; + public interface ToggleBoardEmojiUseCase { - String toggleEmojiStatus(Long boardId, String emoji); + BoardEmojiToggleResponseDto toggleEmojiStatus(Long boardId, String emoji); } diff --git a/src/main/java/page/clab/api/domain/community/board/application/service/BoardEmojiToggleService.java b/src/main/java/page/clab/api/domain/community/board/application/service/BoardEmojiToggleService.java index fbd38c661..59fcae1a7 100644 --- a/src/main/java/page/clab/api/domain/community/board/application/service/BoardEmojiToggleService.java +++ b/src/main/java/page/clab/api/domain/community/board/application/service/BoardEmojiToggleService.java @@ -3,6 +3,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import page.clab.api.domain.community.board.application.dto.mapper.BoardEmojiDtoMapper; +import page.clab.api.domain.community.board.application.dto.response.BoardEmojiToggleResponseDto; import page.clab.api.domain.community.board.application.port.in.ToggleBoardEmojiUseCase; import page.clab.api.domain.community.board.application.port.out.RegisterBoardEmojiPort; import page.clab.api.domain.community.board.application.port.out.RetrieveBoardEmojiPort; @@ -22,6 +24,7 @@ public class BoardEmojiToggleService implements ToggleBoardEmojiUseCase { private final RetrieveBoardEmojiPort retrieveBoardEmojiPort; private final RegisterBoardEmojiPort registerBoardEmojiPort; private final ExternalRetrieveMemberUseCase externalRetrieveMemberUseCase; + private final BoardEmojiDtoMapper mapper; /** * 게시글의 이모지 상태를 토글합니다. @@ -36,7 +39,7 @@ public class BoardEmojiToggleService implements ToggleBoardEmojiUseCase { */ @Transactional @Override - public String toggleEmojiStatus(Long boardId, String emoji) { + public BoardEmojiToggleResponseDto toggleEmojiStatus(Long boardId, String emoji) { if (!EmojiUtils.isEmoji(emoji)) { throw new InvalidEmojiException("지원하지 않는 이모지입니다."); } @@ -50,6 +53,6 @@ public String toggleEmojiStatus(Long boardId, String emoji) { }) .orElseGet(() -> BoardEmoji.create(memberId, boardId, emoji)); registerBoardEmojiPort.save(boardEmoji); - return board.getCategory().getKey(); + return mapper.toDto(boardEmoji); } } diff --git a/src/main/java/page/clab/api/domain/community/comment/adapter/in/web/CommentLikeToggleController.java b/src/main/java/page/clab/api/domain/community/comment/adapter/in/web/CommentLikeToggleController.java index a6ac318c9..0f2f47597 100644 --- a/src/main/java/page/clab/api/domain/community/comment/adapter/in/web/CommentLikeToggleController.java +++ b/src/main/java/page/clab/api/domain/community/comment/adapter/in/web/CommentLikeToggleController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import page.clab.api.domain.community.comment.application.dto.response.CommentLikeToggleResponseDto; import page.clab.api.domain.community.comment.application.port.in.ToggleCommentLikeUseCase; import page.clab.api.global.common.dto.ApiResponse; @@ -22,10 +23,10 @@ public class CommentLikeToggleController { @Operation(summary = "[U] 댓글 좋아요 누르기/취소하기", description = "ROLE_USER 이상의 권한이 필요함") @PreAuthorize("hasRole('USER')") @PostMapping("/likes/{commentId}") - public ApiResponse toggleLikeStatus( + public ApiResponse toggleLikeStatus( @PathVariable(name = "commentId") Long commentId ) { - Long id = toggleCommentLikeUseCase.toggleLikeStatus(commentId); - return ApiResponse.success(id); + CommentLikeToggleResponseDto responseDto = toggleCommentLikeUseCase.toggleLikeStatus(commentId); + return ApiResponse.success(responseDto); } } diff --git a/src/main/java/page/clab/api/domain/community/comment/application/dto/mapper/CommentLikeDtoMapper.java b/src/main/java/page/clab/api/domain/community/comment/application/dto/mapper/CommentLikeDtoMapper.java new file mode 100644 index 000000000..872568c56 --- /dev/null +++ b/src/main/java/page/clab/api/domain/community/comment/application/dto/mapper/CommentLikeDtoMapper.java @@ -0,0 +1,16 @@ +package page.clab.api.domain.community.comment.application.dto.mapper; + +import org.springframework.stereotype.Component; +import page.clab.api.domain.community.comment.application.dto.response.CommentLikeToggleResponseDto; + +@Component +public class CommentLikeDtoMapper { + + public CommentLikeToggleResponseDto of(Long boardId, Long commentLikes, Boolean isDeleted) { + return CommentLikeToggleResponseDto.builder() + .boardId(boardId) + .likes(commentLikes) + .isDeleted(isDeleted) + .build(); + } +} diff --git a/src/main/java/page/clab/api/domain/community/comment/application/dto/response/CommentLikeToggleResponseDto.java b/src/main/java/page/clab/api/domain/community/comment/application/dto/response/CommentLikeToggleResponseDto.java new file mode 100644 index 000000000..a7317773f --- /dev/null +++ b/src/main/java/page/clab/api/domain/community/comment/application/dto/response/CommentLikeToggleResponseDto.java @@ -0,0 +1,15 @@ +package page.clab.api.domain.community.comment.application.dto.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Builder +public class CommentLikeToggleResponseDto { + + private Long boardId; + private Long likes; + private Boolean isDeleted; +} diff --git a/src/main/java/page/clab/api/domain/community/comment/application/port/in/ToggleCommentLikeUseCase.java b/src/main/java/page/clab/api/domain/community/comment/application/port/in/ToggleCommentLikeUseCase.java index 387c2e94b..0a5f3792e 100644 --- a/src/main/java/page/clab/api/domain/community/comment/application/port/in/ToggleCommentLikeUseCase.java +++ b/src/main/java/page/clab/api/domain/community/comment/application/port/in/ToggleCommentLikeUseCase.java @@ -1,5 +1,7 @@ package page.clab.api.domain.community.comment.application.port.in; +import page.clab.api.domain.community.comment.application.dto.response.CommentLikeToggleResponseDto; + public interface ToggleCommentLikeUseCase { - Long toggleLikeStatus(Long commentId); + CommentLikeToggleResponseDto toggleLikeStatus(Long commentId); } diff --git a/src/main/java/page/clab/api/domain/community/comment/application/service/CommentLikeToggleService.java b/src/main/java/page/clab/api/domain/community/comment/application/service/CommentLikeToggleService.java index 58f1877ee..07280ad5c 100644 --- a/src/main/java/page/clab/api/domain/community/comment/application/service/CommentLikeToggleService.java +++ b/src/main/java/page/clab/api/domain/community/comment/application/service/CommentLikeToggleService.java @@ -3,6 +3,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import page.clab.api.domain.community.comment.application.dto.mapper.CommentLikeDtoMapper; +import page.clab.api.domain.community.comment.application.dto.response.CommentLikeToggleResponseDto; import page.clab.api.domain.community.comment.application.port.in.ToggleCommentLikeUseCase; import page.clab.api.domain.community.comment.application.port.out.RegisterCommentLikePort; import page.clab.api.domain.community.comment.application.port.out.RemoveCommentLikePort; @@ -23,6 +25,7 @@ public class CommentLikeToggleService implements ToggleCommentLikeUseCase { private final ExternalRetrieveCommentUseCase externalRetrieveCommentUseCase; private final ExternalRegisterCommentUseCase externalRegisterCommentUseCase; private final ExternalRetrieveMemberUseCase externalRetrieveMemberUseCase; + private final CommentLikeDtoMapper mapper; /** * 댓글의 좋아요 상태를 토글합니다. @@ -36,20 +39,23 @@ public class CommentLikeToggleService implements ToggleCommentLikeUseCase { */ @Transactional @Override - public Long toggleLikeStatus(Long commentId) { + public CommentLikeToggleResponseDto toggleLikeStatus(Long commentId) { String currentMemberId = externalRetrieveMemberUseCase.getCurrentMemberId(); Comment comment = externalRetrieveCommentUseCase.getById(commentId); + Long boardId = comment.getBoardId(); return retrieveCommentLikePort.findByCommentIdAndMemberId(comment.getId(), currentMemberId) .map(commentLike -> { removeCommentLikePort.delete(commentLike); comment.decrementLikes(); - return externalRegisterCommentUseCase.save(comment).getLikes(); + externalRegisterCommentUseCase.save(comment); + return mapper.of(boardId, comment.getLikes(), true); }) .orElseGet(() -> { CommentLike newLike = CommentLike.create(currentMemberId, comment.getId()); registerCommentLikePort.save(newLike); comment.incrementLikes(); - return externalRegisterCommentUseCase.save(comment).getLikes(); + externalRegisterCommentUseCase.save(comment); + return mapper.of(boardId, comment.getLikes(), false); }); } }