Adding move track on playlist api

This commit is contained in:
Sebastien 2018-03-19 15:24:50 +01:00
parent de52decfd9
commit 5bc467d941
6 changed files with 108 additions and 5 deletions

View File

@ -36,6 +36,14 @@ public class TrackEntity {
this.pos = pos;
}
public TrackEntity(TrackEntity trackEntity){
this.title = trackEntity.title;
this.url = trackEntity.url;
this.identifier = trackEntity.identifier;
this.pos = trackEntity.pos;
this.playlist = trackEntity.playlist;
}
public Integer getId() {
return id;
}

View File

@ -0,0 +1,10 @@
package net.Broken.RestApi.Data.Playlist;
public class MoveTrackData {
public int playlistId;
public int id;
public int newPos;
}

View File

@ -1,15 +1,43 @@
package net.Broken.RestApi;
import net.Broken.DB.Entity.PlaylistEntity;
import net.Broken.DB.Entity.TrackEntity;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.PlaylistRepository;
import net.Broken.DB.Repository.TrackRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/")
public class GeneralApiController {
private final
UserRepository userRepository;
private final
PlaylistRepository playlistRepository;
private final
TrackRepository trackRepository;
@Autowired
public GeneralApiController(UserRepository userRepository, PlaylistRepository playlistRepository, TrackRepository trackRepository) {
this.userRepository = userRepository;
this.playlistRepository = playlistRepository;
this.trackRepository = trackRepository;
}
@RequestMapping(value = "/isReady", method = RequestMethod.GET)
public ResponseEntity<String> isReady(){
if(MainBot.ready){

View File

@ -6,10 +6,7 @@ import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.PlaylistRepository;
import net.Broken.DB.Repository.TrackRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.RestApi.Data.Playlist.AddToPlaylistData;
import net.Broken.RestApi.Data.Playlist.CreatePlaylistData;
import net.Broken.RestApi.Data.Playlist.DeleteTrackData;
import net.Broken.RestApi.Data.Playlist.PlaylistResponseData;
import net.Broken.RestApi.Data.Playlist.*;
import net.Broken.audio.Playlist.PlaylistManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -90,5 +87,15 @@ public class PlaylistAPIController {
}
@RequestMapping("/moveTrack")
public ResponseEntity<PlaylistResponseData> moveTrack(@CookieValue(value = "token", defaultValue = "") String token, @RequestBody MoveTrackData data){
PlaylistManager playlistManager = PlaylistManager.getINSTANCE();
return playlistManager.moveTrack(token, data);
}
}

View File

@ -10,6 +10,7 @@ import net.Broken.MainBot;
import net.Broken.RestApi.Data.CommandResponseData;
import net.Broken.RestApi.Data.Playlist.AddToPlaylistData;
import net.Broken.RestApi.Data.Playlist.DeleteTrackData;
import net.Broken.RestApi.Data.Playlist.MoveTrackData;
import net.Broken.RestApi.Data.Playlist.PlaylistResponseData;
import net.Broken.SpringContext;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
@ -110,7 +111,7 @@ public class PlaylistManager {
logger.warn("Playlist: " + data.playlistId + " Track: " + data.id);
return TRACK_NOT_FOUND;
}
return new ResponseEntity<>(new PlaylistResponseData("Ok", playlist),HttpStatus.OK);
} catch (UnknownTokenException e) {
@ -122,9 +123,43 @@ public class PlaylistManager {
}
}
public ResponseEntity<PlaylistResponseData> moveTrack(String token, MoveTrackData data) {
UserUtils userUtils = UserUtils.getInstance();
try {
UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
PlaylistEntity playlist = getPlaylist(data.playlistId);
TrackEntity toMove = trackRepository.findOne(data.id);
TrackEntity save = new TrackEntity(toMove);
playlist = remove(playlist, toMove);
if(playlist == null)
{
logger.warn("Playlist: " + data.playlistId + " Track: " + data.id);
return TRACK_NOT_FOUND;
}
save.setPos(data.newPos);
playlist = insert(playlist, save);
return new ResponseEntity<>(new PlaylistResponseData("Ok", playlist),HttpStatus.OK);
} catch (UnknownTokenException e) {
logger.warn("Unknown token: "+ token);
return TOKEN_ERROR;
} catch (PlaylistNotFoundException e) {
logger.debug("Playlist not found: "+ data.playlistId);
return PLAYLIST_NOT_FOUND;
}
}
private PlaylistEntity getPlaylist(int id) throws PlaylistNotFoundException{
PlaylistEntity playlist = playlistRepository.findOne(id);
if(playlist == null)

View File

@ -1,14 +1,25 @@
package net.Broken.webView;
import net.Broken.DB.Entity.PlaylistEntity;
import net.Broken.DB.Entity.TrackEntity;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.RestApi.Commands.Play;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
/**
* Web page controller for index
*/
@Controller
public class GeneralWebView {
@RequestMapping("/")
public String music(Model model){
@ -18,4 +29,8 @@ public class GeneralWebView {
public String loading(Model model){
return "loading";
}
}