Skip to content

Commit 92665a2

Browse files
committed
feat: 이벤트 기반 좋아요 처리와 집계 분리
1 parent 022d90f commit 92665a2

4 files changed

Lines changed: 34 additions & 20 deletions

File tree

apps/commerce-api/src/main/java/com/loopers/application/event/LikeEventHandler.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.loopers.application.event.like;
2+
3+
import com.loopers.application.like.LikeCreateEvent;
4+
import com.loopers.domain.product.Product;
5+
import com.loopers.domain.product.ProductRepository;
6+
import com.loopers.support.error.CoreException;
7+
import com.loopers.support.error.ErrorType;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Component;
10+
import org.springframework.transaction.annotation.Propagation;
11+
import org.springframework.transaction.annotation.Transactional;
12+
import org.springframework.transaction.event.TransactionPhase;
13+
import org.springframework.transaction.event.TransactionalEventListener;
14+
15+
@Component
16+
@RequiredArgsConstructor
17+
public class LikeEventHandler {
18+
private final ProductRepository productRepository;
19+
20+
@Transactional(propagation = Propagation.REQUIRES_NEW)
21+
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
22+
public void onLikeCreated(LikeCreateEvent event) {
23+
Product product = productRepository.findById(event.productId()).orElseThrow(
24+
() -> new CoreException(ErrorType.NOT_FOUND, "존재하지 않는 상품입니다.")
25+
);
26+
27+
product.addLikeCount();
28+
}
29+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.loopers.application.like;
2+
3+
public record LikeCreateEvent(Long userId, Long productId) {
4+
}

apps/commerce-api/src/main/java/com/loopers/application/like/LikeFacade.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.loopers.domain.like.Like;
44
import com.loopers.domain.like.LikeRepository;
5-
import com.loopers.domain.product.Product;
65
import com.loopers.domain.product.ProductRepository;
76
import com.loopers.domain.user.UserRepository;
87
import com.loopers.interfaces.api.like.LikeV1Dto;
@@ -35,7 +34,7 @@ public LikeInfo doLike(LikeV1Dto.LikeRequest request) {
3534
() -> new CoreException(ErrorType.NOT_FOUND, "존재하지 않는 유저입니다.")
3635
);
3736

38-
Product product = productRepository.findById(productId).orElseThrow(
37+
productRepository.findById(productId).orElseThrow(
3938
() -> new CoreException(ErrorType.NOT_FOUND, "존재하지 않는 상품입니다.")
4039
);
4140

0 commit comments

Comments
 (0)