Skip to content

Commit 7963473

Browse files
committed
feat: 밈 집계시 집계 데이터가 없을 경우 fallback으로 최신 데이터를 가져오도록 구현한다
1 parent a8bf345 commit 7963473

File tree

4 files changed

+231
-16
lines changed

4 files changed

+231
-16
lines changed

src/main/java/spring/memewikibe/application/MemeAggregationLookUpServiceImpl.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import spring.memewikibe.infrastructure.MemeViewLogRepository;
1111

1212
import java.time.Duration;
13+
import java.util.ArrayList;
1314
import java.util.List;
1415

1516
@Service
@@ -20,13 +21,11 @@ public class MemeAggregationLookUpServiceImpl implements MemeAggregationLookUpSe
2021
private final static int MOST_POPULAR_MEMES_COUNT = 6;
2122

2223
private final MemeCustomLogRepository customLogRepository;
23-
private final MemeViewLogRepository viewLogRepository;
2424
private final MemeShareLogRepository shareLogRepository;
2525
private final MemeRepository memeRepository;
2626

2727
public MemeAggregationLookUpServiceImpl(MemeCustomLogRepository customLogRepository, MemeViewLogRepository viewLogRepository, MemeShareLogRepository shareLogRepository, MemeRepository memeRepository) {
2828
this.customLogRepository = customLogRepository;
29-
this.viewLogRepository = viewLogRepository;
3029
this.shareLogRepository = shareLogRepository;
3130
this.memeRepository = memeRepository;
3231
}
@@ -35,21 +34,43 @@ public MemeAggregationLookUpServiceImpl(MemeCustomLogRepository customLogReposit
3534
@Transactional(readOnly = true)
3635
@Override
3736
public List<MemeSimpleResponse> getMostFrequentSharedMemes() {
38-
return shareLogRepository.findTopMemesByShareCountWithin(AGGREGATION_DURATION_PERIOD, AGGREGATION_ITEM_COUNT);
37+
List<MemeSimpleResponse> response = shareLogRepository.findTopMemesByShareCountWithin(AGGREGATION_DURATION_PERIOD, AGGREGATION_ITEM_COUNT);
38+
if (response.size() < AGGREGATION_ITEM_COUNT) {
39+
return fillWithLatestMemes(response, AGGREGATION_ITEM_COUNT);
40+
}
41+
return response;
3942
}
4043

4144
@Transactional(readOnly = true)
4245
@Override
4346
public List<MemeSimpleResponse> getMostFrequentCustomMemes() {
44-
return customLogRepository.findTopMemesByCustomCountWithin(AGGREGATION_DURATION_PERIOD, AGGREGATION_ITEM_COUNT);
47+
List<MemeSimpleResponse> response = customLogRepository.findTopMemesByCustomCountWithin(AGGREGATION_DURATION_PERIOD, AGGREGATION_ITEM_COUNT);
48+
if (response.size() < AGGREGATION_ITEM_COUNT) {
49+
return fillWithLatestMemes(response, AGGREGATION_ITEM_COUNT);
50+
}
51+
return response;
4552
}
4653

4754
@Transactional(readOnly = true)
4855
@Override
4956
public List<MemeSimpleResponse> getMostPopularMemes() {
5057
List<MemeAggregationResult> aggregationResult = memeRepository.findTopRatedMemesBy(AGGREGATION_DURATION_PERIOD, MOST_POPULAR_MEMES_COUNT);
51-
return aggregationResult.stream()
58+
List<MemeSimpleResponse> response = aggregationResult.stream()
5259
.map(it -> new MemeSimpleResponse(it.id(), it.title(), it.imgUrl()))
5360
.toList();
61+
if (response.size() < MOST_POPULAR_MEMES_COUNT) {
62+
return fillWithLatestMemes(response, MOST_POPULAR_MEMES_COUNT);
63+
}
64+
return response;
65+
}
66+
67+
private List<MemeSimpleResponse> fillWithLatestMemes(List<MemeSimpleResponse> originalList, int targetCount) {
68+
int remainingCount = targetCount - originalList.size();
69+
List<Long> existingIds = originalList.stream().map(MemeSimpleResponse::id).toList();
70+
List<MemeSimpleResponse> latestMemes = memeRepository.findLatestMemesExcludingIds(existingIds, remainingCount);
71+
72+
List<MemeSimpleResponse> result = new ArrayList<>(originalList);
73+
result.addAll(latestMemes);
74+
return result;
5475
}
5576
}

src/main/java/spring/memewikibe/infrastructure/MemeAggregationRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package spring.memewikibe.infrastructure;
22

33
import org.springframework.data.domain.Limit;
4+
import spring.memewikibe.api.controller.meme.response.MemeSimpleResponse;
45
import spring.memewikibe.domain.meme.Meme;
56
import spring.memewikibe.domain.meme.MemeAggregationResult;
67

@@ -13,4 +14,6 @@ public interface MemeAggregationRepository {
1314
List<Meme> findByTitleDynamicContainingOrderByIdDesc(String title, Limit limit);
1415

1516
List<Meme> findByTitleDynamicContainingAndIdLessThanOrderByIdDesc(String title, Long lastId, Limit limit);
17+
18+
List<MemeSimpleResponse> findLatestMemesExcludingIds(List<Long> excludeIds, int limit);
1619
}

src/main/java/spring/memewikibe/infrastructure/MemeAggregationRepositoryImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.querydsl.jpa.impl.JPAQueryFactory;
1010
import org.springframework.data.domain.Limit;
1111
import org.springframework.stereotype.Repository;
12+
import spring.memewikibe.api.controller.meme.response.MemeSimpleResponse;
1213
import spring.memewikibe.domain.meme.Meme;
1314
import spring.memewikibe.domain.meme.MemeAggregationResult;
1415

@@ -101,6 +102,21 @@ public List<Meme> findByTitleDynamicContainingAndIdLessThanOrderByIdDesc(String
101102
.fetch();
102103
}
103104

105+
@Override
106+
public List<MemeSimpleResponse> findLatestMemesExcludingIds(List<Long> excludeIds, int limit) {
107+
return queryFactory
108+
.select(Projections.constructor(MemeSimpleResponse.class,
109+
meme.id,
110+
meme.title,
111+
meme.imgUrl
112+
))
113+
.from(meme)
114+
.where(excludeIds.isEmpty() ? null : meme.id.notIn(excludeIds))
115+
.orderBy(meme.id.desc())
116+
.limit(limit)
117+
.fetch();
118+
}
119+
104120
private BooleanExpression titleContains(String title) {
105121
return title != null && !title.trim().isEmpty() ? meme.title.containsIgnoreCase(title) : null;
106122
}

0 commit comments

Comments
 (0)