diff --git a/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs b/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs index cf033bb23..fb7bfe5df 100644 --- a/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs +++ b/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs @@ -13,6 +13,7 @@ using NadekoBot.Core.Common.TypeReaders.Models; using NadekoBot.Core.Services; using NadekoBot.Core.Services.Database.Models; using NadekoBot.Extensions; +using NadekoBot.Modules.Administration.Common; using NadekoBot.Modules.Permissions.Services; using Newtonsoft.Json; using Serilog; @@ -63,12 +64,13 @@ namespace NadekoBot.Modules.Administration.Services ps = uow.GuildConfigs.ForId(guildId, set => set.Include(x => x.WarnPunishments)) .WarnPunishments; - warnings += uow.Warnings + warnings += uow._context + .Warnings .ForId(guildId, userId) .Where(w => !w.Forgiven && w.UserId == userId) .Count(); - uow.Warnings.Add(warn); + uow._context.Warnings.Add(warn); uow.SaveChanges(); } @@ -244,7 +246,7 @@ WHERE GuildId={guildId} { using (var uow = _db.GetDbContext()) { - return uow.Warnings.GetForGuild(gid).GroupBy(x => x.UserId).ToArray(); + return uow._context.Warnings.GetForGuild(gid).GroupBy(x => x.UserId).ToArray(); } } @@ -252,7 +254,7 @@ WHERE GuildId={guildId} { using (var uow = _db.GetDbContext()) { - return uow.Warnings.ForId(gid, userId); + return uow._context.Warnings.ForId(gid, userId); } } @@ -263,11 +265,11 @@ WHERE GuildId={guildId} { if (index == 0) { - await uow.Warnings.ForgiveAll(guildId, userId, moderator); + await uow._context.Warnings.ForgiveAll(guildId, userId, moderator); } else { - toReturn = uow.Warnings.Forgive(guildId, userId, moderator, index - 1); + toReturn = uow._context.Warnings.Forgive(guildId, userId, moderator, index - 1); } uow.SaveChanges(); } diff --git a/src/NadekoBot/Modules/Music/PlaylistCommands.cs b/src/NadekoBot/Modules/Music/PlaylistCommands.cs index eaa31b6a2..36068520f 100644 --- a/src/NadekoBot/Modules/Music/PlaylistCommands.cs +++ b/src/NadekoBot/Modules/Music/PlaylistCommands.cs @@ -10,6 +10,7 @@ using NadekoBot.Core.Services; using NadekoBot.Core.Services.Database.Models; using NadekoBot.Extensions; using NadekoBot.Modules; +using NadekoBot.Modules.Music; using NadekoBot.Modules.Music.Services; using Serilog; @@ -55,7 +56,7 @@ namespace NadekoBot.Core.Modules.Music using (var uow = _db.GetDbContext()) { - playlists = uow.MusicPlaylists.GetPlaylistsOnPage(num); + playlists = uow._context.MusicPlaylists.GetPlaylistsOnPage(num); } var embed = new EmbedBuilder() @@ -75,13 +76,13 @@ namespace NadekoBot.Core.Modules.Music { using (var uow = _db.GetDbContext()) { - var pl = uow.MusicPlaylists.GetById(id); + var pl = uow._context.MusicPlaylists.FirstOrDefault(x => x.Id == id); if (pl != null) { if (_creds.IsOwner(ctx.User) || pl.AuthorId == ctx.User.Id) { - uow.MusicPlaylists.Remove(pl); + uow._context.MusicPlaylists.Remove(pl); await uow.SaveChangesAsync(); success = true; } @@ -109,7 +110,7 @@ namespace NadekoBot.Core.Modules.Music MusicPlaylist mpl; using (var uow = _db.GetDbContext()) { - mpl = uow.MusicPlaylists.GetWithSongs(id); + mpl = uow._context.MusicPlaylists.GetWithSongs(id); } await ctx.SendPaginatedConfirmAsync(page, (cur) => @@ -155,7 +156,7 @@ namespace NadekoBot.Core.Modules.Music AuthorId = ctx.User.Id, Songs = songs.ToList(), }; - uow.MusicPlaylists.Add(playlist); + uow._context.MusicPlaylists.Add(playlist); await uow.SaveChangesAsync(); } @@ -207,7 +208,7 @@ namespace NadekoBot.Core.Modules.Music MusicPlaylist mpl; using (var uow = _db.GetDbContext()) { - mpl = uow.MusicPlaylists.GetWithSongs(id); + mpl = uow._context.MusicPlaylists.GetWithSongs(id); } if (mpl == null) diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index b711f9a23..b8ed3fadb 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -11,10 +11,8 @@ namespace NadekoBot.Core.Services.Database IQuoteRepository Quotes { get; } IGuildConfigRepository GuildConfigs { get; } ICustomReactionRepository CustomReactions { get; } - IMusicPlaylistRepository MusicPlaylists { get; } IWaifuRepository Waifus { get; } IDiscordUserRepository DiscordUsers { get; } - IWarningsRepository Warnings { get; } IXpRepository Xp { get; } int SaveChanges(); diff --git a/src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs b/src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs deleted file mode 100644 index bcb15f262..000000000 --- a/src/NadekoBot/Services/Database/Repositories/IMusicPlaylistRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using System.Collections.Generic; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface IMusicPlaylistRepository : IRepository - { - List GetPlaylistsOnPage(int num); - MusicPlaylist GetWithSongs(int id); - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/IWarningsRepository.cs b/src/NadekoBot/Services/Database/Repositories/IWarningsRepository.cs deleted file mode 100644 index f31d9b3bb..000000000 --- a/src/NadekoBot/Services/Database/Repositories/IWarningsRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using System.Threading.Tasks; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface IWarningsRepository : IRepository - { - Warning[] ForId(ulong guildId, ulong userId); - Task ForgiveAll(ulong guildId, ulong userId, string moderator); - bool Forgive(ulong guildId, ulong userId, string moderator, int index); - Warning[] GetForGuild(ulong id); - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs b/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs index a9f55ce99..7de60f80c 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs @@ -25,7 +25,7 @@ namespace NadekoBot.Modules.Xp.Common public static ClubInfo GetByMember(this DbSet clubs, ulong userId) => Include(clubs).FirstOrDefault(c => c.Users.Any(u => u.UserId == userId)); - public static ClubInfo? GetByName(this DbSet clubs, string name, int discrim) + public static ClubInfo GetByName(this DbSet clubs, string name, int discrim) => Include(clubs).FirstOrDefault(c => c.Name.ToUpper() == name.ToUpper() && c.Discrim == discrim); public static int GetNextDiscrim(this DbSet clubs, string name) diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistExtensions.cs similarity index 50% rename from src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs rename to src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistExtensions.cs index 9773751a5..6ebef5158 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/MusicPlaylistExtensions.cs @@ -4,28 +4,26 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; -namespace NadekoBot.Core.Services.Database.Repositories.Impl +namespace NadekoBot.Modules.Music { - public class MusicPlaylistRepository : Repository, IMusicPlaylistRepository + public static class MusicPlaylistExtensions { - public MusicPlaylistRepository(DbContext context) : base(context) - { - } - - public List GetPlaylistsOnPage(int num) + public static List GetPlaylistsOnPage(this DbSet playlists, int num) { if (num < 1) throw new IndexOutOfRangeException(); - return _set.AsQueryable() + return playlists + .AsQueryable() .Skip((num - 1) * 20) .Take(20) .Include(pl => pl.Songs) .ToList(); } - public MusicPlaylist GetWithSongs(int id) => - _set.Include(mpl => mpl.Songs) + public static MusicPlaylist GetWithSongs(this DbSet playlists, int id) => + playlists + .Include(mpl => mpl.Songs) .FirstOrDefault(mpl => mpl.Id == id); } } diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/WarningsRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/WarningExtensions.cs similarity index 51% rename from src/NadekoBot/Services/Database/Repositories/Impl/WarningsRepository.cs rename to src/NadekoBot/Services/Database/Repositories/Impl/WarningExtensions.cs index 6405f2529..acaf39bae 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/WarningsRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/WarningExtensions.cs @@ -4,28 +4,25 @@ using System.Linq; using System.Threading.Tasks; using System; -namespace NadekoBot.Core.Services.Database.Repositories.Impl +namespace NadekoBot.Modules.Administration.Common { - public class WarningsRepository : Repository, IWarningsRepository + public static class WarningExtensions { - public WarningsRepository(DbContext context) : base(context) + public static Warning[] ForId(this DbSet warnings, ulong guildId, ulong userId) { - } - - public Warning[] ForId(ulong guildId, ulong userId) - { - var query = _set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId) + var query = warnings.AsQueryable() + .Where(x => x.GuildId == guildId && x.UserId == userId) .OrderByDescending(x => x.DateAdded); return query.ToArray(); } - public bool Forgive(ulong guildId, ulong userId, string mod, int index) + public static bool Forgive(this DbSet warnings, ulong guildId, ulong userId, string mod, int index) { if (index < 0) throw new ArgumentOutOfRangeException(nameof(index)); - var warn = _set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId) + var warn = warnings.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId) .OrderByDescending(x => x.DateAdded) .Skip(index) .FirstOrDefault(); @@ -38,9 +35,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl return true; } - public async Task ForgiveAll(ulong guildId, ulong userId, string mod) + public static async Task ForgiveAll(this DbSet warnings, ulong guildId, ulong userId, string mod) { - await _set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId) + await warnings.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId) .ForEachAsync(x => { if (x.Forgiven != true) @@ -51,9 +48,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl }); } - public Warning[] GetForGuild(ulong id) + public static Warning[] GetForGuild(this DbSet warnings, ulong id) { - return _set.AsQueryable().Where(x => x.GuildId == id).ToArray(); + return warnings.AsQueryable().Where(x => x.GuildId == id).ToArray(); } } } diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index 69534f733..156b35f93 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -15,9 +15,6 @@ namespace NadekoBot.Core.Services.Database private IGuildConfigRepository _guildConfigs; public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context)); - private IMusicPlaylistRepository _musicPlaylists; - public IMusicPlaylistRepository MusicPlaylists => _musicPlaylists ?? (_musicPlaylists = new MusicPlaylistRepository(_context)); - private ICustomReactionRepository _customReactions; public ICustomReactionRepository CustomReactions => _customReactions ?? (_customReactions = new CustomReactionsRepository(_context)); @@ -27,9 +24,6 @@ namespace NadekoBot.Core.Services.Database private IDiscordUserRepository _discordUsers; public IDiscordUserRepository DiscordUsers => _discordUsers ?? (_discordUsers = new DiscordUserRepository(_context)); - private IWarningsRepository _warnings; - public IWarningsRepository Warnings => _warnings ?? (_warnings = new WarningsRepository(_context)); - private IXpRepository _xp; public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context));