From 8e163bc4497c44e6f8ad9802b00e0cfef2a3e834 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:16:33 +0900 Subject: [PATCH 01/17] =?UTF-8?q?[FEAT]=20ComponentCommentCountIncreaseEve?= =?UTF-8?q?nt=20=EC=B6=94=EA=B0=80=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/event/ComponentCommentCountIncreaseEvent.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java diff --git a/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java new file mode 100644 index 00000000..278d556b --- /dev/null +++ b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java @@ -0,0 +1,9 @@ +package ject.componote.domain.component.dto.event; + +import ject.componote.domain.comment.domain.Comment; + +public record ComponentCommentCountIncreaseEvent(Long componentId) { + public static ComponentCommentCountIncreaseEvent from(final Comment comment) { + return new ComponentCommentCountIncreaseEvent(comment.getComponentId()); + } +} From 41978143d46d4c2162331dc2adaec7748d961bc4 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:16:38 +0900 Subject: [PATCH 02/17] =?UTF-8?q?[FEAT]=20ComponentCommentCountDecreaseEve?= =?UTF-8?q?nt=20=EC=B6=94=EA=B0=80=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/event/ComponentCommentCountDecreaseEvent.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java diff --git a/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java new file mode 100644 index 00000000..2e8cc61e --- /dev/null +++ b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java @@ -0,0 +1,9 @@ +package ject.componote.domain.component.dto.event; + +import ject.componote.domain.comment.domain.Comment; + +public record ComponentCommentCountDecreaseEvent(Long componentId) { + public static ComponentCommentCountDecreaseEvent from(final Comment comment) { + return new ComponentCommentCountDecreaseEvent(comment.getComponentId()); + } +} From 084873a741816bbeb6712a075ca9b3c8a7ff0c85 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:16:58 +0900 Subject: [PATCH 03/17] =?UTF-8?q?[FEAT]=20Component=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=97=90=20=EB=8C=93=EA=B8=80=20=EC=88=98=20=EC=A6=9D?= =?UTF-8?q?=EA=B0=80/=EA=B0=90=EC=86=8C=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ject/componote/domain/component/domain/Component.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/ject/componote/domain/component/domain/Component.java b/src/main/java/ject/componote/domain/component/domain/Component.java index d98df419..74d6d31a 100644 --- a/src/main/java/ject/componote/domain/component/domain/Component.java +++ b/src/main/java/ject/componote/domain/component/domain/Component.java @@ -85,6 +85,14 @@ public void increaseViewCount() { this.viewCount.increase(); } + public void increaseCommentCount() { + this.commentCount.increase(); + } + + public void decreaseCommentCount() { + this.commentCount.decrease(); + } + private List parseMixedNames(final List mixedNames) { return mixedNames.stream() .map(MixedName::from) From b2a4464ee23ca308a964bfb63514e3355b6c2013 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:20:11 +0900 Subject: [PATCH 04/17] =?UTF-8?q?[FEAT]=20ComponentCommentCountEventListen?= =?UTF-8?q?er=20=EC=B6=94=EA=B0=80=20(#95)=20-=20=EC=BB=B4=ED=8F=AC?= =?UTF-8?q?=EB=84=8C=ED=8A=B8=20=EB=8C=93=EA=B8=80=20=EC=88=98=EB=A5=BC=20?= =?UTF-8?q?=EC=A6=9D=EA=B0=80/=EA=B0=90=EC=86=8C=20=EC=9D=B4=EB=B2=A4?= =?UTF-8?q?=ED=8A=B8=EB=A5=BC=20=EA=B5=AC=EB=8F=85=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=EB=84=88=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComponentCommentCountEventListener.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java diff --git a/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java b/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java new file mode 100644 index 00000000..3ce01737 --- /dev/null +++ b/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java @@ -0,0 +1,42 @@ +package ject.componote.domain.component.application; + +import ject.componote.domain.component.dao.ComponentRepository; +import ject.componote.domain.component.domain.Component; +import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; +import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; +import ject.componote.domain.component.error.NotFoundComponentException; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Async; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +@org.springframework.stereotype.Component +@RequiredArgsConstructor +public class ComponentCommentCountEventListener { + private final ComponentRepository componentRepository; + + @Async + @Transactional(propagation = Propagation.REQUIRES_NEW) + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void handleCommentCountIncreaseEvent(final ComponentCommentCountIncreaseEvent event) { + final Long componentId = event.componentId(); + final Component component = findComponentById(componentId); + component.increaseCommentCount(); + } + + @Async + @Transactional(propagation = Propagation.REQUIRES_NEW) + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void handleCommentCountDecreaseEvent(final ComponentCommentCountDecreaseEvent event) { + final Long componentId = event.componentId(); + final Component component = findComponentById(componentId); + component.decreaseCommentCount(); + } + + private Component findComponentById(final Long componentId) { + return componentRepository.findById(componentId) + .orElseThrow(() -> new NotFoundComponentException(componentId)); + } +} From 82cb46857a3e2f97334e558b24b0a37cafc8e5b8 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:20:23 +0900 Subject: [PATCH 05/17] =?UTF-8?q?[FEAT]=20CommentImageMoveEvent=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/dto/image/event/CommentImageMoveEvent.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java diff --git a/src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java b/src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java new file mode 100644 index 00000000..6838f5fb --- /dev/null +++ b/src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java @@ -0,0 +1,10 @@ +package ject.componote.domain.comment.dto.image.event; + +import ject.componote.domain.comment.domain.Comment; +import ject.componote.domain.comment.model.CommentImage; + +public record CommentImageMoveEvent(CommentImage image) { + public static CommentImageMoveEvent from(final Comment comment) { + return new CommentImageMoveEvent(comment.getImage()); + } +} From 15379398e36c0590822f95cc3e74467a3cceb0eb Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:20:42 +0900 Subject: [PATCH 06/17] =?UTF-8?q?[FEAT]=20CommentImageMoveEvent=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#95)=20-=20=EB=8C=93=EA=B8=80=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EA=B2=BD=EB=A1=9C=20=EC=9D=B4=EB=8F=99=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=EB=A5=BC=20=EA=B5=AC=EB=8F=85?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A6=AC=EC=8A=A4=EB=84=88=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommentImageEventListener.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java diff --git a/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java b/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java new file mode 100644 index 00000000..2b4cd7db --- /dev/null +++ b/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java @@ -0,0 +1,22 @@ +package ject.componote.domain.comment.application; + +import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; +import ject.componote.infra.storage.application.StorageProducer; +import ject.componote.infra.storage.dto.move.request.ImageMoveRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +@Component +@RequiredArgsConstructor +public class CommentImageEventListener { + private final StorageProducer storageProducer; + + @Async + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void handleImageMove(final CommentImageMoveEvent event) { + storageProducer.sendImageMoveMessage(ImageMoveRequest.from(event.image())); + } +} From 54803edd4dd7dfac97bc84c961856ed3a1a4c816 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:22:17 +0900 Subject: [PATCH 07/17] =?UTF-8?q?[REFACTOR]=20Component=20=EC=9D=B4?= =?UTF-8?q?=EB=B2=A4=ED=8A=B8=20=EA=B0=9D=EC=B2=B4=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EB=B3=80=EA=B2=BD=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/component/application/ComponentService.java | 2 +- .../component/application/ComponentViewCountEventHandler.java | 2 +- .../dto/{find => }/event/ComponentViewCountIncreaseEvent.java | 2 +- .../domain/component/application/ComponentServiceTest.java | 2 +- .../application/ComponentViewCountEventHandlerTest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/main/java/ject/componote/domain/component/dto/{find => }/event/ComponentViewCountIncreaseEvent.java (83%) diff --git a/src/main/java/ject/componote/domain/component/application/ComponentService.java b/src/main/java/ject/componote/domain/component/application/ComponentService.java index 1311412f..3cddc8f4 100644 --- a/src/main/java/ject/componote/domain/component/application/ComponentService.java +++ b/src/main/java/ject/componote/domain/component/application/ComponentService.java @@ -7,7 +7,7 @@ import ject.componote.domain.component.dao.ComponentSummaryDao; import ject.componote.domain.component.domain.Component; import ject.componote.domain.component.domain.ComponentType; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.dto.find.request.ComponentSearchRequest; import ject.componote.domain.component.dto.find.response.ComponentDetailResponse; import ject.componote.domain.component.dto.find.response.ComponentSummaryResponse; diff --git a/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java b/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java index 52169b03..1d88242b 100644 --- a/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java +++ b/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java @@ -2,7 +2,7 @@ import ject.componote.domain.component.dao.ComponentRepository; import ject.componote.domain.component.domain.Component; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.error.NotFoundComponentException; import lombok.RequiredArgsConstructor; import org.springframework.context.event.EventListener; diff --git a/src/main/java/ject/componote/domain/component/dto/find/event/ComponentViewCountIncreaseEvent.java b/src/main/java/ject/componote/domain/component/dto/event/ComponentViewCountIncreaseEvent.java similarity index 83% rename from src/main/java/ject/componote/domain/component/dto/find/event/ComponentViewCountIncreaseEvent.java rename to src/main/java/ject/componote/domain/component/dto/event/ComponentViewCountIncreaseEvent.java index 15315f7b..60cf2bca 100644 --- a/src/main/java/ject/componote/domain/component/dto/find/event/ComponentViewCountIncreaseEvent.java +++ b/src/main/java/ject/componote/domain/component/dto/event/ComponentViewCountIncreaseEvent.java @@ -1,4 +1,4 @@ -package ject.componote.domain.component.dto.find.event; +package ject.componote.domain.component.dto.event; import ject.componote.domain.component.domain.Component; diff --git a/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java b/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java index 0f0c1ff5..ccd0e400 100644 --- a/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java +++ b/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java @@ -7,7 +7,7 @@ import ject.componote.domain.component.dao.ComponentSummaryDao; import ject.componote.domain.component.domain.Component; import ject.componote.domain.component.domain.ComponentType; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.dto.find.request.ComponentSearchRequest; import ject.componote.domain.component.dto.find.response.ComponentDetailResponse; import ject.componote.domain.component.dto.find.response.ComponentSummaryResponse; diff --git a/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java b/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java index 861789d0..6381ff84 100644 --- a/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java +++ b/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java @@ -3,7 +3,7 @@ import ject.componote.domain.common.model.Count; import ject.componote.domain.component.dao.ComponentRepository; import ject.componote.domain.component.domain.Component; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.error.NotFoundComponentException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From c19db7c0f427ca2ac2853c7b2d674641ced88798 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:23:26 +0900 Subject: [PATCH 08/17] =?UTF-8?q?[REFACTOR]=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1/=EC=82=AD=EC=A0=9C=20=EC=8B=9C=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89=ED=95=A0=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95,=20=EB=8C=93=EA=B8=80=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=8B=9C=20ComponentId=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/application/CommentService.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/ject/componote/domain/comment/application/CommentService.java b/src/main/java/ject/componote/domain/comment/application/CommentService.java index 51c829a0..076457c2 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentService.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentService.java @@ -10,6 +10,7 @@ import ject.componote.domain.comment.dto.find.response.CommentFindByComponentResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByMemberResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByParentResponse; +import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyNotificationEvent; import ject.componote.domain.comment.dto.update.request.CommentUpdateRequest; @@ -19,6 +20,10 @@ import ject.componote.domain.comment.model.CommentImage; import ject.componote.domain.comment.validation.CommenterValidation; import ject.componote.domain.common.dto.response.PageResponse; +import ject.componote.domain.component.dao.ComponentRepository; +import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; +import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; +import ject.componote.domain.component.error.NotFoundComponentException; import ject.componote.infra.storage.application.StorageService; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; @@ -33,16 +38,18 @@ public class CommentService { private final ApplicationEventPublisher eventPublisher; private final CommentRepository commentRepository; + private final ComponentRepository componentRepository; private final StorageService storageService; @Transactional public CommentCreateResponse create(final AuthPrincipal authPrincipal, final CommentCreateRequest request) { + validateComponentId(request); validateParentId(request); - final Comment comment = commentRepository.save( - CommentCreationStrategy.createBy(request, authPrincipal.id()) - ); - storageService.moveImage(comment.getImage()); + final Comment comment = commentRepository.save(CommentCreationStrategy.createBy(request, authPrincipal.id())); + // 아래 코드 이벤트 계층을 분리하여 리펙토링 예정 + eventPublisher.publishEvent(CommentImageMoveEvent.from(comment)); + eventPublisher.publishEvent(ComponentCommentCountIncreaseEvent.from(comment)); if (isReply(request)) { eventPublisher.publishEvent(CommentReplyCountIncreaseEvent.from(comment)); eventPublisher.publishEvent(CommentReplyNotificationEvent.from(comment)); @@ -90,7 +97,17 @@ public void update(final AuthPrincipal authPrincipal, final Long commentId, fina @CommenterValidation @Transactional public void delete(final AuthPrincipal authPrincipal, final Long commentId) { - commentRepository.deleteByIdAndMemberId(commentId, authPrincipal.id()); + final Long memberId = authPrincipal.id(); + final Comment comment = findCommentByIdAndMemberId(commentId, memberId); // 로직 수정 예정 + commentRepository.deleteByIdAndMemberId(commentId, memberId); + eventPublisher.publishEvent(ComponentCommentCountDecreaseEvent.from(comment)); + } + + private void validateComponentId(final CommentCreateRequest request) { + final Long componentId = request.componentId(); + if (!componentRepository.existsById(componentId)) { + throw new NotFoundComponentException(componentId); + } } private Comment findCommentByIdAndMemberId(final Long commentId, final Long memberId) { From 65efddaa65fa7e5e8db13a9f99220df23e401a3b Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:24:58 +0900 Subject: [PATCH 09/17] =?UTF-8?q?[REFACTOR]=20=EC=9D=B4=EB=B2=A4=ED=8A=B8?= =?UTF-8?q?=20=EB=A6=AC=EC=8A=A4=EB=84=88=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=AA=85=20suffix=EB=A5=BC=20Listener=EB=A1=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...va => CommentReplyCountEventListener.java} | 2 +- ...r.java => CommentReportEventListener.java} | 2 +- ...a => ComponentViewCountEventListener.java} | 2 +- .../CommentReplyCountEventListenerTest.java | 74 +++++++++++++++++++ ... ComponentViewCountEventListenerTest.java} | 8 +- 5 files changed, 81 insertions(+), 7 deletions(-) rename src/main/java/ject/componote/domain/comment/application/{CommentReplyCountEventHandler.java => CommentReplyCountEventListener.java} (96%) rename src/main/java/ject/componote/domain/comment/application/{CommentReportEventHandler.java => CommentReportEventListener.java} (96%) rename src/main/java/ject/componote/domain/component/application/{ComponentViewCountEventHandler.java => ComponentViewCountEventListener.java} (96%) create mode 100644 src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java rename src/test/java/ject/componote/domain/component/application/{ComponentViewCountEventHandlerTest.java => ComponentViewCountEventListenerTest.java} (88%) diff --git a/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventHandler.java b/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventListener.java similarity index 96% rename from src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventHandler.java rename to src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventListener.java index 0f7b8746..22b719f6 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventHandler.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventListener.java @@ -15,7 +15,7 @@ @Component @RequiredArgsConstructor -public class CommentReplyCountEventHandler { +public class CommentReplyCountEventListener { private final CommentRepository commentRepository; @Async diff --git a/src/main/java/ject/componote/domain/comment/application/CommentReportEventHandler.java b/src/main/java/ject/componote/domain/comment/application/CommentReportEventListener.java similarity index 96% rename from src/main/java/ject/componote/domain/comment/application/CommentReportEventHandler.java rename to src/main/java/ject/componote/domain/comment/application/CommentReportEventListener.java index a7c8c273..3f1011dd 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentReportEventHandler.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentReportEventListener.java @@ -13,7 +13,7 @@ @Component @RequiredArgsConstructor @Transactional(readOnly = true) -public class CommentReportEventHandler { +public class CommentReportEventListener { private final CommentRepository commentRepository; @Async diff --git a/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java b/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventListener.java similarity index 96% rename from src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java rename to src/main/java/ject/componote/domain/component/application/ComponentViewCountEventListener.java index 1d88242b..a15c913e 100644 --- a/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java +++ b/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventListener.java @@ -11,7 +11,7 @@ @org.springframework.stereotype.Component @RequiredArgsConstructor -public class ComponentViewCountEventHandler { +public class ComponentViewCountEventListener { private final ComponentRepository componentRepository; @Async diff --git a/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java b/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java new file mode 100644 index 00000000..73f6f29a --- /dev/null +++ b/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java @@ -0,0 +1,74 @@ +package ject.componote.domain.comment.application; + +import ject.componote.domain.auth.domain.Member; +import ject.componote.domain.comment.dao.CommentRepository; +import ject.componote.domain.comment.domain.Comment; +import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; +import ject.componote.domain.common.model.Count; +import ject.componote.fixture.CommentFixture; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Optional; + +import static ject.componote.fixture.CommentFixture.답글_이미지O; +import static ject.componote.fixture.MemberFixture.이메일X_회원; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +@ExtendWith(MockitoExtension.class) +class CommentReplyCountEventListenerTest { + @Mock + CommentRepository commentRepository; + + @InjectMocks + CommentReplyCountEventListener commentReplyCountEventListener; + + Member member = 이메일X_회원.생성(1L); + + @ParameterizedTest + @DisplayName("대댓글 개수 증가 이벤트 처리") + @EnumSource(value = CommentFixture.class) + public void handleCommentReplyCountIncreaseEvent(final CommentFixture fixture) throws Exception { + // given + final Long memberId = member.getId(); + final Comment parent = fixture.생성(memberId); + final Long parentId = parent.getParentId(); + + final Comment comment = spy(답글_이미지O.생성()); + doReturn(parentId).when(comment) + .getParentId(); + + final CommentReplyCountIncreaseEvent event = CommentReplyCountIncreaseEvent.from(comment); + final Count previousReplyCount = parent.getReplyCount(); + + // when + doReturn(Optional.of(parent)).when(commentRepository) + .findById(parent.getId()); + commentReplyCountEventListener.handleCommentReplyCountIncreaseEvent(event); + + // then + final Count currentReplyCount = parent.getReplyCount(); + previousReplyCount.increase(); + assertThat(currentReplyCount).isEqualTo(previousReplyCount); + } + + @Test + @DisplayName("대댓글 수 증가 이벤트 처리 시 댓글 ID가 잘못된 경우 예외 발생") + public void handleCommentReplyCountIncreaseEventWhenInvalidCommentId() throws Exception { + + // given + + // when + + // then + + } +} \ No newline at end of file diff --git a/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java b/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventListenerTest.java similarity index 88% rename from src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java rename to src/test/java/ject/componote/domain/component/application/ComponentViewCountEventListenerTest.java index 6381ff84..e746ed04 100644 --- a/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java +++ b/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventListenerTest.java @@ -20,12 +20,12 @@ import static org.mockito.Mockito.doReturn; @ExtendWith(MockitoExtension.class) -class ComponentViewCountEventHandlerTest { +class ComponentViewCountEventListenerTest { @Mock ComponentRepository componentRepository; @InjectMocks - ComponentViewCountEventHandler componentViewCountEventHandler; + ComponentViewCountEventListener componentViewCountEventListener; Component component = INPUT_COMPONENT.생성(); @@ -40,7 +40,7 @@ public void handleViewCountIncrease() throws Exception { // when doReturn(Optional.of(component)).when(componentRepository) .findById(componentId); - componentViewCountEventHandler.handleViewCountIncrease(event); + componentViewCountEventListener.handleViewCountIncrease(event); // then final Count newViewCount = component.getViewCount(); @@ -60,7 +60,7 @@ public void handleViewCountIncreaseWhenInvalidComponentId() throws Exception { .findById(componentId); // then - assertThatThrownBy(() -> componentViewCountEventHandler.handleViewCountIncrease(event)) + assertThatThrownBy(() -> componentViewCountEventListener.handleViewCountIncrease(event)) .isInstanceOf(NotFoundComponentException.class); } From 3fc232916ee315c3cbc962c3ab087f121c3d69bb Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:45:30 +0900 Subject: [PATCH 10/17] =?UTF-8?q?[TEST]=20=EB=AF=B8=EC=99=84=EC=84=B1=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=A3=BC=EC=84=9D=ED=99=94=20(#9?= =?UTF-8?q?5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommentReplyCountEventListenerTest.java | 148 +++++++++--------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java b/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java index 73f6f29a..86f5cd16 100644 --- a/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java +++ b/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java @@ -1,74 +1,74 @@ -package ject.componote.domain.comment.application; - -import ject.componote.domain.auth.domain.Member; -import ject.componote.domain.comment.dao.CommentRepository; -import ject.componote.domain.comment.domain.Comment; -import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; -import ject.componote.domain.common.model.Count; -import ject.componote.fixture.CommentFixture; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.Optional; - -import static ject.componote.fixture.CommentFixture.답글_이미지O; -import static ject.componote.fixture.MemberFixture.이메일X_회원; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; - -@ExtendWith(MockitoExtension.class) -class CommentReplyCountEventListenerTest { - @Mock - CommentRepository commentRepository; - - @InjectMocks - CommentReplyCountEventListener commentReplyCountEventListener; - - Member member = 이메일X_회원.생성(1L); - - @ParameterizedTest - @DisplayName("대댓글 개수 증가 이벤트 처리") - @EnumSource(value = CommentFixture.class) - public void handleCommentReplyCountIncreaseEvent(final CommentFixture fixture) throws Exception { - // given - final Long memberId = member.getId(); - final Comment parent = fixture.생성(memberId); - final Long parentId = parent.getParentId(); - - final Comment comment = spy(답글_이미지O.생성()); - doReturn(parentId).when(comment) - .getParentId(); - - final CommentReplyCountIncreaseEvent event = CommentReplyCountIncreaseEvent.from(comment); - final Count previousReplyCount = parent.getReplyCount(); - - // when - doReturn(Optional.of(parent)).when(commentRepository) - .findById(parent.getId()); - commentReplyCountEventListener.handleCommentReplyCountIncreaseEvent(event); - - // then - final Count currentReplyCount = parent.getReplyCount(); - previousReplyCount.increase(); - assertThat(currentReplyCount).isEqualTo(previousReplyCount); - } - - @Test - @DisplayName("대댓글 수 증가 이벤트 처리 시 댓글 ID가 잘못된 경우 예외 발생") - public void handleCommentReplyCountIncreaseEventWhenInvalidCommentId() throws Exception { - - // given - - // when - - // then - - } -} \ No newline at end of file +//package ject.componote.domain.comment.application; +// +//import ject.componote.domain.auth.domain.Member; +//import ject.componote.domain.comment.dao.CommentRepository; +//import ject.componote.domain.comment.domain.Comment; +//import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; +//import ject.componote.domain.common.model.Count; +//import ject.componote.fixture.CommentFixture; +//import org.junit.jupiter.api.DisplayName; +//import org.junit.jupiter.api.Test; +//import org.junit.jupiter.api.extension.ExtendWith; +//import org.junit.jupiter.params.ParameterizedTest; +//import org.junit.jupiter.params.provider.EnumSource; +//import org.mockito.InjectMocks; +//import org.mockito.Mock; +//import org.mockito.junit.jupiter.MockitoExtension; +// +//import java.util.Optional; +// +//import static ject.componote.fixture.CommentFixture.답글_이미지O; +//import static ject.componote.fixture.MemberFixture.이메일X_회원; +//import static org.assertj.core.api.Assertions.assertThat; +//import static org.mockito.Mockito.doReturn; +//import static org.mockito.Mockito.spy; +// +//@ExtendWith(MockitoExtension.class) +//class CommentReplyCountEventListenerTest { +// @Mock +// CommentRepository commentRepository; +// +// @InjectMocks +// CommentReplyCountEventListener commentReplyCountEventListener; +// +// Member member = 이메일X_회원.생성(1L); +// +// @ParameterizedTest +// @DisplayName("대댓글 개수 증가 이벤트 처리") +// @EnumSource(value = CommentFixture.class) +// public void handleCommentReplyCountIncreaseEvent(final CommentFixture fixture) throws Exception { +// // given +// final Long memberId = member.getId(); +// final Comment parent = fixture.생성(memberId); +// final Long parentId = parent.getParentId(); +// +// final Comment comment = spy(답글_이미지O.생성()); +// doReturn(parentId).when(comment) +// .getParentId(); +// +// final CommentReplyCountIncreaseEvent event = CommentReplyCountIncreaseEvent.from(comment); +// final Count previousReplyCount = parent.getReplyCount(); +// +// // when +// doReturn(Optional.of(parent)).when(commentRepository) +// .findById(parent.getId()); +// commentReplyCountEventListener.handleCommentReplyCountIncreaseEvent(event); +// +// // then +// final Count currentReplyCount = parent.getReplyCount(); +// previousReplyCount.increase(); +// assertThat(currentReplyCount).isEqualTo(previousReplyCount); +// } +// +// @Test +// @DisplayName("대댓글 수 증가 이벤트 처리 시 댓글 ID가 잘못된 경우 예외 발생") +// public void handleCommentReplyCountIncreaseEventWhenInvalidCommentId() throws Exception { +// +// // given +// +// // when +// +// // then +// +// } +//} \ No newline at end of file From 9e902371895745a34e1f71bb61e1e8ec298bfffe Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:46:26 +0900 Subject: [PATCH 11/17] =?UTF-8?q?[REFACTOR]=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EA=B2=BD=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD=20(#95)=20-=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=EA=B0=80=20=EC=97=86=EB=8A=94=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/application/CommentImageEventListener.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java b/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java index 2b4cd7db..8fcc7e74 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java @@ -1,6 +1,7 @@ package ject.componote.domain.comment.application; import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; +import ject.componote.domain.comment.model.CommentImage; import ject.componote.infra.storage.application.StorageProducer; import ject.componote.infra.storage.dto.move.request.ImageMoveRequest; import lombok.RequiredArgsConstructor; @@ -17,6 +18,10 @@ public class CommentImageEventListener { @Async @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void handleImageMove(final CommentImageMoveEvent event) { - storageProducer.sendImageMoveMessage(ImageMoveRequest.from(event.image())); + final CommentImage image = event.image(); + if (image == null || image.isEmpty()) { + return; + } + storageProducer.sendImageMoveMessage(ImageMoveRequest.from(image)); } } From e26c574567f82cc9bf5f689e20088e548f33912f Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:47:13 +0900 Subject: [PATCH 12/17] =?UTF-8?q?[REFACTOR]=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=88=98=EC=A0=95=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD=20(#95)=20-=20StorageService=20?= =?UTF-8?q?=EB=8C=80=EC=8B=A0=20CommentImageEventListener=20=EB=A5=BC=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../componote/domain/comment/application/CommentService.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/ject/componote/domain/comment/application/CommentService.java b/src/main/java/ject/componote/domain/comment/application/CommentService.java index 076457c2..5b908fdb 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentService.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentService.java @@ -24,7 +24,6 @@ import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; import ject.componote.domain.component.error.NotFoundComponentException; -import ject.componote.infra.storage.application.StorageService; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; @@ -39,7 +38,6 @@ public class CommentService { private final ApplicationEventPublisher eventPublisher; private final CommentRepository commentRepository; private final ComponentRepository componentRepository; - private final StorageService storageService; @Transactional public CommentCreateResponse create(final AuthPrincipal authPrincipal, final CommentCreateRequest request) { @@ -87,7 +85,7 @@ public void update(final AuthPrincipal authPrincipal, final Long commentId, fina final CommentImage image = CommentImage.from(commentUpdateRequest.imageObjectKey()); if (!comment.equalsImage(image)) { - storageService.moveImage(image); + eventPublisher.publishEvent(CommentImageMoveEvent.from(comment)); } final CommentContent content = CommentContent.from(commentUpdateRequest.content()); // 가비지가 발생하지 않을까? From 978e4681c3d45c981adddbd5ed75f352dc8ec5c2 Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:47:27 +0900 Subject: [PATCH 13/17] =?UTF-8?q?[TEST]=20CommentServiceTest=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/CommentServiceTest.java | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java b/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java index c8409bf8..f07bc0f1 100644 --- a/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java +++ b/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java @@ -13,6 +13,7 @@ import ject.componote.domain.comment.dto.create.response.CommentCreateResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByComponentResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByMemberResponse; +import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyNotificationEvent; import ject.componote.domain.comment.dto.update.request.CommentUpdateRequest; @@ -21,8 +22,11 @@ import ject.componote.domain.comment.model.CommentImage; import ject.componote.domain.common.dto.response.PageResponse; import ject.componote.domain.common.model.Count; +import ject.componote.domain.component.dao.ComponentRepository; +import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; +import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; +import ject.componote.domain.component.error.NotFoundComponentException; import ject.componote.fixture.CommentFixture; -import ject.componote.infra.storage.application.StorageService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -59,7 +63,7 @@ class CommentServiceTest { CommentRepository commentRepository; @Mock - StorageService storageService; + ComponentRepository componentRepository; @InjectMocks CommentService commentService; @@ -74,6 +78,7 @@ class CommentServiceTest { public void create(final CommentFixture fixture) throws Exception { // given final CommentCreateRequest createRequest = fixture.toCreateRequest(); + final Long componentId = createRequest.componentId(); final Comment comment = fixture.생성(authPrincipal.id()); final CommentCreateResponse expect = CommentCreateResponse.from(comment); @@ -88,10 +93,14 @@ public void create(final CommentFixture fixture) throws Exception { .publishEvent(CommentReplyNotificationEvent.from(comment)); } + doReturn(true).when(componentRepository) + .existsById(componentId); doReturn(comment).when(commentRepository) .save(any()); - doNothing().when(storageService) - .moveImage(comment.getImage()); + doNothing().when(eventPublisher) + .publishEvent(ComponentCommentCountIncreaseEvent.from(comment)); + doNothing().when(eventPublisher) + .publishEvent(CommentImageMoveEvent.from(comment)); final CommentCreateResponse actual = commentService.create(authPrincipal, createRequest); // then @@ -103,16 +112,35 @@ public void create(final CommentFixture fixture) throws Exception { public void createWhenInvalidParentId() { // given final CommentCreateRequest createRequest = 답글_이미지X.toCreateRequest(); + final Long componentId = createRequest.componentId(); final Long parentId = createRequest.parentId(); // when doReturn(false).when(commentRepository) .existsById(parentId); + doReturn(true).when(componentRepository) + .existsById(componentId); + // then assertThatThrownBy(() -> commentService.create(authPrincipal, createRequest)) .isInstanceOf(NotFoundParentCommentException.class); } + @Test + @DisplayName("댓글 생성시 잘못된 componentId가 입력된 경우 예외 발생") + public void createWhenInvalidComponentId() { + // given + final CommentCreateRequest createRequest = 답글_이미지X.toCreateRequest(); + final Long componentId = createRequest.componentId(); + + // when + doReturn(false).when(componentRepository) + .existsById(componentId); + // then + assertThatThrownBy(() -> commentService.create(authPrincipal, createRequest)) + .isInstanceOf(NotFoundComponentException.class); + } + @Test @DisplayName("마이 페이지 댓글 페이징 조회") public void getCommentsByMemberId() throws Exception { @@ -198,6 +226,8 @@ public void updateImage(final CommentFixture fixture) throws Exception { // when doReturn(Optional.of(comment)).when(commentRepository) .findByIdAndMemberId(commentId, memberId); + doNothing().when(eventPublisher) + .publishEvent(CommentImageMoveEvent.from(comment)); // then assertDoesNotThrow( @@ -244,6 +274,8 @@ public void updateAll(final CommentFixture fixture) throws Exception { // when doReturn(Optional.of(comment)).when(commentRepository) .findByIdAndMemberId(commentId, memberId); + doNothing().when(eventPublisher) + .publishEvent(CommentImageMoveEvent.from(comment)); // then assertDoesNotThrow( @@ -263,8 +295,12 @@ public void delete(final CommentFixture fixture) throws Exception { final Long memberId = authPrincipal.id(); // when + doReturn(Optional.of(comment)).when(commentRepository) + .findByIdAndMemberId(commentId, memberId); doNothing().when(commentRepository) .deleteByIdAndMemberId(commentId, memberId); + doNothing().when(eventPublisher) + .publishEvent(ComponentCommentCountDecreaseEvent.from(comment)); // then assertDoesNotThrow( From 5236e67afe023783931d26ca9e3d9c87d17f806a Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:51:27 +0900 Subject: [PATCH 14/17] =?UTF-8?q?[REFACTOR]=20NotFoundParentCommentExcepti?= =?UTF-8?q?on=20=EC=98=88=EC=99=B8=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/comment/error/NotFoundParentCommentException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java b/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java index dcace58d..c8829dee 100644 --- a/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java +++ b/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java @@ -3,7 +3,7 @@ import org.springframework.http.HttpStatus; public class NotFoundParentCommentException extends CommentException { - public NotFoundParentCommentException(final Long parentId) { - super("일치하는 부모 댓글을 찾을 수 없습니다. 댓글 ID: " + parentId, HttpStatus.NOT_FOUND); + public NotFoundParentCommentException() { + super("컴포넌트에 부모 댓글이 존재하지 않습니다.", HttpStatus.NOT_FOUND); } } From ad097801eff9a14af418c63a78357042df88f04f Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:51:43 +0900 Subject: [PATCH 15/17] =?UTF-8?q?[FEAT]=20CommentRepository.existsByIdAndC?= =?UTF-8?q?omponentId()=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ject/componote/domain/comment/dao/CommentRepository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java b/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java index bb04504b..e2f39bd7 100644 --- a/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java +++ b/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java @@ -8,5 +8,6 @@ public interface CommentRepository extends JpaRepository, CommentQueryDsl { Optional findByIdAndMemberId(final Long id, final Long memberId); boolean existsByIdAndMemberId(final Long id, final Long memberId); + boolean existsByIdAndComponentId(final Long id, final Long componentId); void deleteByIdAndMemberId(final Long commentId, final Long memberId); } From 6c188ffdc6f03ac3a22961f4d79cf181cb80102d Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Fri, 7 Feb 2025 21:52:05 +0900 Subject: [PATCH 16/17] =?UTF-8?q?[FEAT]=20=EB=8B=B5=EA=B8=80=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=8B=9C=20=EC=BB=B4=ED=8F=AC=EB=84=8C=ED=8A=B8=20?= =?UTF-8?q?ID,=20=EB=B6=80=EB=AA=A8=20=EB=8C=93=EA=B8=80=20ID=20=EA=B4=80?= =?UTF-8?q?=EA=B3=84=20=EA=B2=80=EC=A6=9D=20=EC=B6=94=EA=B0=80=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../componote/domain/comment/application/CommentService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/ject/componote/domain/comment/application/CommentService.java b/src/main/java/ject/componote/domain/comment/application/CommentService.java index 5b908fdb..682111b5 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentService.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentService.java @@ -143,8 +143,9 @@ private void validateParentId(final CommentCreateRequest request) { } final Long parentId = request.parentId(); - if (!commentRepository.existsById(parentId)) { - throw new NotFoundParentCommentException(parentId); + final Long componentId = request.componentId(); + if (!commentRepository.existsByIdAndComponentId(parentId, componentId)) { + throw new NotFoundParentCommentException(); } } } From 0d23559e2d1c7a3dd952c90a58058ede51ae391e Mon Sep 17 00:00:00 2001 From: kmw2378 Date: Sun, 9 Feb 2025 17:38:30 +0900 Subject: [PATCH 17/17] =?UTF-8?q?[REFACTOR]=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=88=98=20=EC=A6=9D=EA=B0=80=20=EC=9D=B4=EB=B2=A4=ED=8A=B8?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=99=EC=9D=80=20=ED=8A=B8=EB=9E=9C=EC=9E=AD?= =?UTF-8?q?=EC=85=98=EC=97=90=EC=84=9C=20=EC=88=98=ED=96=89=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComponentCommentCountEventListener.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java b/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java index 3ce01737..8ca0a847 100644 --- a/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java +++ b/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java @@ -6,9 +6,6 @@ import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; import ject.componote.domain.component.error.NotFoundComponentException; import lombok.RequiredArgsConstructor; -import org.springframework.scheduling.annotation.Async; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.event.TransactionPhase; import org.springframework.transaction.event.TransactionalEventListener; @@ -17,18 +14,14 @@ public class ComponentCommentCountEventListener { private final ComponentRepository componentRepository; - @Async - @Transactional(propagation = Propagation.REQUIRES_NEW) - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) // cf) Spirng은 내부적으로 트랜잭션 정보를 ThreadLocal 변수에 저장하기 때문에 다른 쓰레드로 트랜잭션이 전파되지 않는다. public void handleCommentCountIncreaseEvent(final ComponentCommentCountIncreaseEvent event) { final Long componentId = event.componentId(); final Component component = findComponentById(componentId); component.increaseCommentCount(); } - @Async - @Transactional(propagation = Propagation.REQUIRES_NEW) - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) public void handleCommentCountDecreaseEvent(final ComponentCommentCountDecreaseEvent event) { final Long componentId = event.componentId(); final Component component = findComponentById(componentId);