1010import spring .memewikibe .infrastructure .MemeViewLogRepository ;
1111
1212import java .time .Duration ;
13+ import java .util .ArrayList ;
1314import 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}
0 commit comments