-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π BugFix/#175 - feat: like μμ± μ λμμ± λ¬Έμ ν΄κ²°
- Loading branch information
1 parent
321ebea
commit 2cb2210
Showing
8 changed files
with
138 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/daon/onjung/suggestion/application/controller/consumer/LikeV1Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.daon.onjung.suggestion.application.controller.consumer; | ||
|
||
import com.daon.onjung.account.domain.User; | ||
import com.daon.onjung.account.repository.mysql.UserRepository; | ||
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.domain.Board; | ||
import com.daon.onjung.suggestion.domain.Like; | ||
import com.daon.onjung.suggestion.domain.service.BoardService; | ||
import com.daon.onjung.suggestion.domain.service.LikeService; | ||
import com.daon.onjung.suggestion.repository.mysql.BoardRepository; | ||
import com.daon.onjung.suggestion.repository.mysql.LikeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hibernate.dialect.lock.OptimisticEntityLockException; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LikeV1Consumer { | ||
|
||
private final LikeRepository likeRepository; | ||
private final BoardRepository boardRepository; | ||
private final UserRepository userRepository; | ||
|
||
private final LikeService likeService; | ||
private final BoardService boardService; | ||
|
||
@Transactional | ||
@RabbitListener(queues = "like-queue") | ||
public void processLikeMessage(LikeMessage likeMessage) { | ||
try { | ||
|
||
// κ²μκΈ μ‘°ν | ||
Board board = boardRepository.findById(likeMessage.boardId()) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
// μ μ μ‘°ν | ||
User user = userRepository.findById(likeMessage.userId()) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
// μ’μμκ° μ‘΄μ¬νλμ§ νμΈ | ||
Like like = likeRepository.findByBoardAndUser(board, user) | ||
.orElse(null); | ||
|
||
if (like != null) { | ||
|
||
// μ’μμκ° μ΄λ―Έ μ‘΄μ¬νλ©΄ μμ | ||
likeRepository.delete(like); | ||
// κ²μκΈ μ’μμ μ κ°μ | ||
board = boardService.subtractLikeCount(board); | ||
boardRepository.save(board); | ||
} else { | ||
|
||
// μ’μμκ° μ‘΄μ¬νμ§ μμΌλ©΄ μμ± | ||
likeRepository.save(likeService.createLike(user, board)); | ||
|
||
// κ²μκΈ μ’μμ μ μ¦κ° | ||
board = boardService.addLikeCount(board); | ||
boardRepository.save(board); | ||
} | ||
} catch (OptimisticEntityLockException e) { | ||
throw new CommonException(ErrorCode.OPTIMISTIC_EXCEPTION); | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/daon/onjung/suggestion/application/controller/producer/LikeV1Producer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.daon.onjung.suggestion.application.controller.producer; | ||
|
||
import com.daon.onjung.suggestion.application.dto.request.LikeMessage; | ||
import com.daon.onjung.suggestion.application.usecase.SendLikeRequestUseCase; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LikeV1Producer { | ||
|
||
private final SendLikeRequestUseCase sendLikeRequestUseCase; | ||
|
||
public void sendLike (LikeMessage likeMessage) { | ||
sendLikeRequestUseCase.execute(likeMessage); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/daon/onjung/suggestion/application/dto/request/LikeMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.daon.onjung.suggestion.application.dto.request; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.UUID; | ||
|
||
@Builder | ||
public record LikeMessage( | ||
Long boardId, | ||
UUID userId | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/daon/onjung/suggestion/application/service/SendLikeRequestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.daon.onjung.suggestion.application.service; | ||
|
||
import com.daon.onjung.suggestion.application.dto.request.LikeMessage; | ||
import com.daon.onjung.suggestion.application.usecase.SendLikeRequestUseCase; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SendLikeRequestService implements SendLikeRequestUseCase { | ||
|
||
private final RabbitTemplate rabbitTemplate; | ||
|
||
@Override | ||
public void execute(LikeMessage likeMessage) { | ||
rabbitTemplate.convertAndSend("like-queue", likeMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/daon/onjung/suggestion/application/usecase/SendLikeRequestUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.daon.onjung.suggestion.application.usecase; | ||
|
||
import com.daon.onjung.core.annotation.bean.UseCase; | ||
import com.daon.onjung.suggestion.application.dto.request.LikeMessage; | ||
|
||
@UseCase | ||
public interface SendLikeRequestUseCase { | ||
void execute(LikeMessage likeMessage); | ||
} |