Skip to content

Commit

Permalink
refactor : 게시글 이모지와 댓글 좋아요 누르기/취소하기 API 응답 DTO 반환하도록 수정 (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
mingmingmon authored Dec 20, 2024
1 parent ba19038 commit 3dd26a8
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,11 +23,11 @@ public class BoardEmojiToggleController {
@Operation(summary = "[U] 커뮤니티 게시글 이모지 누르기/취소하기", description = "ROLE_USER 이상의 권한이 필요함")
@PreAuthorize("hasRole('USER')")
@PostMapping("/{boardId}/react/{emoji}")
public ApiResponse<String> toggleEmojiStatus(
public ApiResponse<BoardEmojiToggleResponseDto> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
* 게시글의 이모지 상태를 토글합니다.
Expand All @@ -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("지원하지 않는 이모지입니다.");
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,10 +23,10 @@ public class CommentLikeToggleController {
@Operation(summary = "[U] 댓글 좋아요 누르기/취소하기", description = "ROLE_USER 이상의 권한이 필요함")
@PreAuthorize("hasRole('USER')")
@PostMapping("/likes/{commentId}")
public ApiResponse<Long> toggleLikeStatus(
public ApiResponse<CommentLikeToggleResponseDto> toggleLikeStatus(
@PathVariable(name = "commentId") Long commentId
) {
Long id = toggleCommentLikeUseCase.toggleLikeStatus(commentId);
return ApiResponse.success(id);
CommentLikeToggleResponseDto responseDto = toggleCommentLikeUseCase.toggleLikeStatus(commentId);
return ApiResponse.success(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
* 댓글의 좋아요 상태를 토글합니다.
Expand All @@ -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);
});
}
}

0 comments on commit 3dd26a8

Please sign in to comment.