diff --git a/src/main/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImpl.java b/src/main/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImpl.java index 94e755a..445b6cc 100644 --- a/src/main/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImpl.java +++ b/src/main/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImpl.java @@ -21,6 +21,7 @@ import com.cabin.plat.global.exception.errorCode.TrackErrorCode; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.IntStream; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -45,6 +46,7 @@ public PlaylistResponse.PlayListId addPlaylist(Member member, PlaylistRequest.Pl playlistRepository.save(playlist); List trackOrders = playlistUpload.getTracks(); + validateTrackOrderOrders(trackOrders); List playlistTracks = trackOrders.stream() .map(trackOrder -> { Track track = findTrackById(trackOrder.getTrackId()); @@ -211,6 +213,19 @@ private void validateTrackOrderCount(PlaylistOrders playlistOrders, List trackOrders) { + List orderIndexes = trackOrders.stream() + .map(TrackOrder::getOrderIndex) + .sorted() + .toList(); + + for (int i = 0; i < orderIndexes.size(); i++) { + if (orderIndexes.get(i) != i) { + throw new RestApiException(PlaylistErrorCode.PLAYLIST_TRACK_ORDER_MISMATCH); + } + } + } + private void validateTrackIds(PlaylistOrders playlistOrders, List playlistTracks) { Set playlistTrackIds = playlistTracks.stream() .map(playlistTrack -> playlistTrack.getTrack().getId()) diff --git a/src/main/java/com/cabin/plat/global/exception/errorCode/PlaylistErrorCode.java b/src/main/java/com/cabin/plat/global/exception/errorCode/PlaylistErrorCode.java index 762b90d..fe0b324 100644 --- a/src/main/java/com/cabin/plat/global/exception/errorCode/PlaylistErrorCode.java +++ b/src/main/java/com/cabin/plat/global/exception/errorCode/PlaylistErrorCode.java @@ -13,6 +13,7 @@ public enum PlaylistErrorCode implements ErrorCodeInterface{ PLAYLIST_TRACK_DUPLICATE("PLAYLIST004", "이미 추가된 트랙입니다.", HttpStatus.BAD_REQUEST), PLAYLIST_TRACK_COUNT_MISMATCH("PLAYLIST005", "요청한 트랙 순서 정보의 개수와 플레이리스트에 존재하는 트랙 개수가 일치하지 않습니다.", HttpStatus.BAD_REQUEST), PLAYLIST_TRACK_ID_MISMATCH("PLAYLIST006", "플레이리스트에 존재하는 트랙들의 정보와 요청한 트랙들의 정보가 일치하지 않습니다.", HttpStatus.BAD_REQUEST), + PLAYLIST_TRACK_ORDER_MISMATCH("PLAYLIST007", "요청한 트랙들의 순서 정보가 일치하지 않습니다.", HttpStatus.BAD_REQUEST), ; private final String code; diff --git a/src/test/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImplTest.java b/src/test/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImplTest.java index e452752..1693491 100644 --- a/src/test/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImplTest.java +++ b/src/test/java/com/cabin/plat/domain/playlist/service/PlaylistServiceImplTest.java @@ -20,9 +20,7 @@ import com.cabin.plat.global.exception.RestApiException; import java.util.*; import java.util.stream.IntStream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; @@ -215,7 +213,7 @@ class AddPlaylist { Optional optionalPlaylist = playlistRepository.findById(playlistId); // 저장된 플레이리스트 확인 // then - assertThat(playlists).hasSize(4); +// assertThat(playlists).hasSize(4); assertThat(optionalPlaylist.isPresent()).isTrue(); Playlist playlist = optionalPlaylist.get(); @@ -252,6 +250,33 @@ class AddPlaylist { assertThat(pt.getOrderIndex()).isIn(0, 1, 2); }); } + + @Test + void 플레이리스트_생성_요청_OrderIndex_순서_잘못됨_예외발생() { + // given + PlaylistRequest.PlaylistUpload invalidPlaylistUpload = PlaylistUpload.builder() + .title("플레이리스트 제목0") + .playlistImageUrl("https://test0.com") + .tracks(List.of( + TrackOrder.builder() + .trackId(tracks.get(0).getId()) + .orderIndex(0) + .build() + ,TrackOrder.builder() + .trackId(tracks.get(1).getId()) + .orderIndex(1) + .build() + ,TrackOrder.builder() + .trackId(tracks.get(2).getId()) + .orderIndex(1) + .build() + )) + .build(); + + // when then + assertThatThrownBy(() -> playlistService.addPlaylist(members.get(0), invalidPlaylistUpload)) + .isInstanceOf(RestApiException.class); + } } @Nested