Start playlist api (create and add to plalist ready) and somme api change

This commit is contained in:
Sebastien 2018-03-08 18:21:13 +01:00
parent 812bdfc52b
commit 20e8750085
23 changed files with 557 additions and 92 deletions

View File

@ -0,0 +1,83 @@
package net.Broken.DB.Entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.annotation.Nullable;
import javax.persistence.*;
import javax.sound.midi.Track;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Entity
public class PlaylistEntity {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
private String name;
@JsonIgnore
@ManyToOne
@JoinColumn(name="userEntity_id", nullable=false)
private UserEntity user;
@OneToMany(mappedBy = "playlist")
private List<TrackEntity> tracks;
public PlaylistEntity() {
}
public PlaylistEntity(String name, UserEntity user) {
this.name = name;
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
public List<TrackEntity> getTracks() {
return tracks;
}
public void setTracks(List<TrackEntity> tracks) {
this.tracks = tracks;
}
public void addTracks(TrackEntity... tracks )
{
if(this.tracks == null)
this.tracks = new ArrayList<>();
this.tracks.addAll(Arrays.asList(tracks));
}
}

View File

@ -0,0 +1,86 @@
package net.Broken.DB.Entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import javax.persistence.*;
@Entity
public class TrackEntity {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
private String title;
private String url;
private String identifier;
private Integer pos;
@JsonIgnore
@ManyToOne
@JoinColumn(name="playlistEntity_id", nullable=false)
private PlaylistEntity playlist;
public TrackEntity() {
}
public TrackEntity(AudioTrackInfo trackInfo, int pos, PlaylistEntity playlist) {
this.title = trackInfo.title;
this.url = trackInfo.uri;
this.identifier = trackInfo.identifier;
this.playlist = playlist;
this.pos = pos;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public PlaylistEntity getPlaylist() {
return playlist;
}
public void setPlaylist(PlaylistEntity playlist) {
this.playlist = playlist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public Integer getPos() {
return pos;
}
public void setPos(Integer pos) {
this.pos = pos;
}
}

View File

@ -1,9 +1,11 @@
package net.Broken.DB.Entity; package net.Broken.DB.Entity;
import javax.persistence.Entity; import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.*;
import javax.persistence.Id; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* Entity for DB. Represent confirmed user account. * Entity for DB. Represent confirmed user account.
@ -20,8 +22,12 @@ public class UserEntity {
private String apiToken; private String apiToken;
@JsonIgnore
private String password; private String password;
@OneToMany(mappedBy = "user")
private List<PlaylistEntity> playlists;
public UserEntity() { public UserEntity() {
} }
@ -71,4 +77,19 @@ public class UserEntity {
public void setApiToken(String apiToken) { public void setApiToken(String apiToken) {
this.apiToken = apiToken; this.apiToken = apiToken;
} }
public List<PlaylistEntity> getPlaylists() {
return playlists;
}
public void setPlaylists(List<PlaylistEntity> playlists) {
this.playlists = playlists;
}
public void addPlaylist(PlaylistEntity... playlists){
if(this.playlists == null)
this.playlists = new ArrayList<>();
this.playlists.addAll(Arrays.asList(playlists));
}
} }

View File

@ -0,0 +1,7 @@
package net.Broken.DB.Repository;
import net.Broken.DB.Entity.PlaylistEntity;
import org.springframework.data.repository.CrudRepository;
public interface PlaylistRepository extends CrudRepository<PlaylistEntity, Integer> {
}

View File

@ -0,0 +1,11 @@
package net.Broken.DB.Repository;
import net.Broken.DB.Entity.PlaylistEntity;
import net.Broken.DB.Entity.TrackEntity;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface TrackRepository extends CrudRepository<TrackEntity, Integer> {
List<TrackEntity> findDistinctByPlaylistOrderByPos(PlaylistEntity playlistEntity);
}

View File

@ -14,6 +14,6 @@ import org.springframework.http.ResponseEntity;
public class Add implements CommandInterface { public class Add implements CommandInterface {
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
return new WebLoadUtils(musicCommande ,data, user).getResponse(); return new WebLoadUtils(data, user, true).getResponse();
} }
} }

View File

@ -18,13 +18,8 @@ public class AutoFlowOff implements CommandInterface{
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
AudioM audioM = AudioM.getInstance(null); AudioM audioM = AudioM.getInstance(null);
try {
TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler; TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler;
scheduler.setAutoFlow(false); scheduler.setAutoFlow(false);
return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
LogManager.getLogger().catching(nullMusicManager);
return new ResponseEntity<>(new CommandResponseData(data.command,"Not connected", "connect"), HttpStatus.INTERNAL_SERVER_ERROR);
}
} }
} }

View File

@ -18,14 +18,9 @@ public class AutoFlowOn implements CommandInterface{
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
AudioM audioM = AudioM.getInstance(null); AudioM audioM = AudioM.getInstance(null);
try {
TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler; TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler;
scheduler.setAutoFlow(true); scheduler.setAutoFlow(true);
return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
LogManager.getLogger().catching(nullMusicManager);
return new ResponseEntity<>(new CommandResponseData(data.command,"Not connected", "connect"), HttpStatus.INTERNAL_SERVER_ERROR);
}
} }
} }

View File

@ -17,15 +17,11 @@ public class Dell implements CommandInterface {
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
if(data.url != null) { if(data.url != null) {
try {
if(musicCommande.getAudioManager().getGuildMusicManager().scheduler.remove(data.url)){ if(musicCommande.getAudioManager().getGuildMusicManager().scheduler.remove(data.url)){
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
} }
else else
return new ResponseEntity<>(new CommandResponseData(data.command,"URL not found"), HttpStatus.NOT_FOUND); return new ResponseEntity<>(new CommandResponseData(data.command,"URL not found"), HttpStatus.NOT_FOUND);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
}
} }
return new ResponseEntity<>(new CommandResponseData(data.command, "Missing URL"), HttpStatus.NOT_ACCEPTABLE); return new ResponseEntity<>(new CommandResponseData(data.command, "Missing URL"), HttpStatus.NOT_ACCEPTABLE);

View File

@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
public class Flush implements CommandInterface { public class Flush implements CommandInterface {
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
try {
musicCommande.getAudioManager().getGuildMusicManager().scheduler.flush(); musicCommande.getAudioManager().getGuildMusicManager().scheduler.flush();
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
}
} }
} }

View File

@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
public class Next implements CommandInterface { public class Next implements CommandInterface {
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
try {
musicCommande.getAudioManager().getGuildMusicManager().scheduler.nextTrack(); musicCommande.getAudioManager().getGuildMusicManager().scheduler.nextTrack();
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
}
} }
} }

View File

@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
public class Pause implements CommandInterface { public class Pause implements CommandInterface {
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
try {
musicCommande.getAudioManager().getGuildMusicManager().scheduler.pause(); musicCommande.getAudioManager().getGuildMusicManager().scheduler.pause();
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
}
} }
} }

View File

@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
public class Play implements CommandInterface { public class Play implements CommandInterface {
@Override @Override
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) { public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
try {
musicCommande.getAudioManager().getGuildMusicManager().scheduler.resume(); musicCommande.getAudioManager().getGuildMusicManager().scheduler.resume();
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK); return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
}
} }
} }

View File

@ -0,0 +1,11 @@
package net.Broken.RestApi.Data.Playlist;
import net.Broken.RestApi.Data.CommandPostData;
public class AddToPlaylistData extends CommandPostData{
public int playlistId;
public int pos;
}

View File

@ -0,0 +1,5 @@
package net.Broken.RestApi.Data.Playlist;
public class CreatePlaylistData {
public String name;
}

View File

@ -0,0 +1,20 @@
package net.Broken.RestApi.Data.Playlist;
import com.fasterxml.jackson.annotation.JsonInclude;
import net.Broken.DB.Entity.PlaylistEntity;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PlaylistResponseData {
public String message;
public String error;
public PlaylistEntity playlist;
public PlaylistResponseData(String message, PlaylistEntity playlist) {
this.message = message;
this.playlist = playlist;
}
public PlaylistResponseData(String message, String error) {
this.message = message;
this.error = error;
}
}

View File

@ -46,7 +46,6 @@ public class MusicWebAPIController {
Music musicCommande = (Music) MainBot.commandes.get("music"); Music musicCommande = (Music) MainBot.commandes.get("music");
if(musicCommande.audio.getGuild().getAudioManager().isConnected()){ if(musicCommande.audio.getGuild().getAudioManager().isConnected()){
try {
AudioPlayer player = musicCommande.audio.getGuildMusicManager().player; AudioPlayer player = musicCommande.audio.getGuildMusicManager().player;
AudioTrack currentTrack = player.getPlayingTrack(); AudioTrack currentTrack = player.getPlayingTrack();
if(currentTrack == null) if(currentTrack == null)
@ -55,9 +54,6 @@ public class MusicWebAPIController {
} }
UserAudioTrackData uat = new UserAudioTrackData(musicCommande.audio.getGuildMusicManager().scheduler.getCurrentPlayingTrack()); UserAudioTrackData uat = new UserAudioTrackData(musicCommande.audio.getGuildMusicManager().scheduler.getCurrentPlayingTrack());
return new CurrentMusicData(uat, currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused(), musicCommande.audio.getGuildMusicManager().scheduler.isAutoFlow()); return new CurrentMusicData(uat, currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused(), musicCommande.audio.getGuildMusicManager().scheduler.isAutoFlow());
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new CurrentMusicData(null,0, "STOP",false, false);
}
}else }else
{ {
return new CurrentMusicData(null,0, "DISCONNECTED",false, false); return new CurrentMusicData(null,0, "DISCONNECTED",false, false);
@ -68,12 +64,8 @@ public class MusicWebAPIController {
public PlaylistData getPlaylist(){ public PlaylistData getPlaylist(){
Music musicCommande = (Music) MainBot.commandes.get("music"); Music musicCommande = (Music) MainBot.commandes.get("music");
List<UserAudioTrackData> list = null; List<UserAudioTrackData> list = null;
try {
list = musicCommande.getAudioManager().getGuildMusicManager().scheduler.getList(); list = musicCommande.getAudioManager().getGuildMusicManager().scheduler.getList();
return new PlaylistData(list); return new PlaylistData(list);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
return new PlaylistData(list);
}
} }
@RequestMapping(value = "/command", method = RequestMethod.POST) @RequestMapping(value = "/command", method = RequestMethod.POST)

View File

@ -0,0 +1,90 @@
package net.Broken.RestApi;
import net.Broken.DB.Entity.PlaylistEntity;
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 net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;
import net.Broken.RestApi.Data.Playlist.AddToPlaylistData;
import net.Broken.RestApi.Data.Playlist.CreatePlaylistData;
import net.Broken.RestApi.Data.Playlist.PlaylistResponseData;
import net.Broken.audio.Playlist.PlaylistManager;
import net.Broken.audio.WebLoadUtils;
import net.dv8tion.jda.core.entities.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/playlist/")
public class PlaylistAPIController {
private final
UserRepository userRepository;
private final
PlaylistRepository playlistRepository;
private final
TrackRepository trackRepository;
private Logger logger = LogManager.getLogger();
@Autowired
public PlaylistAPIController(UserRepository userRepository, PlaylistRepository playlistRepository, TrackRepository trackRepository) {
this.userRepository = userRepository;
this.playlistRepository = playlistRepository;
this.trackRepository = trackRepository;
}
@RequestMapping("/myPlaylist")
public List<PlaylistEntity> myPlaylist(@CookieValue(value = "token", defaultValue = "") String token){
if(token.isEmpty())
return null;
else{
UserEntity user = userRepository.findByApiToken(token).get(0);
return user.getPlaylists();
}
}
@RequestMapping("/createPlaylist")
public ResponseEntity<PlaylistResponseData> createPlaylist(@CookieValue(value = "token", defaultValue = "") String token, @RequestBody CreatePlaylistData data){
if(token.isEmpty())
return new ResponseEntity<>(new PlaylistResponseData("Unknown Token!\nPlease Re-connect.", "token"), HttpStatus.UNAUTHORIZED);
else{
UserEntity user = userRepository.findByApiToken(token).get(0);
PlaylistEntity playlistEntity = new PlaylistEntity(data.name, user);
playlistEntity = playlistRepository.save(playlistEntity);
user.addPlaylist(playlistEntity);
userRepository.save(user);
return new ResponseEntity<>(new PlaylistResponseData("Ok", playlistEntity), HttpStatus.OK);
}
}
@RequestMapping("/addToPlaylist")
public ResponseEntity<PlaylistResponseData> addToPlaylist(@CookieValue(value = "token", defaultValue = "") String token, @RequestBody AddToPlaylistData data){
PlaylistManager playlistManager = PlaylistManager.getINSTANCE();
return playlistManager.addToPlaylist(token, data);
}
}

View File

@ -0,0 +1,24 @@
package net.Broken;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContext implements ApplicationContextAware{
@Autowired
static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getAppContext(){
return context;
}
}

View File

@ -337,11 +337,9 @@ public class AudioM {
guild.getAudioManager().closeAudioConnection(); guild.getAudioManager().closeAudioConnection();
} }
public GuildMusicManager getGuildMusicManager() throws NullMusicManager, NotConnectedException { public GuildMusicManager getGuildMusicManager(){
if( musicManager == null) if( musicManager == null)
throw new NullMusicManager(); musicManager = getGuildAudioPlayer(guild);
else if( playedChanel == null)
throw new NotConnectedException();
return musicManager; return musicManager;
} }

View File

@ -0,0 +1,4 @@
package net.Broken.audio.Playlist.Exception;
public class PlaylistNotFoundException extends Exception {
}

View File

@ -0,0 +1,139 @@
package net.Broken.audio.Playlist;
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 net.Broken.RestApi.Data.CommandResponseData;
import net.Broken.RestApi.Data.Playlist.AddToPlaylistData;
import net.Broken.RestApi.Data.Playlist.PlaylistResponseData;
import net.Broken.SpringContext;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.UserUtils;
import net.Broken.audio.Playlist.Exception.PlaylistNotFoundException;
import net.Broken.audio.WebLoadUtils;
import net.dv8tion.jda.core.entities.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import sun.applet.Main;
import java.util.List;
public class PlaylistManager {
private final ResponseEntity<PlaylistResponseData> TOKEN_ERROR = new ResponseEntity<>(new PlaylistResponseData("Unknown Token!\nPlease Re-connect.", "token"), HttpStatus.UNAUTHORIZED);
private final ResponseEntity<PlaylistResponseData> PLAYLIST_NOT_FOUND = new ResponseEntity<>(new PlaylistResponseData("Playlist not found", "playlist"), HttpStatus.NOT_FOUND);
private final ResponseEntity<PlaylistResponseData> TRACK_NOT_FOUND = new ResponseEntity<>(new PlaylistResponseData("Can't find media!", "track"), HttpStatus.NOT_FOUND);
private PlaylistRepository playlistRepository;
private TrackRepository trackRepository;
private UserRepository userRepository;
Logger logger = LogManager.getLogger();
private static PlaylistManager INSTANCE = new PlaylistManager();
private PlaylistManager() {
ApplicationContext context = SpringContext.getAppContext();
playlistRepository = (PlaylistRepository) context.getBean("playlistRepository");
trackRepository = (TrackRepository) context.getBean("trackRepository");
userRepository = (UserRepository) context.getBean("userRepository");
}
public static PlaylistManager getINSTANCE() {
return INSTANCE;
}
public ResponseEntity<PlaylistResponseData> addToPlaylist(String token, AddToPlaylistData data) {
UserUtils userUtils = UserUtils.getInstance();
try {
UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
PlaylistEntity playlist = getPlaylist(data.playlistId);
User jdaUser = MainBot.jda.getUserById(user.getJdaId());
WebLoadUtils webLoadUtils = new WebLoadUtils(data, jdaUser, false);
webLoadUtils.getResponse();
if(webLoadUtils.userAudioTrack == null){
return TRACK_NOT_FOUND;
}
else
{
TrackEntity trackEntity = new TrackEntity(webLoadUtils.userAudioTrack.getAudioTrack().getInfo(), data.pos, playlist);
playlist = insert(playlist, trackEntity);
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)
throw new PlaylistNotFoundException();
else
return playlist;
}
private PlaylistEntity insert(PlaylistEntity playlistEntity, TrackEntity trackEntity){
List<TrackEntity> tracks = trackRepository.findDistinctByPlaylistOrderByPos(playlistEntity);
boolean increase = false;
for(TrackEntity track : tracks){
if(track.getPos().equals(trackEntity.getPos())){
logger.debug("Need re-organisation");
increase = true;
}
if(increase){
track.setPos(track.getPos() + 1);
trackRepository.save(track);
}
}
if(!increase)
{
trackEntity.setPos(tracks.size());
}
trackRepository.save(trackEntity);
playlistEntity.addTracks(trackEntity);
return playlistRepository.save(playlistEntity);
}
}

View File

@ -5,7 +5,6 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import net.Broken.Commands.Music;
import net.Broken.RestApi.Data.CommandPostData; import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData; import net.Broken.RestApi.Data.CommandResponseData;
import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.entities.User;
@ -18,47 +17,52 @@ import org.springframework.http.ResponseEntity;
* Interface between WebApi and Music bot for submitting track * Interface between WebApi and Music bot for submitting track
*/ */
public class WebLoadUtils { public class WebLoadUtils {
ResponseEntity<CommandResponseData> response; private ResponseEntity<CommandResponseData> response;
Logger logger = LogManager.getLogger(); private Logger logger = LogManager.getLogger();
public UserAudioTrack userAudioTrack;
/** /**
* Submit a track or playlist to Music bot * Submit a track or playlist to Music bot
* @param musicCommand The current guild music command.
* @param data Received data from API * @param data Received data from API
* @param user User who submit the track * @param user User who submit the track
*/ */
public WebLoadUtils(Music musicCommand, CommandPostData data, User user){ public WebLoadUtils(CommandPostData data, User user, boolean submit){
AudioPlayerManager playerM = musicCommand.getAudioManager().getPlayerManager(); AudioPlayerManager playerM = AudioM.getInstance(null).getPlayerManager();
try { try {
AudioM audioM = AudioM.getInstance(null); AudioM audioM = AudioM.getInstance(null);
playerM.loadItemOrdered(musicCommand.getAudioManager().getGuildMusicManager(), data.url, new AudioLoadResultHandler() { playerM.loadItemOrdered(audioM.getGuildMusicManager(), data.url, new AudioLoadResultHandler() {
@Override @Override
public void trackLoaded(AudioTrack track) { public void trackLoaded(AudioTrack track) {
logger.info("Single Track detected from web!"); logger.info("Single Track detected from web!");
try { userAudioTrack = new UserAudioTrack(user, track); //TODO
UserAudioTrack userAudioTrack = new UserAudioTrack(user, track); //TODO if(submit)
audioM.play(audioM.getGuild(), audioM.getPlayedChanel(), audioM.getGuildMusicManager(), userAudioTrack, data.onHead); audioM.play(audioM.getGuild(), audioM.getPlayedChanel(), audioM.getGuildMusicManager(), userAudioTrack, data.onHead);
response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK); response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK);
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
nullMusicManager.printStackTrace();
}
} }
@Override @Override
public void playlistLoaded(AudioPlaylist playlist) { public void playlistLoaded(AudioPlaylist playlist) {
if(submit)
{
logger.info("Playlist detected from web! Limit: " + data.playlistLimit); logger.info("Playlist detected from web! Limit: " + data.playlistLimit);
audioM.playListLoader(playlist, data.playlistLimit, user, data.onHead); audioM.playListLoader(playlist, data.playlistLimit, user, data.onHead);
response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK); response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK);
}else
{
response = new ResponseEntity<>(new CommandResponseData("ADD", "Adding a list on saved playlist is currently not supported"), HttpStatus.NOT_ACCEPTABLE);
}
} }
@Override @Override
public void noMatches() { public void noMatches() {
logger.warn("Cant find media ! (web)"); logger.warn("Cant find media ! (web) url: "+ data.url);
response = new ResponseEntity<>(new CommandResponseData("ADD", "Can't find media!"), HttpStatus.NOT_FOUND); response = new ResponseEntity<>(new CommandResponseData("ADD", "Can't find media!"), HttpStatus.NOT_FOUND);
} }
@ -73,7 +77,7 @@ public class WebLoadUtils {
while(response == null) while(response == null)
Thread.sleep(10); Thread.sleep(10);
} catch (NullMusicManager | NotConnectedException | InterruptedException nullMusicManager) { } catch (InterruptedException nullMusicManager) {
nullMusicManager.printStackTrace(); nullMusicManager.printStackTrace();
} }
} }