Skip to content

Commit

Permalink
🔨 Refactor/#177 - RabbitMQ의 RPC 요청-응답 패턴을 사용하여 좋아요 생성 시, ResponseBody…
Browse files Browse the repository at this point in the history
… 반환하게 변경
  • Loading branch information
dongkyeomjang committed Nov 27, 2024
1 parent 28164ab commit c362a1e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.daon.onjung.suggestion.application.dto.request.CreateBoardRequestDto;
import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto;
import com.daon.onjung.suggestion.application.dto.response.CreateCommentResponseDto;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;
import com.daon.onjung.suggestion.application.usecase.CreateBoardUseCase;
import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase;
import com.daon.onjung.suggestion.application.usecase.CreateOrDeleteLikeUseCase;
Expand Down Expand Up @@ -44,11 +45,10 @@ public ResponseDto<CreateCommentResponseDto> createComment(
}

@PutMapping("/api/v1/boards/{id}/likes")
public ResponseDto<Void> likeBoard(
public ResponseDto<CreateOrDeleteLikeResponseDto> likeBoard(
@AccountID UUID accountId,
@PathVariable Long id
) {
createOrDeleteLikeUseCase.execute(accountId, id);
return ResponseDto.ok(null);
return ResponseDto.ok(createOrDeleteLikeUseCase.execute(accountId, id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.daon.onjung.suggestion.repository.mysql.CommentRepository;
import lombok.RequiredArgsConstructor;
import org.hibernate.dialect.lock.OptimisticEntityLockException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -35,7 +34,7 @@ public class CommentV1Consumer {

@Transactional
@RabbitListener(queues = "comment-queue-1")
public CreateCommentResponseDto processCommentMessage1(CommentMessage commentMessage, Message amqpMessage) {
public CreateCommentResponseDto processCommentMessage1(CommentMessage commentMessage) {
try {

// 게시글 조회
Expand Down Expand Up @@ -66,7 +65,7 @@ public CreateCommentResponseDto processCommentMessage1(CommentMessage commentMes

@Transactional
@RabbitListener(queues = "comment-queue-2")
public CreateCommentResponseDto processCommentMessage2(CommentMessage commentMessage, Message amqpMessage) {
public CreateCommentResponseDto processCommentMessage2(CommentMessage commentMessage) {
try {

// 게시글 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.daon.onjung.core.exception.error.ErrorCode;
import com.daon.onjung.core.exception.type.CommonException;
import com.daon.onjung.suggestion.application.dto.request.LikeMessage;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;
import com.daon.onjung.suggestion.domain.Board;
import com.daon.onjung.suggestion.domain.Like;
import com.daon.onjung.suggestion.domain.service.BoardService;
Expand All @@ -30,7 +31,7 @@ public class LikeV1Consumer {

@Transactional
@RabbitListener(queues = "like-queue")
public void processLikeMessage(LikeMessage likeMessage) {
public Boolean processLikeMessage(LikeMessage likeMessage) {
try {

// 게시글 조회
Expand All @@ -52,6 +53,7 @@ public void processLikeMessage(LikeMessage likeMessage) {
// 게시글 좋아요 수 감소
board = boardService.subtractLikeCount(board);
boardRepository.save(board);
return false;
} else {

// 좋아요가 존재하지 않으면 생성
Expand All @@ -60,6 +62,7 @@ public void processLikeMessage(LikeMessage likeMessage) {
// 게시글 좋아요 수 증가
board = boardService.addLikeCount(board);
boardRepository.save(board);
return true;
}
} catch (OptimisticEntityLockException e) {
throw new CommonException(ErrorCode.OPTIMISTIC_EXCEPTION);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.daon.onjung.suggestion.application.controller.producer;

import com.daon.onjung.suggestion.application.dto.request.LikeMessage;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;
import com.daon.onjung.suggestion.application.usecase.SendLikeRequestUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -11,8 +12,8 @@ public class LikeV1Producer {

private final SendLikeRequestUseCase sendLikeRequestUseCase;

public void sendLike (LikeMessage likeMessage) {
sendLikeRequestUseCase.execute(likeMessage);
public CreateOrDeleteLikeResponseDto sendLike (LikeMessage likeMessage) {
return sendLikeRequestUseCase.execute(likeMessage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.daon.onjung.suggestion.application.dto.response;

import com.daon.onjung.core.dto.SelfValidating;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;

@Getter
public class CreateOrDeleteLikeResponseDto extends SelfValidating<CreateOrDeleteLikeResponseDto> {

@JsonProperty("is_like")
private final Boolean isLike;

@Builder
public CreateOrDeleteLikeResponseDto(Boolean isLike) {
this.isLike = isLike;

this.validateSelf();
}

public static CreateOrDeleteLikeResponseDto of(Boolean isLike) {
return CreateOrDeleteLikeResponseDto.builder()
.isLike(isLike)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.daon.onjung.suggestion.application.controller.producer.LikeV1Producer;
import com.daon.onjung.suggestion.application.dto.request.LikeMessage;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;
import com.daon.onjung.suggestion.application.usecase.CreateOrDeleteLikeUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -17,9 +18,9 @@ public class CreateOrDeleteLikeService implements CreateOrDeleteLikeUseCase {

@Override
@Transactional
public void execute(UUID accountId, Long boardId) {
public CreateOrDeleteLikeResponseDto execute(UUID accountId, Long boardId) {

likeProducer.sendLike(LikeMessage.builder()
return likeProducer.sendLike(LikeMessage.builder()
.boardId(boardId)
.userId(accountId)
.build());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.daon.onjung.suggestion.application.service;

import com.daon.onjung.suggestion.application.dto.request.LikeMessage;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;
import com.daon.onjung.suggestion.application.usecase.SendLikeRequestUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
Expand All @@ -13,7 +14,9 @@ public class SendLikeRequestService implements SendLikeRequestUseCase {
private final RabbitTemplate rabbitTemplate;

@Override
public void execute(LikeMessage likeMessage) {
rabbitTemplate.convertAndSend("like-queue", likeMessage);
public CreateOrDeleteLikeResponseDto execute(LikeMessage likeMessage) {

Boolean response = (Boolean) rabbitTemplate.convertSendAndReceive("like-queue", likeMessage);
return CreateOrDeleteLikeResponseDto.of(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.daon.onjung.suggestion.application.usecase;

import com.daon.onjung.core.annotation.bean.UseCase;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;

import java.util.UUID;

@UseCase
public interface CreateOrDeleteLikeUseCase {

void execute(UUID accountId, Long boardId);
CreateOrDeleteLikeResponseDto execute(UUID accountId, Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.daon.onjung.core.annotation.bean.UseCase;
import com.daon.onjung.suggestion.application.dto.request.LikeMessage;
import com.daon.onjung.suggestion.application.dto.response.CreateOrDeleteLikeResponseDto;

@UseCase
public interface SendLikeRequestUseCase {
void execute(LikeMessage likeMessage);
CreateOrDeleteLikeResponseDto execute(LikeMessage likeMessage);
}

0 comments on commit c362a1e

Please sign in to comment.