Skip to content

Commit 170808d

Browse files
committed
feat: MonthlyRankingJpaRepository 및 WeeklyRankingJpaRepository에 삭제 메서드 추가
1 parent 4563be8 commit 170808d

11 files changed

Lines changed: 261 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.loopers.infrastructure.ranking;
2+
3+
import com.loopers.domain.ranking.MonthlyRanking;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
8+
import java.time.YearMonth;
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
public interface MonthlyRankingJpaRepository extends JpaRepository<MonthlyRanking, Long> {
13+
14+
@Query(value = """
15+
SELECT * FROM mv_product_rank_monthly
16+
WHERE month_period = :monthPeriod
17+
ORDER BY monthly_rank ASC
18+
LIMIT :limit OFFSET :offset
19+
""", nativeQuery = true)
20+
List<MonthlyRanking> findByPeriodWithPagination(
21+
@Param("monthPeriod") String monthPeriod,
22+
@Param("limit") int limit,
23+
@Param("offset") int offset);
24+
25+
Optional<MonthlyRanking> findByProductIdAndMonthPeriod(Long productId, YearMonth monthPeriod);
26+
27+
long countByMonthPeriod(YearMonth monthPeriod);
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.loopers.infrastructure.ranking;
2+
3+
import com.loopers.domain.ranking.*;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.stereotype.Repository;
6+
7+
import java.time.LocalDate;
8+
import java.time.YearMonth;
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
@Repository
13+
@RequiredArgsConstructor
14+
public class RankingRepositoryImpl implements RankingRepository {
15+
16+
private final WeeklyRankingJpaRepository weeklyJpaRepository;
17+
private final MonthlyRankingJpaRepository monthlyJpaRepository;
18+
19+
@Override
20+
public List<WeeklyRanking> findWeeklyByDateOrderByRank(LocalDate weekStart, LocalDate weekEnd, int limit, int offset) {
21+
return weeklyJpaRepository.findByDateWithPagination(weekStart, weekEnd, limit, offset);
22+
}
23+
24+
@Override
25+
public Optional<WeeklyRanking> findWeeklyByProductIdAndDate(Long productId, LocalDate weekStart, LocalDate weekEnd) {
26+
return weeklyJpaRepository.findByProductIdAndWeekStartAndWeekEnd(productId, weekStart, weekEnd);
27+
}
28+
29+
@Override
30+
public long countWeeklyByDate(LocalDate weekStart, LocalDate weekEnd) {
31+
return weeklyJpaRepository.countByWeekStartAndWeekEnd(weekStart, weekEnd);
32+
}
33+
34+
@Override
35+
public List<MonthlyRanking> findMonthlyByPeriodOrderByRank(YearMonth period, int limit, int offset) {
36+
return monthlyJpaRepository.findByPeriodWithPagination(period.toString(), limit, offset);
37+
}
38+
39+
@Override
40+
public Optional<MonthlyRanking> findMonthlyByProductIdAndPeriod(Long productId, YearMonth period) {
41+
return monthlyJpaRepository.findByProductIdAndMonthPeriod(productId, period);
42+
}
43+
44+
@Override
45+
public long countMonthlyByPeriod(YearMonth period) {
46+
return monthlyJpaRepository.countByMonthPeriod(period);
47+
}
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.loopers.infrastructure.ranking;
2+
3+
import com.loopers.domain.ranking.WeeklyRanking;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
8+
import java.time.LocalDate;
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
public interface WeeklyRankingJpaRepository extends JpaRepository<WeeklyRanking, Long> {
13+
14+
@Query(value = """
15+
SELECT * FROM mv_product_rank_weekly
16+
WHERE week_start = :weekStart AND week_end = :weekEnd
17+
ORDER BY weekly_rank ASC
18+
LIMIT :limit OFFSET :offset
19+
""", nativeQuery = true)
20+
List<WeeklyRanking> findByDateWithPagination(
21+
@Param("weekStart") LocalDate weekStart,
22+
@Param("weekEnd") LocalDate weekEnd,
23+
@Param("limit") int limit,
24+
@Param("offset") int offset);
25+
26+
Optional<WeeklyRanking> findByProductIdAndWeekStartAndWeekEnd(Long productId, LocalDate weekStart, LocalDate weekEnd);
27+
28+
long countByWeekStartAndWeekEnd(LocalDate weekStart, LocalDate weekEnd);
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.loopers.batch.domain.metrics;
2+
3+
import com.loopers.dto.ProductMetricsSummary;
4+
5+
import java.time.LocalDate;
6+
import java.util.List;
7+
8+
public interface ProductMetricsRepository {
9+
List<ProductMetricsSummary> findAllByDateRange(LocalDate startDate, LocalDate endDate);
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.loopers.batch.domain.ranking;
2+
3+
import java.time.YearMonth;
4+
import java.util.List;
5+
6+
public interface MonthlyRankingRepository {
7+
8+
void deleteByMonthPeriod(YearMonth monthPeriod);
9+
10+
void saveAll(List<MonthlyRanking> rankings);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.loopers.batch.domain.ranking;
2+
3+
import java.time.LocalDate;
4+
import java.util.List;
5+
6+
public interface WeeklyRankingRepository {
7+
8+
void deleteByWeekStartAndWeekEnd(LocalDate weekStart, LocalDate weekEnd);
9+
10+
void saveAll(List<WeeklyRanking> rankings);
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.loopers.infrastructure.metrics;
2+
3+
import com.loopers.batch.domain.metrics.ProductMetrics;
4+
import com.loopers.batch.domain.metrics.ProductMetricsId;
5+
import com.loopers.dto.ProductMetricsSummary;
6+
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.data.jpa.repository.Query;
8+
import org.springframework.data.repository.query.Param;
9+
10+
import java.time.LocalDate;
11+
import java.util.List;
12+
13+
public interface ProductMetricsJpaRepository extends JpaRepository<ProductMetrics, ProductMetricsId> {
14+
15+
@Query("""
16+
SELECT new com.loopers.dto.ProductMetricsSummary(
17+
pm.id.productId,
18+
pm.id.metricsDate,
19+
CAST(pm.likesDelta AS long),
20+
CAST(pm.salesDelta AS long),
21+
CAST(pm.viewsDelta AS long)
22+
)
23+
FROM ProductMetrics pm
24+
WHERE pm.id.metricsDate BETWEEN :startDate AND :endDate
25+
ORDER BY pm.id.productId, pm.id.metricsDate
26+
""")
27+
List<ProductMetricsSummary> findAllByDateRange(
28+
@Param("startDate") LocalDate startDate,
29+
@Param("endDate") LocalDate endDate
30+
);
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.loopers.infrastructure.metrics;
2+
3+
import com.loopers.batch.domain.metrics.ProductMetricsRepository;
4+
import com.loopers.dto.ProductMetricsSummary;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.time.LocalDate;
9+
import java.util.List;
10+
11+
@Repository
12+
@RequiredArgsConstructor
13+
public class ProductMetricsRepositoryImpl implements ProductMetricsRepository {
14+
15+
private final ProductMetricsJpaRepository jpaRepository;
16+
17+
@Override
18+
public List<ProductMetricsSummary> findAllByDateRange(LocalDate startDate, LocalDate endDate) {
19+
return jpaRepository.findAllByDateRange(startDate, endDate);
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.loopers.infrastructure.ranking;
2+
3+
import com.loopers.batch.domain.ranking.MonthlyRanking;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
9+
import java.time.YearMonth;
10+
11+
public interface MonthlyRankingJpaRepository extends JpaRepository<MonthlyRanking, Long> {
12+
13+
@Modifying
14+
@Query("DELETE FROM MonthlyRanking m WHERE m.monthPeriod = :monthPeriod")
15+
void deleteByMonthPeriod(@Param("monthPeriod") YearMonth monthPeriod);
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.loopers.infrastructure.ranking;
2+
3+
import com.loopers.batch.domain.ranking.MonthlyRanking;
4+
import com.loopers.batch.domain.ranking.MonthlyRankingRepository;
5+
import com.loopers.batch.domain.ranking.WeeklyRanking;
6+
import com.loopers.batch.domain.ranking.WeeklyRankingRepository;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.time.LocalDate;
11+
import java.time.YearMonth;
12+
import java.util.List;
13+
14+
@Repository
15+
@RequiredArgsConstructor
16+
public class RankingRepositoryImpl implements WeeklyRankingRepository, MonthlyRankingRepository {
17+
18+
private final WeeklyRankingJpaRepository weeklyJpaRepository;
19+
private final MonthlyRankingJpaRepository monthlyJpaRepository;
20+
21+
@Override
22+
public void deleteByWeekStartAndWeekEnd(LocalDate weekStart, LocalDate weekEnd) {
23+
weeklyJpaRepository.deleteByWeekStartAndWeekEnd(weekStart, weekEnd);
24+
}
25+
26+
@Override
27+
public void saveAll(List<WeeklyRanking> rankings) {
28+
weeklyJpaRepository.saveAll(rankings);
29+
}
30+
31+
@Override
32+
public void deleteByMonthPeriod(YearMonth monthPeriod) {
33+
monthlyJpaRepository.deleteByMonthPeriod(monthPeriod);
34+
}
35+
36+
@Override
37+
public void saveAll(List<MonthlyRanking> rankings) {
38+
monthlyJpaRepository.saveAll(rankings);
39+
}
40+
}

0 commit comments

Comments
 (0)