Skip to content

Commit 9309db8

Browse files
committed
feat: 사용자 행동 상태 로깅
1 parent 09c833d commit 9309db8

5 files changed

Lines changed: 74 additions & 2 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

apps/commerce-api/src/main/java/com/loopers/application/product/ProductFacade.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.loopers.application.product;
22

33
import com.loopers.domain.brand.BrandRepository;
4-
import com.loopers.domain.like.LikeRepository;
54
import com.loopers.domain.product.Product;
65
import com.loopers.domain.product.ProductRepository;
76
import com.loopers.interfaces.api.product.ProductV1Dto;
@@ -10,6 +9,7 @@
109
import lombok.RequiredArgsConstructor;
1110
import org.springframework.cache.annotation.CacheEvict;
1211
import org.springframework.cache.annotation.Cacheable;
12+
import org.springframework.context.ApplicationEventPublisher;
1313
import org.springframework.stereotype.Component;
1414
import org.springframework.transaction.annotation.Transactional;
1515

@@ -20,8 +20,8 @@
2020
@RequiredArgsConstructor
2121
public 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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.loopers.application.product;
2+
3+
public record ProductLookedUpEvent(Long productId) {
4+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

0 commit comments

Comments
 (0)