Skip to content

Commit c900d98

Browse files
KoSeonJeclaude
andcommitted
refactor: 이달의 현황 API lastMonthRank를 스냅샷 단건 조회로 변경 (#405)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 67ab911 commit c900d98

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/main/java/ddingdong/ddingdongBE/domain/feed/repository/FeedMonthlyRankingRepository.java

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

33
import ddingdong.ddingdongBE.domain.feed.entity.FeedMonthlyRanking;
44
import java.util.List;
5+
import java.util.Optional;
56
import org.springframework.data.jpa.repository.JpaRepository;
67

78
public interface FeedMonthlyRankingRepository extends JpaRepository<FeedMonthlyRanking, Long> {
@@ -10,4 +11,10 @@ public interface FeedMonthlyRankingRepository extends JpaRepository<FeedMonthlyR
1011

1112
List<FeedMonthlyRanking> findAllByTargetYearAndTargetMonthAndRanking(
1213
int targetYear, int targetMonth, int ranking);
14+
15+
List<FeedMonthlyRanking> findAllByTargetYearAndTargetMonthOrderByRankingAsc(
16+
int targetYear, int targetMonth);
17+
18+
Optional<FeedMonthlyRanking> findByClubIdAndTargetYearAndTargetMonth(
19+
Long clubId, int targetYear, int targetMonth);
1320
}

src/main/java/ddingdong/ddingdongBE/domain/feed/service/GeneralFeedRankingService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import ddingdong.ddingdongBE.domain.club.entity.Club;
44
import ddingdong.ddingdongBE.domain.club.service.ClubService;
5+
import ddingdong.ddingdongBE.domain.feed.entity.FeedMonthlyRanking;
6+
import ddingdong.ddingdongBE.domain.feed.repository.FeedMonthlyRankingRepository;
57
import ddingdong.ddingdongBE.domain.feed.repository.FeedRepository;
68
import ddingdong.ddingdongBE.domain.feed.repository.dto.MonthlyFeedRankingDto;
79
import ddingdong.ddingdongBE.domain.feed.service.dto.query.ClubFeedRankingQuery;
@@ -24,6 +26,7 @@ public class GeneralFeedRankingService implements FeedRankingService {
2426
private static final int COMMENT_WEIGHT = 5;
2527

2628
private final FeedRepository feedRepository;
29+
private final FeedMonthlyRankingRepository feedMonthlyRankingRepository;
2730
private final ClubService clubService;
2831

2932
@Override
@@ -73,12 +76,9 @@ private int getLastMonthRank(Long clubId, int year, int month) {
7376
int lastYear = month == 1 ? year - 1 : year;
7477
int lastMonth = month == 1 ? 12 : month - 1;
7578

76-
List<ClubFeedRankingQuery> lastMonthRankings = getClubFeedRanking(lastYear, lastMonth);
77-
return lastMonthRankings.stream()
78-
.filter(ranking -> ranking.clubId().equals(clubId))
79-
.filter(ranking -> ranking.totalScore() > 0)
80-
.findFirst()
81-
.map(ClubFeedRankingQuery::rank)
79+
return feedMonthlyRankingRepository
80+
.findByClubIdAndTargetYearAndTargetMonth(clubId, lastYear, lastMonth)
81+
.map(FeedMonthlyRanking::getRanking)
8282
.orElse(0);
8383
}
8484

src/test/java/ddingdong/ddingdongBE/domain/feed/service/GeneralFeedRankingServiceTest.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
import ddingdong.ddingdongBE.common.fixture.ClubFixture;
77
import ddingdong.ddingdongBE.common.fixture.FeedFixture;
8+
import ddingdong.ddingdongBE.common.fixture.FeedMonthlyRankingFixture;
89
import ddingdong.ddingdongBE.common.fixture.UserFixture;
910
import ddingdong.ddingdongBE.common.support.TestContainerSupport;
1011
import ddingdong.ddingdongBE.domain.club.entity.Club;
1112
import ddingdong.ddingdongBE.domain.club.repository.ClubRepository;
1213
import ddingdong.ddingdongBE.domain.feed.entity.Feed;
1314
import ddingdong.ddingdongBE.domain.feed.repository.FeedCommentRepository;
15+
import ddingdong.ddingdongBE.domain.feed.repository.FeedMonthlyRankingRepository;
1416
import ddingdong.ddingdongBE.domain.feed.repository.FeedRepository;
1517
import ddingdong.ddingdongBE.domain.feed.service.dto.query.ClubFeedRankingQuery;
1618
import ddingdong.ddingdongBE.domain.feed.service.dto.query.ClubMonthlyStatusQuery;
@@ -41,6 +43,9 @@ class GeneralFeedRankingServiceTest extends TestContainerSupport {
4143
@Autowired
4244
private FeedCommentRepository feedCommentRepository;
4345

46+
@Autowired
47+
private FeedMonthlyRankingRepository feedMonthlyRankingRepository;
48+
4449
@Autowired
4550
private UserRepository userRepository;
4651

@@ -280,18 +285,17 @@ void getClubMonthlyStatus_rankAccuracy() {
280285
});
281286
}
282287

283-
@DisplayName("동아리 이달의 현황 조회 - 성공: 저번 달에 피드가 있으면 lastMonthRank가 반환된다")
288+
@DisplayName("동아리 이달의 현황 조회 - 성공: 저번 달 스냅샷이 있으면 lastMonthRank가 반환된다")
284289
@Test
285290
void getClubMonthlyStatus_withLastMonthRank() {
286291
// given
287292
User user = userRepository.save(UserFixture.createClubUser());
288293
Club club = clubRepository.save(ClubFixture.createClub(user));
289294

290-
// 저번 달 피드 생성
295+
// 저번 달 스냅샷 저장
291296
LocalDate lastMonth = LocalDate.now().minusMonths(1);
292-
Feed lastMonthFeed = feedRepository.save(FeedFixture.createImageFeed(club, "저번달 피드"));
293-
jdbcTemplate.update("UPDATE feed SET created_at = ? WHERE id = ?",
294-
Timestamp.valueOf(lastMonth.atStartOfDay()), lastMonthFeed.getId());
297+
feedMonthlyRankingRepository.save(FeedMonthlyRankingFixture.createWithRanking(
298+
club.getId(), club.getName(), lastMonth.getYear(), lastMonth.getMonthValue(), 1));
295299

296300
// 이번 달 피드 생성
297301
feedRepository.save(FeedFixture.createImageFeed(club, "이번달 피드"));
@@ -302,7 +306,7 @@ void getClubMonthlyStatus_withLastMonthRank() {
302306
// when
303307
ClubMonthlyStatusQuery result = feedRankingService.getClubMonthlyStatus(user.getId(), year, month);
304308

305-
// then — 저번 달에도 피드가 있으므로 lastMonthRank > 0
309+
// then — 저번 달 스냅샷이 있으므로 lastMonthRank > 0
306310
assertSoftly(softly -> {
307311
softly.assertThat(result.rank()).isEqualTo(1);
308312
softly.assertThat(result.lastMonthRank()).isEqualTo(1);
@@ -336,11 +340,10 @@ void getClubMonthlyStatus_noCurrentMonthFeed_butHasLastMonthRank() {
336340
User user = userRepository.save(UserFixture.createClubUser());
337341
Club club = clubRepository.save(ClubFixture.createClub(user));
338342

339-
// 저번 달 피드만 생성 (이번 달 피드 없음)
343+
// 저번 달 스냅샷만 저장 (이번 달 피드 없음)
340344
LocalDate lastMonth = LocalDate.now().minusMonths(1);
341-
Feed lastMonthFeed = feedRepository.save(FeedFixture.createImageFeed(club, "저번달 피드"));
342-
jdbcTemplate.update("UPDATE feed SET created_at = ? WHERE id = ?",
343-
Timestamp.valueOf(lastMonth.atStartOfDay()), lastMonthFeed.getId());
345+
feedMonthlyRankingRepository.save(FeedMonthlyRankingFixture.createWithRanking(
346+
club.getId(), club.getName(), lastMonth.getYear(), lastMonth.getMonthValue(), 1));
344347

345348
int year = LocalDate.now().getYear();
346349
int month = LocalDate.now().getMonthValue();
@@ -363,11 +366,11 @@ void getClubMonthlyStatus_januaryLooksAtDecember() {
363366
User user = userRepository.save(UserFixture.createClubUser());
364367
Club club = clubRepository.save(ClubFixture.createClub(user));
365368

366-
// 전년도 12월 피드 생성
367369
int currentYear = LocalDate.now().getYear();
368-
Feed decemberFeed = feedRepository.save(FeedFixture.createImageFeed(club, "12월 피드"));
369-
jdbcTemplate.update("UPDATE feed SET created_at = ? WHERE id = ?",
370-
Timestamp.valueOf(LocalDateTime.of(currentYear - 1, 12, 15, 10, 0)), decemberFeed.getId());
370+
371+
// 전년도 12월 스냅샷 저장
372+
feedMonthlyRankingRepository.save(FeedMonthlyRankingFixture.createWithRanking(
373+
club.getId(), club.getName(), currentYear - 1, 12, 1));
371374

372375
// 1월 피드 생성
373376
Feed januaryFeed = feedRepository.save(FeedFixture.createImageFeed(club, "1월 피드"));

0 commit comments

Comments
 (0)