From 5bc467d94157f76e49275d6b0ca073a734045f21 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 19 Mar 2018 15:24:50 +0100 Subject: [PATCH] Adding move track on playlist api --- .../net/Broken/DB/Entity/TrackEntity.java | 8 ++++ .../RestApi/Data/Playlist/MoveTrackData.java | 10 +++++ .../Broken/RestApi/GeneralApiController.java | 28 ++++++++++++++ .../Broken/RestApi/PlaylistAPIController.java | 15 ++++++-- .../audio/Playlist/PlaylistManager.java | 37 ++++++++++++++++++- .../net/Broken/webView/GeneralWebView.java | 15 ++++++++ 6 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/Broken/RestApi/Data/Playlist/MoveTrackData.java diff --git a/src/main/java/net/Broken/DB/Entity/TrackEntity.java b/src/main/java/net/Broken/DB/Entity/TrackEntity.java index 31e7dc6..caa8fc2 100644 --- a/src/main/java/net/Broken/DB/Entity/TrackEntity.java +++ b/src/main/java/net/Broken/DB/Entity/TrackEntity.java @@ -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; } diff --git a/src/main/java/net/Broken/RestApi/Data/Playlist/MoveTrackData.java b/src/main/java/net/Broken/RestApi/Data/Playlist/MoveTrackData.java new file mode 100644 index 0000000..950affb --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/Playlist/MoveTrackData.java @@ -0,0 +1,10 @@ +package net.Broken.RestApi.Data.Playlist; + +public class MoveTrackData { + + public int playlistId; + + public int id; + + public int newPos; +} diff --git a/src/main/java/net/Broken/RestApi/GeneralApiController.java b/src/main/java/net/Broken/RestApi/GeneralApiController.java index ae994bc..96f73bf 100644 --- a/src/main/java/net/Broken/RestApi/GeneralApiController.java +++ b/src/main/java/net/Broken/RestApi/GeneralApiController.java @@ -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 isReady(){ if(MainBot.ready){ diff --git a/src/main/java/net/Broken/RestApi/PlaylistAPIController.java b/src/main/java/net/Broken/RestApi/PlaylistAPIController.java index 3614865..9a428c4 100644 --- a/src/main/java/net/Broken/RestApi/PlaylistAPIController.java +++ b/src/main/java/net/Broken/RestApi/PlaylistAPIController.java @@ -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 moveTrack(@CookieValue(value = "token", defaultValue = "") String token, @RequestBody MoveTrackData data){ + PlaylistManager playlistManager = PlaylistManager.getINSTANCE(); + + return playlistManager.moveTrack(token, data); + + } + + + } diff --git a/src/main/java/net/Broken/audio/Playlist/PlaylistManager.java b/src/main/java/net/Broken/audio/Playlist/PlaylistManager.java index c033aa3..a70b713 100644 --- a/src/main/java/net/Broken/audio/Playlist/PlaylistManager.java +++ b/src/main/java/net/Broken/audio/Playlist/PlaylistManager.java @@ -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 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) diff --git a/src/main/java/net/Broken/webView/GeneralWebView.java b/src/main/java/net/Broken/webView/GeneralWebView.java index 0f7287d..71616ad 100644 --- a/src/main/java/net/Broken/webView/GeneralWebView.java +++ b/src/main/java/net/Broken/webView/GeneralWebView.java @@ -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"; } + + + + }