Skip to content

Commit 5d11f21

Browse files
authored
Merge pull request #137 from dnd-side-project/dev
fix(PlaylistSaveService): ν”Œλ ˆμ΄λ¦¬μŠ€νŠΈ μˆ˜μ • μ‹œ 곑 쀑볡 κ°€λŠ₯ν•˜λ„λ‘ μˆ˜μ •
2 parents 03a0c93 + 0d41235 commit 5d11f21

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

β€Žapi-server/src/main/java/com/example/demo/domain/songs/service/YouTubeSongServiceImpl.javaβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ private Map<String, String> extractValidLinkToVideoId(List<String> links) {
5454
.filter(this::isValidYouTubeUrl)
5555
.collect(Collectors.toMap(
5656
Function.identity(),
57-
this::extractVideoId
57+
this::extractVideoId,
58+
(exist, dup) -> exist, // 쀑볡 μ‹œ 첫 κ°’ μœ μ§€
59+
LinkedHashMap::new // μ‚½μž… μˆœμ„œ 보쑴
5860
));
5961
}
6062

β€Žmain-server/src/main/java/com/example/demo/domain/playlist/service/PlaylistSaveService.javaβ€Ž

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
Β (0)