From 66b0d11415a056dae2f5a25f2c5eb742d13da972 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 2 Oct 2016 03:00:03 +0200 Subject: [PATCH] playlist stuff --- ...er.cs => 20161001173937_first.Designer.cs} | 49 +++++++++++++++++- ...01917_first.cs => 20161001173937_first.cs} | 50 +++++++++++++++++++ src/NadekoBot/Modules/Music/Music.cs | 44 +++++++++------- .../Services/Database/Models/MusicPlaylist.cs | 16 ++++++ .../Services/Database/Models/PlaylistSong.cs | 18 +++++++ .../Services/Database/NadekoContext.cs | 13 ++++- .../Repositories/IMusicPlaylistRepository.cs | 15 ++++++ .../Impl/MusicPlaylistRepository.cs | 32 ++++++++++++ 8 files changed, 217 insertions(+), 20 deletions(-) rename src/NadekoBot/Migrations/{20160930001917_first.Designer.cs => 20161001173937_first.Designer.cs} (92%) rename src/NadekoBot/Migrations/{20160930001917_first.cs => 20161001173937_first.cs} (92%) create mode 100644 src/NadekoBot/Services/Database/Models/MusicPlaylist.cs create mode 100644 src/NadekoBot/Services/Database/Models/PlaylistSong.cs create mode 100644 src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs create mode 100644 src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs diff --git a/src/NadekoBot/Migrations/20160930001917_first.Designer.cs b/src/NadekoBot/Migrations/20161001173937_first.Designer.cs similarity index 92% rename from src/NadekoBot/Migrations/20160930001917_first.Designer.cs rename to src/NadekoBot/Migrations/20161001173937_first.Designer.cs index dd4fabf1..6647dc9e 100644 --- a/src/NadekoBot/Migrations/20160930001917_first.Designer.cs +++ b/src/NadekoBot/Migrations/20161001173937_first.Designer.cs @@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl; namespace NadekoBot.Migrations { [DbContext(typeof(NadekoSqliteContext))] - [Migration("20160930001917_first")] + [Migration("20161001173937_first")] partial class first { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -357,6 +357,22 @@ namespace NadekoBot.Migrations b.ToTable("ModulePrefixes"); }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.MusicPlaylist", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Author"); + + b.Property("AuthorId"); + + b.Property("Name"); + + b.HasKey("Id"); + + b.ToTable("MusicPlaylists"); + }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.Permission", b => { b.Property("Id") @@ -398,6 +414,30 @@ namespace NadekoBot.Migrations b.ToTable("PlayingStatus"); }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.PlaylistSong", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("MusicPlaylistId"); + + b.Property("Provider"); + + b.Property("ProviderType"); + + b.Property("Query"); + + b.Property("Title"); + + b.Property("Uri"); + + b.HasKey("Id"); + + b.HasIndex("MusicPlaylistId"); + + b.ToTable("PlaylistSong"); + }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b => { b.Property("Id") @@ -589,6 +629,13 @@ namespace NadekoBot.Migrations .HasForeignKey("BotConfigId"); }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.PlaylistSong", b => + { + b.HasOne("NadekoBot.Services.Database.Models.MusicPlaylist") + .WithMany("Songs") + .HasForeignKey("MusicPlaylistId"); + }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.RaceAnimal", b => { b.HasOne("NadekoBot.Services.Database.Models.BotConfig") diff --git a/src/NadekoBot/Migrations/20160930001917_first.cs b/src/NadekoBot/Migrations/20161001173937_first.cs similarity index 92% rename from src/NadekoBot/Migrations/20160930001917_first.cs rename to src/NadekoBot/Migrations/20161001173937_first.cs index 400ac665..2cf892c5 100644 --- a/src/NadekoBot/Migrations/20160930001917_first.cs +++ b/src/NadekoBot/Migrations/20161001173937_first.cs @@ -122,6 +122,21 @@ namespace NadekoBot.Migrations table.PrimaryKey("PK_LogSettings", x => x.Id); }); + migrationBuilder.CreateTable( + name: "MusicPlaylists", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Autoincrement", true), + Author = table.Column(nullable: true), + AuthorId = table.Column(nullable: false), + Name = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_MusicPlaylists", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Permission", columns: table => new @@ -390,6 +405,30 @@ namespace NadekoBot.Migrations onDelete: ReferentialAction.Restrict); }); + migrationBuilder.CreateTable( + name: "PlaylistSong", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Autoincrement", true), + MusicPlaylistId = table.Column(nullable: true), + Provider = table.Column(nullable: true), + ProviderType = table.Column(nullable: false), + Query = table.Column(nullable: true), + Title = table.Column(nullable: true), + Uri = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_PlaylistSong", x => x.Id); + table.ForeignKey( + name: "FK_PlaylistSong_MusicPlaylists_MusicPlaylistId", + column: x => x.MusicPlaylistId, + principalTable: "MusicPlaylists", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + migrationBuilder.CreateTable( name: "GuildConfigs", columns: table => new @@ -535,6 +574,11 @@ namespace NadekoBot.Migrations table: "PlayingStatus", column: "BotConfigId"); + migrationBuilder.CreateIndex( + name: "IX_PlaylistSong_MusicPlaylistId", + table: "PlaylistSong", + column: "MusicPlaylistId"); + migrationBuilder.CreateIndex( name: "IX_RaceAnimals_BotConfigId", table: "RaceAnimals", @@ -588,6 +632,9 @@ namespace NadekoBot.Migrations migrationBuilder.DropTable( name: "PlayingStatus"); + migrationBuilder.DropTable( + name: "PlaylistSong"); + migrationBuilder.DropTable( name: "Quotes"); @@ -612,6 +659,9 @@ namespace NadekoBot.Migrations migrationBuilder.DropTable( name: "GuildConfigs"); + migrationBuilder.DropTable( + name: "MusicPlaylists"); + migrationBuilder.DropTable( name: "BotConfig"); diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index ec7e7373..926bc8e3 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -571,7 +571,7 @@ namespace NadekoBot.Modules.Music if (mpl == null) { - await channel.SendMessageAsync("Can't find playlist with that ID").ConfigureAwait(false); + await channel.SendMessageAsync("`Can't find playlist with that ID`").ConfigureAwait(false); return; } @@ -619,26 +619,34 @@ namespace NadekoBot.Modules.Music bool success = false; MusicPlaylist pl = null; - using (var uow = DbHandler.UnitOfWork()) + try { - pl = uow.MusicPlaylists.Get(id); - - if (pl != null) + using (var uow = DbHandler.UnitOfWork()) { - if (NadekoBot.Credentials.IsOwner(umsg.Author) || pl.AuthorId == umsg.Author.Id) - { - uow.MusicPlaylists.Remove(pl.Id); - await uow.CompleteAsync().ConfigureAwait(false); - } - else - success = false; - } - } + pl = uow.MusicPlaylists.Get(id); - if (success) - await channel.SendMessageAsync("Failed to delete that playlist. It either doesn't exist, or you are not its author.").ConfigureAwait(false); - else - await channel.SendMessageAsync("`Playlist successfully deleted.`").ConfigureAwait(false); + if (pl != null) + { + if (NadekoBot.Credentials.IsOwner(umsg.Author) || pl.AuthorId == umsg.Author.Id) + { + uow.MusicPlaylists.Remove(pl); + await uow.CompleteAsync().ConfigureAwait(false); + success = true; + } + else + success = false; + } + } + + if (!success) + await channel.SendMessageAsync("Failed to delete that playlist. It either doesn't exist, or you are not its author.").ConfigureAwait(false); + else + await channel.SendMessageAsync("`Playlist successfully deleted.`").ConfigureAwait(false); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } } [LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias] diff --git a/src/NadekoBot/Services/Database/Models/MusicPlaylist.cs b/src/NadekoBot/Services/Database/Models/MusicPlaylist.cs new file mode 100644 index 00000000..d14d3c01 --- /dev/null +++ b/src/NadekoBot/Services/Database/Models/MusicPlaylist.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Database.Models +{ + public class MusicPlaylist : DbEntity + { + public string Name { get; set; } + public string Author { get; set; } + public ulong AuthorId { get; set; } + public List Songs { get; set; } + } +} diff --git a/src/NadekoBot/Services/Database/Models/PlaylistSong.cs b/src/NadekoBot/Services/Database/Models/PlaylistSong.cs new file mode 100644 index 00000000..166bedd0 --- /dev/null +++ b/src/NadekoBot/Services/Database/Models/PlaylistSong.cs @@ -0,0 +1,18 @@ +using NadekoBot.Modules.Music.Classes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Database.Models +{ + public class PlaylistSong : DbEntity + { + public string Provider { get; set; } + public MusicType ProviderType { get; set; } + public string Title { get; set; } + public string Uri { get; set; } + public string Query { get; set; } + } +} diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index ea4242f2..bb8b3087 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -187,7 +187,7 @@ namespace NadekoBot.Services.Database permissionEntity .HasOne(p => p.Next) .WithOne(p => p.Previous); - + #endregion #region LogSettings @@ -203,6 +203,17 @@ namespace NadekoBot.Services.Database // .HasMany(ls => ls.IgnoredVoicePresenceChannelIds) // .WithOne(ls => ls.LogSetting); #endregion + + #region MusicPlaylists + var musicPlaylistEntity = modelBuilder.Entity(); + + musicPlaylistEntity + .HasMany(p => p.Songs) + .WithOne() + .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Cascade); + + + #endregion } protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder); } diff --git a/src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs b/src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs new file mode 100644 index 00000000..58c870c8 --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs @@ -0,0 +1,15 @@ +using NadekoBot.Services.Database.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Database.Repositories +{ + public interface IMusicPlaylistRepository : IRepository + { + List GetPlaylistsOnPage(int num); + MusicPlaylist GetWithSongs(int id); + } +} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs new file mode 100644 index 00000000..572461ce --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs @@ -0,0 +1,32 @@ +using NadekoBot.Services.Database.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace NadekoBot.Services.Database.Repositories.Impl +{ + public class MusicPlaylistRepository : Repository, IMusicPlaylistRepository + { + public MusicPlaylistRepository(DbContext context) : base(context) + { + } + + public List GetPlaylistsOnPage(int num) + { + if (num < 1) + throw new IndexOutOfRangeException(); + + return _set.Skip((num - 1) * 20) + .Take(20) + .Include(pl => pl.Songs) + .ToList(); + } + + public MusicPlaylist GetWithSongs(int id) => + _set.Include(mpl => mpl.Songs) + .FirstOrDefault(mpl => mpl.Id == id); + } +}