1616import java .time .LocalDate ;
1717import java .util .*;
1818import java .util .stream .Collectors ;
19+ import java .util .stream .Stream ;
1920
2021@ Slf4j
2122@ Service
@@ -31,23 +32,30 @@ public class RecommendationServiceImpl implements RecommendationService {
3132
3233 @ Override
3334 public List <PlaylistCardResponse > getRecommendations (String userId ) {
34- List <Playlist > basePlaylists ;
3535 List <Playlist > genreBased = userPlaylistHistoryRepository .findByUserRecentGenre (userId , 3 );
36+ List <Playlist > visitCountTop6 = playlistRepository .findByVisitCount (6 );
3637
37- if (genreBased .isEmpty ()) {
38- basePlaylists = playlistRepository .findByVisitCount (6 );
39- } else {
40- List <Playlist > visitCountTop3 = playlistRepository .findByVisitCount (3 );
41- basePlaylists = new ArrayList <>(genreBased );
42- basePlaylists .addAll (visitCountTop3 );
43- }
38+ Set <Long > genreIds = genreBased .stream ()
39+ .map (Playlist ::getId )
40+ .collect (Collectors .toCollection (LinkedHashSet ::new ));
41+
42+ List <Playlist > basePlaylists = Stream .concat (
43+ genreBased .stream (),
44+ visitCountTop6 .stream ().filter (p -> !genreIds .contains (p .getId ()))
45+ ).limit (6 ).toList ();
4446
4547 List <Long > playlistIds = basePlaylists .stream ().map (Playlist ::getId ).toList ();
48+
4649 Map <Long , List <Song >> songMap = songRepository .findAllByPlaylistIdIn (playlistIds )
47- .stream ().collect (Collectors .groupingBy (s -> s .getPlaylist ().getId ()));
50+ .stream ()
51+ .collect (Collectors .groupingBy (s -> s .getPlaylist ().getId ()));
4852
4953 return basePlaylists .stream ()
50- .map (p -> PlaylistCardResponse .from (p , songMap .getOrDefault (p .getId (), List .of ()), cdService .getOnlyCdByPlaylistId (p .getId ())))
54+ .map (p -> PlaylistCardResponse .from (
55+ p ,
56+ songMap .getOrDefault (p .getId (), List .of ()),
57+ cdService .getOnlyCdByPlaylistId (p .getId ())
58+ ))
5159 .toList ();
5260 }
5361
0 commit comments