Playlist updates
#6 Users can now add songs to defined playlist, display their playlists and display songs within their playlist
This commit is contained in:
		@@ -27,6 +27,8 @@
 | 
			
		||||
 */
 | 
			
		||||
package net.locusworks.discord.eighttrack.database.repos;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.springframework.data.jpa.repository.Query;
 | 
			
		||||
import org.springframework.data.repository.CrudRepository;
 | 
			
		||||
 | 
			
		||||
@@ -37,12 +39,18 @@ public interface GuildPlaylistRepository extends CrudRepository<GuildPlaylist, L
 | 
			
		||||
  
 | 
			
		||||
  GuildPlaylist findByGuildAndUserIdAndPlaylist(DiscordGuild guild, Long userId, String playlist);
 | 
			
		||||
  
 | 
			
		||||
  @Query("SELECT gpl FROM GuildPlaylist gpl WHERE gpl.guild.guildId = ?1 AND gpl.userId = ?2 AND gpl.playlist = ?3 ")
 | 
			
		||||
  @Query("SELECT gpl FROM GuildPlaylist gpl WHERE gpl.guild.guildId = ?1 AND gpl.userId = ?2 AND gpl.playlist = ?3")
 | 
			
		||||
  GuildPlaylist findByGuildAndUserIdAndPlaylist(Long guild, Long userId, String playlist);
 | 
			
		||||
  
 | 
			
		||||
  GuildPlaylist findByGuildAndUserId(DiscordGuild guild, Long userId);
 | 
			
		||||
  List<GuildPlaylist> findByGuildAndUserId(DiscordGuild guild, Long userId);
 | 
			
		||||
  
 | 
			
		||||
  @Query("SELECT gpl FROM GuildPlaylist gpl WHERE gpl.guild.guildId = ?1 AND gpl.userId = ?2 AND gpl.playlist = ?3 ")
 | 
			
		||||
  GuildPlaylist findByGuildAndUserId(Long guild, Long userId);
 | 
			
		||||
  @Query("SELECT gpl FROM GuildPlaylist gpl WHERE gpl.guild.guildId = ?1 AND gpl.userId = ?2")
 | 
			
		||||
  List<GuildPlaylist> findByGuildAndUserId(Long guild, Long userId);
 | 
			
		||||
  
 | 
			
		||||
  @Query("SELECT DISTINCT gpl FROM GuildPlaylist gpl LEFT JOIN FETCH gpl.guildPlaylistSongList WHERE gpl.guild.guildId = ?1 AND gpl.userId = ?2")
 | 
			
		||||
  List<GuildPlaylist> findByGuildAndUserIdFetchSongs(Long guild, Long userId);
 | 
			
		||||
  
 | 
			
		||||
  @Query("SELECT DISTINCT gpl FROM GuildPlaylist gpl LEFT JOIN FETCH gpl.guildPlaylistSongList WHERE gpl.guild.guildId = ?1 AND gpl.userId = ?2 AND gpl.playlist = ?3")
 | 
			
		||||
  GuildPlaylist findGuildUserPlaylistFetchSongs(Long guild, Long userId, String playlist);
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,7 @@ package net.locusworks.discord.eighttrack.database.repos;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.springframework.data.jpa.repository.Query;
 | 
			
		||||
import org.springframework.data.repository.CrudRepository;
 | 
			
		||||
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
 | 
			
		||||
@@ -38,5 +39,8 @@ public interface GuildPlaylistSongRepository extends CrudRepository<GuildPlaylis
 | 
			
		||||
  
 | 
			
		||||
  List<GuildPlaylistSong> findByGuildPlaylist(GuildPlaylist gpl);
 | 
			
		||||
  
 | 
			
		||||
  @Query("SELECT gpls FROM GuildPlaylistSong gpls WHERE gpls.guildPlaylist = ?1 AND gpls.guildSong.uuid IN ?2")
 | 
			
		||||
  List<GuildPlaylistSong> findByGuildPlaylistAndSongIds(GuildPlaylist gpl, List<String> songIds);
 | 
			
		||||
  
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -33,12 +33,16 @@ import java.math.BigInteger;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.time.OffsetDateTime;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicBoolean;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
import org.apache.commons.lang3.StringUtils;
 | 
			
		||||
 | 
			
		||||
@@ -59,6 +63,7 @@ import net.dv8tion.jda.api.managers.AudioManager;
 | 
			
		||||
import net.locusworks.crypto.utils.HashUtils;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.Song;
 | 
			
		||||
import net.locusworks.discord.eighttrack.scheduler.TrackScheduler;
 | 
			
		||||
@@ -433,10 +438,81 @@ public class GuildMusicHandler {
 | 
			
		||||
      case "add":
 | 
			
		||||
        addPlayList(event, commands);
 | 
			
		||||
        return;
 | 
			
		||||
      case "list":
 | 
			
		||||
        listPlayList(event, commands);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void listPlayList(GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
    if (commands == null || commands.isEmpty()) {
 | 
			
		||||
      List<GuildPlaylist> userPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong());
 | 
			
		||||
      if (userPlaylist.isEmpty()) {
 | 
			
		||||
        event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server").queue();
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      int longestPlaylist = 0;
 | 
			
		||||
      for(GuildPlaylist gpl : userPlaylist) {
 | 
			
		||||
        if (gpl.getPlaylist().length() > longestPlaylist) longestPlaylist = gpl.getPlaylist().length();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      String fmt = "%6s | %-" + longestPlaylist +"s | %s%n"; 
 | 
			
		||||
 | 
			
		||||
      StringBuilder sb = new StringBuilder("```");
 | 
			
		||||
      sb.append(String.format(fmt, "", "Playlist", "Song Count"));
 | 
			
		||||
      sb.append(StringUtils.repeat("-", 18 + longestPlaylist)).append("\n");
 | 
			
		||||
 | 
			
		||||
      int count = 0;
 | 
			
		||||
      for(GuildPlaylist gpl : userPlaylist) {
 | 
			
		||||
        sb.append(String.format(fmt, ++count, gpl.getPlaylist(), gpl.getGuildPlaylistSongList().size()));
 | 
			
		||||
      }
 | 
			
		||||
      sb.append("```");
 | 
			
		||||
      event.getChannel().sendMessage("**" + "Currently available playlists for " + event.getMember().getAsMention() + "**").queue((succcess)-> 
 | 
			
		||||
        event.getChannel().sendMessage(sb.toString()).queue()
 | 
			
		||||
      );
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    String playlist = commands.remove(0);
 | 
			
		||||
    
 | 
			
		||||
    GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findGuildUserPlaylistFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist);
 | 
			
		||||
    if (gpl == null) {
 | 
			
		||||
      event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server by the name of " + playlist).queue();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    if (gpl.getGuildPlaylistSongList().isEmpty()) {
 | 
			
		||||
      event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined songs for playlist " + playlist).queue();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    int longestSong = 0;
 | 
			
		||||
    int longestArtist = 0;
 | 
			
		||||
    for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
 | 
			
		||||
      GuildSong gs = gpls.getGuildSong();
 | 
			
		||||
      if (gs.getSong().getTitle().length() > longestSong) longestSong = gs.getSong().getTitle().length();
 | 
			
		||||
      if (gs.getSong().getArtist().length() > longestArtist) longestArtist = gs.getSong().getArtist().length();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String fmt = "%6s | %-" + longestSong +"s | %-" + longestArtist +"s | %s%n"; 
 | 
			
		||||
    int count = 0;
 | 
			
		||||
    StringBuilder sb = new StringBuilder("```");
 | 
			
		||||
    sb.append(String.format(fmt, "Track", "Title", "Artist", "id"));
 | 
			
		||||
    sb.append(StringUtils.repeat("-", 27 + longestSong + longestArtist)).append("\n");
 | 
			
		||||
 | 
			
		||||
    for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
 | 
			
		||||
      GuildSong gs = gpls.getGuildSong();
 | 
			
		||||
      sb.append(String.format(fmt, ++count, gs.getSong().getTitle(), gs.getSong().getArtist(), gs.getUuid()));
 | 
			
		||||
    }
 | 
			
		||||
    sb.append("```");
 | 
			
		||||
    event.getChannel().sendMessage("**" + "Current songs in playlists " + playlist + " for " + event.getMember().getAsMention() + "**").queue((succcess)->
 | 
			
		||||
      event.getChannel().sendMessage(sb.toString()).queue()
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void addPlayList(GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
    if (commands == null || commands.isEmpty()) {
 | 
			
		||||
      event.getChannel().sendMessage(event.getAuthor().getAsMention() + " you have to provide the playlist to create a new playlist (no spaces in name) and optionally a list of uuids for songs to add").queue();
 | 
			
		||||
@@ -460,7 +536,7 @@ public class GuildMusicHandler {
 | 
			
		||||
    if (gpl != null && commands.isEmpty()) {
 | 
			
		||||
      event.getChannel().sendMessage(event.getAuthor().getAsMention() + " a playlist with the name " + playlist + " already exist for you.").queue();
 | 
			
		||||
      return;
 | 
			
		||||
    } else {
 | 
			
		||||
    } else if (gpl == null) {
 | 
			
		||||
      newList = true;
 | 
			
		||||
      gpl = new GuildPlaylist();
 | 
			
		||||
      gpl.setDateAdded(new Date());
 | 
			
		||||
@@ -475,7 +551,52 @@ public class GuildMusicHandler {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Set<String> songIds = guildSongRepoService.getGuildPlaylistSongRepo()
 | 
			
		||||
        .findByGuildPlaylistAndSongIds(gpl, commands).stream()
 | 
			
		||||
        .map(g -> g.getGuildSong().getUuid())
 | 
			
		||||
        .collect(Collectors.toSet());
 | 
			
		||||
 | 
			
		||||
    List<GuildPlaylistSong> gplsList = new ArrayList<>();
 | 
			
		||||
    StringBuilder sb = new StringBuilder();
 | 
			
		||||
    for(Iterator<String> iter = commands.iterator(); iter.hasNext();) {
 | 
			
		||||
      String id = iter.next();
 | 
			
		||||
      GuildSong gs = guildSongRepoService.getGuildSongRepo().findByUuid(id);
 | 
			
		||||
      if (songIds.contains(id)) {
 | 
			
		||||
        sb.append(String.format("**%s** by __%s__ already exists in this playlist.%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
 | 
			
		||||
        iter.remove();
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (gs == null) {
 | 
			
		||||
        sb.append("Song with id `" + id + "` not found. Please check id\n");
 | 
			
		||||
        iter.remove();
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      GuildPlaylistSong gpls = new GuildPlaylistSong();
 | 
			
		||||
      gpls.setDateAdded(new Date());
 | 
			
		||||
      gpls.setGuildPlaylist(gpl);
 | 
			
		||||
      gpls.setGuildSong(gs);
 | 
			
		||||
      gplsList.add(gpls);
 | 
			
		||||
 | 
			
		||||
      sb.append(String.format("**%s** by __%s__ added to playlist.%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
 | 
			
		||||
      iter.remove();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!gplsList.isEmpty()) {
 | 
			
		||||
      guildSongRepoService.getGuildPlaylistSongRepo().saveAll(gplsList);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    MessageEmbed embed = new EmbedBuilder()
 | 
			
		||||
        .setTitle("Results for adding playlist " + playlist)
 | 
			
		||||
        .setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
 | 
			
		||||
        .setColor(Color.GREEN)
 | 
			
		||||
        .setDescription(sb.toString())
 | 
			
		||||
        .setFooter("", event.getGuild().getSelfMember().getUser().getAvatarUrl())
 | 
			
		||||
        .setTimestamp(OffsetDateTime.now())
 | 
			
		||||
        .build();
 | 
			
		||||
 | 
			
		||||
    event.getChannel().sendMessage(embed).queue();
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user