This commit is contained in:
Sebastien 2018-03-09 16:09:58 +01:00
parent 72deefb166
commit de52decfd9

View File

@ -99,32 +99,17 @@ public class PlaylistManager {
UserEntity user = userUtils.getUserWithApiToken(userRepository, token); UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
PlaylistEntity playlist = getPlaylist(data.playlistId); PlaylistEntity playlist = getPlaylist(data.playlistId);
List<TrackEntity> tracks = trackRepository.findDistinctByPlaylistOrderByPos(playlist);
TrackEntity toDelete = trackRepository.findOne(data.id); TrackEntity toDelete = trackRepository.findOne(data.id);
if(toDelete == null){
logger.warn("Track not found in DB, id: "+ data.id); playlist = remove(playlist, toDelete);
if(playlist == null)
{
logger.warn("Playlist: " + data.playlistId + " Track: " + data.id);
return TRACK_NOT_FOUND; return TRACK_NOT_FOUND;
} }
int toDeleteIndex = tracks.indexOf(toDelete);
logger.debug("To delete index: " + toDeleteIndex);
if(toDeleteIndex == -1){
logger.warn("Track not found in playlist, id: " + data.id + " playlist: " + data.playlistId);
return TRACK_NOT_FOUND;
}
for(int i = toDeleteIndex + 1; i< tracks.size(); i++){
tracks.get(i).setPos(tracks.get(i).getPos() - 1);
trackRepository.save(tracks.get(i));
}
tracks.remove(toDelete);
trackRepository.delete(toDelete);
playlist.setTracks(tracks);
playlist = playlistRepository.save(playlist);
return new ResponseEntity<>(new PlaylistResponseData("Ok", playlist),HttpStatus.OK); return new ResponseEntity<>(new PlaylistResponseData("Ok", playlist),HttpStatus.OK);
@ -182,5 +167,37 @@ public class PlaylistManager {
} }
private PlaylistEntity remove(PlaylistEntity playlistEntity, TrackEntity trackEntity){
if(trackEntity == null){
logger.warn("Track not found in DB!");
return null;
}
List<TrackEntity> tracks = trackRepository.findDistinctByPlaylistOrderByPos(playlistEntity);
int toDeleteIndex = tracks.indexOf(trackEntity);
logger.debug("To delete index: " + toDeleteIndex);
if(toDeleteIndex == -1){
logger.warn("Track not found in playlist");
return null;
}
for(int i = toDeleteIndex + 1; i< tracks.size(); i++){
tracks.get(i).setPos(tracks.get(i).getPos() - 1);
trackRepository.save(tracks.get(i));
}
tracks.remove(trackEntity);
trackRepository.delete(trackEntity);
playlistEntity.setTracks(tracks);
playlistEntity = playlistRepository.save(playlistEntity);
return playlistEntity;
}
} }