Skip to content

Commit c928d5f

Browse files
authored
Merge pull request #152 from dnd-side-project/refactor/first-sprint
fix(PlaylistService): ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ ์ˆ˜์ • ๋กœ์ง ๋ณ€๊ฒฝ
2 parents 9d1dc08 + a657e83 commit c928d5f

File tree

2 files changed

+20
-58
lines changed

2 files changed

+20
-58
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.example.demo.domain.cd.dto.request;
22

3+
import jakarta.validation.constraints.NotNull;
4+
35
import java.util.List;
46

5-
public record SaveCdRequest(List<CdItemRequest> cdItems){
7+
public record SaveCdRequest(@NotNull List<CdItemRequest> cdItems){
68
}

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

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.example.demo.domain.user.repository.UsersRepository;
1919

2020
import java.util.*;
21-
import java.util.stream.Collectors;
2221

2322
import lombok.RequiredArgsConstructor;
2423
import lombok.extern.slf4j.Slf4j;
@@ -65,74 +64,35 @@ public Playlist savePlaylist(String usersId, PlaylistCreateRequest request) {
6564
}
6665

6766
@Transactional
68-
public PlaylistWithSongsResponse editPlaylistWithSongs(String usersId, Long playlistId,
69-
PlaylistCreateRequest request) {
67+
public PlaylistWithSongsResponse editPlaylistWithSongs(
68+
String usersId, Long playlistId, PlaylistCreateRequest request) {
69+
7070
Users users = usersRepository.findById(usersId)
7171
.orElseThrow(() -> new UserException(UserErrorCode.USER_NOT_FOUND));
7272

7373
Playlist playlist = playlistRepository.findById(playlistId)
7474
.orElseThrow(() -> new PlaylistException(PlaylistErrorCode.PLAYLIST_NOT_FOUND));
7575

76-
playlist.editPlaylist(request.name(),request.genre(), request.isPublic());
77-
78-
playlistRepository.save(playlist); // playlist๋ถ€ํ„ฐ ์ˆ˜์ •
76+
playlist.editPlaylist(request.name(), request.genre(), request.isPublic());
77+
playlistRepository.save(playlist);
7978

80-
// ๊ธฐ์กด ๊ณก
81-
List<Song> existingSongs = songRepository.findSongsByPlaylistId(playlistId);
79+
// 1) ๊ธฐ์กด ๊ณก ์ „๋ถ€ ์‚ญ์ œ
80+
songRepository.deleteByPlaylistId(playlistId);
8281

83-
// ์ƒˆ๋กœ ์š”์ฒญ๋œ ๊ณก
84-
List<String> requestedLinks = request.youTubeVideoInfo().stream()
85-
.map(YouTubeVideoInfoDto::link)
86-
.map(String::trim)
82+
// 2) ์š”์ฒญ ์ˆœ์„œ(YouTubeVideoInfoDto.orderIndex)๋ฅผ ๊ทธ๋Œ€๋กœ ์ €์žฅ
83+
List<Song> toSave = request.youTubeVideoInfo().stream()
84+
.sorted(Comparator.comparing(YouTubeVideoInfoDto::orderIndex)) // ์•ˆ์ „ํ•˜๊ฒŒ ์ •๋ ฌ
85+
.map(dto -> SongMapper.toEntity(dto, playlist))
8786
.toList();
8887

89-
Map<String, Long> existingCount = existingSongs.stream()
90-
.collect(Collectors.groupingBy(Song::getYoutubeUrl, Collectors.counting()));
91-
92-
Map<String, Long> requestedCount = requestedLinks.stream()
93-
.collect(Collectors.groupingBy(link -> link, Collectors.counting()));
94-
95-
// ์‚ญ์ œ ๋กœ์ง
96-
List<Long> deleteIds = new ArrayList<>();
97-
for (Song song : existingSongs) {
98-
String url = song.getYoutubeUrl();
99-
long reqCnt = requestedCount.getOrDefault(url, 0L);
100-
long existCnt = existingCount.getOrDefault(url, 0L);
101-
102-
if (existCnt > reqCnt) {
103-
// ์‚ญ์ œ ํ•„์š” โ†’ ํ•˜๋‚˜์”ฉ ์ค„์—ฌ๋‚˜๊ฐ
104-
deleteIds.add(song.getId());
105-
existingCount.put(url, existCnt - 1); // ์นด์šดํŠธ ๊ฐ์†Œ
106-
}
107-
}
108-
if (!deleteIds.isEmpty()) {
109-
songRepository.deleteAllByIdIn(deleteIds);
110-
}
111-
112-
// ์ถ”๊ฐ€
113-
List<Song> toAdd = new ArrayList<>();
114-
for (Map.Entry<String, Long> entry : requestedCount.entrySet()) {
115-
String url = entry.getKey();
116-
long reqCnt = entry.getValue();
117-
118-
long existCnt = existingSongs.stream()
119-
.filter(s -> s.getYoutubeUrl().equals(url))
120-
.count();
121-
122-
if (reqCnt > existCnt) {
123-
long needToAdd = reqCnt - existCnt;
124-
request.youTubeVideoInfo().stream()
125-
.filter(dto -> dto.link().equals(url))
126-
.limit(needToAdd) // ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์ƒ์„ฑ
127-
.forEach(dto -> toAdd.add(SongMapper.toEntity(dto, playlist)));
128-
}
129-
}
130-
if (!toAdd.isEmpty()) {
131-
songRepository.saveAll(toAdd);
88+
if (!toSave.isEmpty()) {
89+
songRepository.saveAll(toSave);
13290
}
13391

134-
// ์‘๋‹ต
135-
List<SongResponseDto> songDtos = songRepository.findSongsByPlaylistId(playlistId).stream()
92+
// 3) ์‘๋‹ต
93+
List<SongResponseDto> songDtos = songRepository
94+
.findSongsByPlaylistId(playlistId)
95+
.stream()
13696
.map(SongMapper::toDto)
13797
.toList();
13898

0 commit comments

Comments
ย (0)