|
18 | 18 | import com.example.demo.domain.user.repository.UsersRepository; |
19 | 19 |
|
20 | 20 | import java.util.*; |
21 | | -import java.util.stream.Collectors; |
22 | 21 |
|
23 | 22 | import lombok.RequiredArgsConstructor; |
24 | 23 | import lombok.extern.slf4j.Slf4j; |
@@ -65,74 +64,35 @@ public Playlist savePlaylist(String usersId, PlaylistCreateRequest request) { |
65 | 64 | } |
66 | 65 |
|
67 | 66 | @Transactional |
68 | | - public PlaylistWithSongsResponse editPlaylistWithSongs(String usersId, Long playlistId, |
69 | | - PlaylistCreateRequest request) { |
| 67 | + public PlaylistWithSongsResponse editPlaylistWithSongs( |
| 68 | + String usersId, Long playlistId, PlaylistCreateRequest request) { |
| 69 | + |
70 | 70 | Users users = usersRepository.findById(usersId) |
71 | 71 | .orElseThrow(() -> new UserException(UserErrorCode.USER_NOT_FOUND)); |
72 | 72 |
|
73 | 73 | Playlist playlist = playlistRepository.findById(playlistId) |
74 | 74 | .orElseThrow(() -> new PlaylistException(PlaylistErrorCode.PLAYLIST_NOT_FOUND)); |
75 | 75 |
|
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); |
79 | 78 |
|
80 | | - // ๊ธฐ์กด ๊ณก |
81 | | - List<Song> existingSongs = songRepository.findSongsByPlaylistId(playlistId); |
| 79 | + // 1) ๊ธฐ์กด ๊ณก ์ ๋ถ ์ญ์ |
| 80 | + songRepository.deleteByPlaylistId(playlistId); |
82 | 81 |
|
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)) |
87 | 86 | .toList(); |
88 | 87 |
|
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); |
132 | 90 | } |
133 | 91 |
|
134 | | - // ์๋ต |
135 | | - List<SongResponseDto> songDtos = songRepository.findSongsByPlaylistId(playlistId).stream() |
| 92 | + // 3) ์๋ต |
| 93 | + List<SongResponseDto> songDtos = songRepository |
| 94 | + .findSongsByPlaylistId(playlistId) |
| 95 | + .stream() |
136 | 96 | .map(SongMapper::toDto) |
137 | 97 | .toList(); |
138 | 98 |
|
|
0 commit comments