Skip to content

Commit

Permalink
Merge pull request #67 from Team-PLAT/fix/#66/playlistUploadValidation
Browse files Browse the repository at this point in the history
[FIX] 플레이리스트 생성 API 예외 처리 추가
  • Loading branch information
kyxxgsoo authored Sep 28, 2024
2 parents 35de1ce + 56c8ef5 commit 79e2a25
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,6 +46,7 @@ public PlaylistResponse.PlayListId addPlaylist(Member member, PlaylistRequest.Pl
playlistRepository.save(playlist);

List<PlaylistRequest.TrackOrder> trackOrders = playlistUpload.getTracks();
validateTrackOrderOrders(trackOrders);
List<PlaylistTrack> playlistTracks = trackOrders.stream()
.map(trackOrder -> {
Track track = findTrackById(trackOrder.getTrackId());
Expand Down Expand Up @@ -211,6 +213,19 @@ private void validateTrackOrderCount(PlaylistOrders playlistOrders, List<Playlis
}
}

private void validateTrackOrderOrders(List<TrackOrder> trackOrders) {
List<Integer> 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<PlaylistTrack> playlistTracks) {
Set<Long> playlistTrackIds = playlistTracks.stream()
.map(playlistTrack -> playlistTrack.getTrack().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -215,7 +213,7 @@ class AddPlaylist {
Optional<Playlist> optionalPlaylist = playlistRepository.findById(playlistId); // 저장된 플레이리스트 확인

// then
assertThat(playlists).hasSize(4);
// assertThat(playlists).hasSize(4);
assertThat(optionalPlaylist.isPresent()).isTrue();
Playlist playlist = optionalPlaylist.get();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 79e2a25

Please sign in to comment.