Skip to content

Commit b2aebc7

Browse files
authored
Merge pull request #123 from lpromotion/staging
feat: 조회 기반 기중치 계산을 위해 UserInteraction에 count 필드 도입
2 parents 78ab149 + c08515b commit b2aebc7

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

src/main/java/com/example/withpeace/domain/UserInteraction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class UserInteraction {
4343
@Column(name = "action_type", nullable = false)
4444
@Enumerated(EnumType.STRING)
4545
private EActionType actionType;
46+
47+
@Column(name = "count", nullable = false)
48+
private int count = 1;
4649

4750
@Column(name = "action_time", nullable = false)
4851
private LocalDateTime actionTime;
@@ -51,6 +54,7 @@ public class UserInteraction {
5154
public UserInteraction(User user, Policy policy, EActionType actionType) {
5255
this.user = user;
5356
this.policy = policy;
57+
this.count = 1; // 최초 1로 초기화
5458
this.actionType = actionType;
5559
this.actionTime = LocalDateTime.now();
5660
}

src/main/java/com/example/withpeace/repository/UserInteractionRepository.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ public interface UserInteractionRepository extends JpaRepository<UserInteraction
1515

1616
// 사용자 조회 기록 저장 (조회 기록이 없으면 INSERT, 있으면 UPDATE)
1717
@Modifying
18-
@Query(value = "INSERT INTO user_interactions (user_id, policy_id, action_type, action_time) " +
19-
"VALUES (:userId, :policyId, :actionType, NOW())" +
20-
"ON DUPLICATE KEY UPDATE action_time = NOW()", nativeQuery = true)
18+
@Query(value = """
19+
INSERT INTO user_interactions (user_id, policy_id, action_type, count, action_time)
20+
VALUES (:userId, :policyId, :actionType, 1, NOW())
21+
ON DUPLICATE KEY UPDATE
22+
count = IF(action_type = 'VIEW', count + 1, count),
23+
action_time = NOW()
24+
""", nativeQuery = true)
2125
void upsertUserInteraction(Long userId, String policyId, String actionType);
2226

2327
List<UserInteraction> findAllByUserOrderByActionTimeDesc(User user);

src/main/java/com/example/withpeace/repository/ViewPolicyRepository.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
@Repository
1010
public interface ViewPolicyRepository extends JpaRepository<ViewPolicy, Long> {
1111

12-
// 조회수가 존재하면 증가시키는 UPDATE 쿼리 (반환값: 업데이트된 행 개수)
12+
// 정책의 조회 기록이 없으면 INSERT, 있으면 UPDATE
1313
@Modifying
14-
@Query(value = "UPDATE view_policies SET view_count = view_count + 1 " +
15-
"WHERE policy_id = :policyId", nativeQuery = true)
16-
int updateViewCount(String policyId);
17-
18-
// 조회수가 없을 경우 새로운 조회수 INSERT
19-
@Modifying
20-
@Query(value = "INSERT INTO view_policies (policy_id, view_count) " +
21-
"VALUES (:policyId, 1)", nativeQuery = true)
22-
void insertViewCount(String policyId);
14+
@Query(value = """
15+
INSERT INTO view_policies (policy_id, view_count)
16+
VALUES (:policyId, 1)
17+
ON DUPLICATE KEY UPDATE view_count = view_count + 1
18+
""", nativeQuery = true)
19+
void upsertViewCount(String policyId);
2320

2421
}

src/main/java/com/example/withpeace/service/PolicyService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,8 @@ public PolicyDetailResponseDto getPolicyDetail(Long userId, String policyId) {
276276
// 사용자가 해당 정책을 찜했는지 여부 확인
277277
boolean isFavorite = favoritePolicyRepository.existsByUserIdAndPolicyId(userId, policyId);
278278

279-
// 정책 조회수 증가 - 조회수가 존재하면 UPDATE, 존재하지 않으면 INSERT
280-
if (viewPolicyRepository.updateViewCount(policyId) == 0) {
281-
viewPolicyRepository.insertViewCount(policyId);
282-
}
279+
// 정책 조회수 증가 - 조회 기록이 없으면 INSERT, 있으면 UPDATE
280+
viewPolicyRepository.upsertViewCount(policyId);
283281

284282
// 사용자 조회 기록 저장 - 조회 기록이 없으면 INSERT, 있으면 UPDATE
285283
userInteractionRepository.upsertUserInteraction(userId, policyId, EActionType.VIEW.name());
@@ -397,6 +395,7 @@ private Map<String, Integer> calculatePolicyWeightByInteraction(User user) {
397395
for(UserInteraction interaction : interactions) {
398396
String policyId = interaction.getPolicy().getId();
399397
EActionType actionType = interaction.getActionType();
398+
int count = interaction.getCount();
400399
LocalDateTime actionTime = interaction.getActionTime();
401400

402401
// 해당 정책의 현재 가중치 가져오기
@@ -405,7 +404,7 @@ private Map<String, Integer> calculatePolicyWeightByInteraction(User user) {
405404

406405
// 상호작용 타입별 가중치 추가
407406
switch (actionType) {
408-
case VIEW -> weight += 1; // 조회 -> +1점
407+
case VIEW -> weight += count; // 조회 -> +(count*1)점
409408
case FAVORITE -> weight +=3; // 찜하기 -> +3점
410409
}
411410

0 commit comments

Comments
 (0)