File tree Expand file tree Collapse file tree
apps/commerce-api/src/main/java/com/loopers Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .loopers .application .event .product ;
2+
3+ import com .loopers .application .product .ProductLookedUpEvent ;
4+ import com .loopers .domain .actionlog .UserActionLog ;
5+ import com .loopers .infrastructure .actionlog .UserActionLogJpaRepository ;
6+ import lombok .RequiredArgsConstructor ;
7+ import org .springframework .stereotype .Component ;
8+ import org .springframework .transaction .annotation .Propagation ;
9+ import org .springframework .transaction .annotation .Transactional ;
10+ import org .springframework .transaction .event .TransactionPhase ;
11+ import org .springframework .transaction .event .TransactionalEventListener ;
12+
13+ @ Component
14+ @ RequiredArgsConstructor
15+ public class ProductEventHandler {
16+ private final UserActionLogJpaRepository userActionLogJpaRepository ;
17+
18+ @ Transactional (propagation = Propagation .REQUIRES_NEW )
19+ @ TransactionalEventListener (phase = TransactionPhase .AFTER_COMMIT )
20+ public void onProductLookedUp (ProductLookedUpEvent event ) {
21+ UserActionLog userActionLog = UserActionLog .create (event );
22+ userActionLogJpaRepository .save (userActionLog );
23+
24+ }
25+ }
Original file line number Diff line number Diff line change 11package com .loopers .application .product ;
22
33import com .loopers .domain .brand .BrandRepository ;
4- import com .loopers .domain .like .LikeRepository ;
54import com .loopers .domain .product .Product ;
65import com .loopers .domain .product .ProductRepository ;
76import com .loopers .interfaces .api .product .ProductV1Dto ;
109import lombok .RequiredArgsConstructor ;
1110import org .springframework .cache .annotation .CacheEvict ;
1211import org .springframework .cache .annotation .Cacheable ;
12+ import org .springframework .context .ApplicationEventPublisher ;
1313import org .springframework .stereotype .Component ;
1414import org .springframework .transaction .annotation .Transactional ;
1515
2020@ RequiredArgsConstructor
2121public class ProductFacade {
2222 private final ProductRepository productRepository ;
23- private final LikeRepository likeRepository ;
2423 private final BrandRepository brandRepository ;
24+ private final ApplicationEventPublisher publisher ;
2525
2626
2727 @ Transactional
@@ -55,6 +55,8 @@ public ProductInfo findProductById(Long id) {
5555 () -> new CoreException (ErrorType .NOT_FOUND , "찾고자 하는 상품이 존재하지 않습니다." )
5656 );
5757
58+ publisher .publishEvent (new ProductLookedUpEvent (product .getId ()));
59+
5860 return ProductInfo .from (product );
5961 }
6062
Original file line number Diff line number Diff line change 1+ package com .loopers .application .product ;
2+
3+ public record ProductLookedUpEvent (Long productId ) {
4+ }
Original file line number Diff line number Diff line change 1+ package com .loopers .domain .actionlog ;
2+
3+ import com .loopers .application .product .ProductLookedUpEvent ;
4+ import com .loopers .domain .BaseEntity ;
5+ import jakarta .persistence .*;
6+ import lombok .AccessLevel ;
7+ import lombok .Getter ;
8+ import lombok .NoArgsConstructor ;
9+
10+
11+ @ Entity
12+ @ Table (name = "user_action_log" )
13+ @ NoArgsConstructor (access = AccessLevel .PROTECTED )
14+ @ Getter
15+ public class UserActionLog extends BaseEntity {
16+ @ Id
17+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
18+ private Long id ;
19+
20+ /*
21+ 유저는 추후 추가 예정
22+ */
23+
24+ @ Column (name = "product_id" , nullable = false )
25+ private Long productId ;
26+
27+ public UserActionLog (Long productId ) {
28+ this .productId = productId ;
29+ }
30+
31+ public static UserActionLog create (ProductLookedUpEvent event ) {
32+ return new UserActionLog (event .productId ());
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package com .loopers .infrastructure .actionlog ;
2+
3+ import com .loopers .domain .actionlog .UserActionLog ;
4+ import org .springframework .data .jpa .repository .JpaRepository ;
5+
6+ public interface UserActionLogJpaRepository extends JpaRepository <UserActionLog , Long > {
7+ }
You can’t perform that action at this time.
0 commit comments