From c7ff982ec1c855ec2a42c95c59304b16d29b087e Mon Sep 17 00:00:00 2001 From: Kwoth Date: Fri, 18 Jun 2021 22:53:44 +0200 Subject: [PATCH] Quotes repository removed --- .../Modules/Utility/QuoteCommands.cs | 20 +++++++++-------- .../Services/Database/IUnitOfWork.cs | 4 +--- .../Database/Repositories/IQuoteRepository.cs | 14 ------------ .../Impl/IGuildConfigRepository.cs | 1 - ...{QuoteRepository.cs => QuoteExtensions.cs} | 22 ++++++++----------- src/NadekoBot/Services/Database/UnitOfWork.cs | 3 --- 6 files changed, 21 insertions(+), 43 deletions(-) delete mode 100644 src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs delete mode 100644 src/NadekoBot/Services/Database/Repositories/Impl/IGuildConfigRepository.cs rename src/NadekoBot/Services/Database/Repositories/Impl/{QuoteRepository.cs => QuoteExtensions.cs} (60%) diff --git a/src/NadekoBot/Modules/Utility/QuoteCommands.cs b/src/NadekoBot/Modules/Utility/QuoteCommands.cs index 3807499e4..9d995a3fa 100644 --- a/src/NadekoBot/Modules/Utility/QuoteCommands.cs +++ b/src/NadekoBot/Modules/Utility/QuoteCommands.cs @@ -9,6 +9,8 @@ using NadekoBot.Extensions; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using NadekoBot.Core.Services.Database.Repositories.Impl; +using NadekoBot.Db; namespace NadekoBot.Modules.Utility { @@ -42,7 +44,7 @@ namespace NadekoBot.Modules.Utility IEnumerable quotes; using (var uow = _db.GetDbContext()) { - quotes = uow.Quotes.GetGroup(ctx.Guild.Id, page, order); + quotes = uow._context.Quotes.GetGroup(ctx.Guild.Id, page, order); } if (quotes.Any()) @@ -65,7 +67,7 @@ namespace NadekoBot.Modules.Utility Quote quote; using (var uow = _db.GetDbContext()) { - quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword); + quote = await uow._context.Quotes.GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword); //if (quote != null) //{ // quote.UseCount += 1; @@ -97,7 +99,7 @@ namespace NadekoBot.Modules.Utility Quote quote; using (var uow = _db.GetDbContext()) { - quote = uow.Quotes.GetById(id); + quote = uow._context.Quotes.GetById(id); if (quote.GuildId != Context.Guild.Id) quote = null; } @@ -136,7 +138,7 @@ namespace NadekoBot.Modules.Utility Quote keywordquote; using (var uow = _db.GetDbContext()) { - keywordquote = await uow.Quotes.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, text); + keywordquote = await uow._context.Quotes.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, text); } if (keywordquote == null) @@ -161,7 +163,7 @@ namespace NadekoBot.Modules.Utility using (var uow = _db.GetDbContext()) { - quote = uow.Quotes.GetById(id); + quote = uow._context.Quotes.GetById(id); } if (quote is null || quote.GuildId != ctx.Guild.Id) @@ -198,7 +200,7 @@ namespace NadekoBot.Modules.Utility Quote q; using (var uow = _db.GetDbContext()) { - uow.Quotes.Add(q = new Quote + uow._context.Quotes.Add(q = new Quote { AuthorId = ctx.Message.Author.Id, AuthorName = ctx.Message.Author.Username, @@ -221,7 +223,7 @@ namespace NadekoBot.Modules.Utility string response; using (var uow = _db.GetDbContext()) { - var q = uow.Quotes.GetById(id); + var q = uow._context.Quotes.GetById(id); if ((q?.GuildId != ctx.Guild.Id) || (!isAdmin && q.AuthorId != ctx.Message.Author.Id)) { @@ -229,7 +231,7 @@ namespace NadekoBot.Modules.Utility } else { - uow.Quotes.Remove(q); + uow._context.Quotes.Remove(q); await uow.SaveChangesAsync(); success = true; response = GetText("quote_deleted", id); @@ -253,7 +255,7 @@ namespace NadekoBot.Modules.Utility using (var uow = _db.GetDbContext()) { - uow.Quotes.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant()); + uow._context.Quotes.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant()); await uow.SaveChangesAsync(); } diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index 0c4d6a2e2..6de6135fc 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -6,9 +6,7 @@ namespace NadekoBot.Core.Services.Database { public interface IUnitOfWork : IDisposable { - NadekoContext _context { get; } - - IQuoteRepository Quotes { get; } + NadekoContext _context { get; } IGuildConfigRepository GuildConfigs { get; } IWaifuRepository Waifus { get; } IDiscordUserRepository DiscordUsers { get; } diff --git a/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs b/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs deleted file mode 100644 index 0bf53ace2..000000000 --- a/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs +++ /dev/null @@ -1,14 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface IQuoteRepository : IRepository - { - Task GetRandomQuoteByKeywordAsync(ulong guildId, string keyword); - Task SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text); - IEnumerable GetGroup(ulong guildId, int page, OrderType order); - void RemoveAllByKeyword(ulong guildId, string keyword); - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/IGuildConfigRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/IGuildConfigRepository.cs deleted file mode 100644 index 5f282702b..000000000 --- a/src/NadekoBot/Services/Database/Repositories/Impl/IGuildConfigRepository.cs +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/QuoteExtensions.cs similarity index 60% rename from src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs rename to src/NadekoBot/Services/Database/Repositories/Impl/QuoteExtensions.cs index a6407f3d5..7623fd102 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/QuoteExtensions.cs @@ -7,15 +7,11 @@ using NadekoBot.Common; namespace NadekoBot.Core.Services.Database.Repositories.Impl { - public class QuoteRepository : Repository, IQuoteRepository + public static class QuoteExtensions { - public QuoteRepository(DbContext context) : base(context) + public static IEnumerable GetGroup(this DbSet quotes, ulong guildId, int page, OrderType order) { - } - - public IEnumerable GetGroup(ulong guildId, int page, OrderType order) - { - var q = _set.AsQueryable().Where(x => x.GuildId == guildId); + var q = quotes.AsQueryable().Where(x => x.GuildId == guildId); if (order == OrderType.Keyword) q = q.OrderBy(x => x.Keyword); else @@ -24,20 +20,20 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl return q.Skip(15 * page).Take(15).ToArray(); } - public async Task GetRandomQuoteByKeywordAsync(ulong guildId, string keyword) + public static async Task GetRandomQuoteByKeywordAsync(this DbSet quotes, ulong guildId, string keyword) { var rng = new NadekoRandom(); - return (await _set.AsQueryable() + return (await quotes.AsQueryable() .Where(q => q.GuildId == guildId && q.Keyword == keyword) .ToListAsync()) .OrderBy(q => rng.Next()) .FirstOrDefault(); } - public async Task SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text) + public static async Task SearchQuoteKeywordTextAsync(this DbSet quotes, ulong guildId, string keyword, string text) { var rngk = new NadekoRandom(); - return (await _set.AsQueryable() + return (await quotes.AsQueryable() .Where(q => q.GuildId == guildId && q.Keyword == keyword && EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%") @@ -48,9 +44,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl .FirstOrDefault(); } - public void RemoveAllByKeyword(ulong guildId, string keyword) + public static void RemoveAllByKeyword(this DbSet quotes, ulong guildId, string keyword) { - _set.RemoveRange(_set.AsQueryable().Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword)); + quotes.RemoveRange(quotes.AsQueryable().Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword)); } } diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index 3d61bc93d..e50cb20e4 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -9,9 +9,6 @@ namespace NadekoBot.Core.Services.Database { public NadekoContext _context { get; } - private IQuoteRepository _quotes; - public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context)); - private IGuildConfigRepository _guildConfigs; public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context));