@@ -88,34 +88,56 @@ public PlaylistWithSongsResponse editPlaylistWithSongs(String usersId, Long play
8888
8989 playlistRepository .save (playlist ); // playlistλΆν° μμ
9090
91+ // 기쑴 곑
9192 List <Song > existingSongs = songRepository .findSongsByPlaylistId (playlistId );
9293
93- // μμ² λ§ν¬ μΈνΈ(κ²μ¦λ λ§ν¬λΌκ³ κ°μ )
94- Set <String > requestedLinks = request .youTubeVideoInfo ().stream ()
94+ // μλ‘ μμ²λ 곑
95+ List <String > requestedLinks = request .youTubeVideoInfo ().stream ()
9596 .map (YouTubeVideoInfoDto ::link )
96- .collect (Collectors .toCollection (LinkedHashSet ::new ));
97+ .map (String ::trim )
98+ .toList ();
9799
98- // κΈ°μ‘΄ λ§ν¬ μΈνΈ
99- Set <String > existingLinks = existingSongs .stream ()
100- .map (Song ::getYoutubeUrl )
101- .collect (Collectors .toSet ());
100+ Map <String , Long > existingCount = existingSongs .stream ()
101+ .collect (Collectors .groupingBy (Song ::getYoutubeUrl , Collectors .counting ()));
102102
103- // 1) μμ : κΈ°μ‘΄μ μμΌλ μμ²μ μλ λ§ν¬ -> id μμ§ ν deleteAllByIdIn
104- List <Long > deleteIds = existingSongs .stream ()
105- .filter (s -> !requestedLinks .contains (s .getYoutubeUrl ()))
106- .map (Song ::getId )
107- .toList ();
103+ Map <String , Long > requestedCount = requestedLinks .stream ()
104+ .collect (Collectors .groupingBy (link -> link , Collectors .counting ()));
108105
106+ // μμ λ‘μ§
107+ List <Long > deleteIds = new ArrayList <>();
108+ for (Song song : existingSongs ) {
109+ String url = song .getYoutubeUrl ();
110+ long reqCnt = requestedCount .getOrDefault (url , 0L );
111+ long existCnt = existingCount .getOrDefault (url , 0L );
112+
113+ if (existCnt > reqCnt ) {
114+ // μμ νμ β νλμ© μ€μ¬λκ°
115+ deleteIds .add (song .getId ());
116+ existingCount .put (url , existCnt - 1 ); // μΉ΄μ΄νΈ κ°μ
117+ }
118+ }
109119 if (!deleteIds .isEmpty ()) {
110120 songRepository .deleteAllByIdIn (deleteIds );
111121 }
112122
113- // 2) μΆκ°: μμ²μ μμΌλ κΈ°μ‘΄μ μλ λ§ν¬λ§ μ μ₯
114- List <Song > toAdd = request .youTubeVideoInfo ().stream ()
115- .filter (dto -> !existingLinks .contains (dto .link ()))
116- .map (dto -> SongMapper .toEntity (dto , playlist ))
117- .toList ();
118-
123+ // μΆκ°
124+ List <Song > toAdd = new ArrayList <>();
125+ for (Map .Entry <String , Long > entry : requestedCount .entrySet ()) {
126+ String url = entry .getKey ();
127+ long reqCnt = entry .getValue ();
128+
129+ long existCnt = existingSongs .stream ()
130+ .filter (s -> s .getYoutubeUrl ().equals (url ))
131+ .count ();
132+
133+ if (reqCnt > existCnt ) {
134+ long needToAdd = reqCnt - existCnt ;
135+ request .youTubeVideoInfo ().stream ()
136+ .filter (dto -> dto .link ().equals (url ))
137+ .limit (needToAdd ) // νμν λ§νΌλ§ μμ±
138+ .forEach (dto -> toAdd .add (SongMapper .toEntity (dto , playlist )));
139+ }
140+ }
119141 if (!toAdd .isEmpty ()) {
120142 songRepository .saveAll (toAdd );
121143 }
@@ -133,9 +155,6 @@ public PlaylistWithSongsResponse editPlaylistWithSongs(String usersId, Long play
133155 return new PlaylistWithSongsResponse (playlist .getId (), songDtos );
134156 }
135157
136- /**
137- * κΈ°μ‘΄ λν ν΄μ β μ λν μ€μ β RepresentativePlaylist κ°±μ
138- */
139158 public void replaceRepresentativePlaylist (Users user , Playlist newRepPlaylist ) {
140159 String userId = user .getId ();
141160
0 commit comments