Quotes repository removed

This commit is contained in:
Kwoth
2021-06-18 22:53:44 +02:00
parent 56f28568d1
commit c7ff982ec1
6 changed files with 21 additions and 43 deletions

View File

@@ -9,6 +9,8 @@ using NadekoBot.Extensions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NadekoBot.Core.Services.Database.Repositories.Impl;
using NadekoBot.Db;
namespace NadekoBot.Modules.Utility namespace NadekoBot.Modules.Utility
{ {
@@ -42,7 +44,7 @@ namespace NadekoBot.Modules.Utility
IEnumerable<Quote> quotes; IEnumerable<Quote> quotes;
using (var uow = _db.GetDbContext()) 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()) if (quotes.Any())
@@ -65,7 +67,7 @@ namespace NadekoBot.Modules.Utility
Quote quote; Quote quote;
using (var uow = _db.GetDbContext()) 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) //if (quote != null)
//{ //{
// quote.UseCount += 1; // quote.UseCount += 1;
@@ -97,7 +99,7 @@ namespace NadekoBot.Modules.Utility
Quote quote; Quote quote;
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
quote = uow.Quotes.GetById(id); quote = uow._context.Quotes.GetById(id);
if (quote.GuildId != Context.Guild.Id) if (quote.GuildId != Context.Guild.Id)
quote = null; quote = null;
} }
@@ -136,7 +138,7 @@ namespace NadekoBot.Modules.Utility
Quote keywordquote; Quote keywordquote;
using (var uow = _db.GetDbContext()) 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) if (keywordquote == null)
@@ -161,7 +163,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.GetDbContext()) 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) if (quote is null || quote.GuildId != ctx.Guild.Id)
@@ -198,7 +200,7 @@ namespace NadekoBot.Modules.Utility
Quote q; Quote q;
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
uow.Quotes.Add(q = new Quote uow._context.Quotes.Add(q = new Quote
{ {
AuthorId = ctx.Message.Author.Id, AuthorId = ctx.Message.Author.Id,
AuthorName = ctx.Message.Author.Username, AuthorName = ctx.Message.Author.Username,
@@ -221,7 +223,7 @@ namespace NadekoBot.Modules.Utility
string response; string response;
using (var uow = _db.GetDbContext()) 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)) if ((q?.GuildId != ctx.Guild.Id) || (!isAdmin && q.AuthorId != ctx.Message.Author.Id))
{ {
@@ -229,7 +231,7 @@ namespace NadekoBot.Modules.Utility
} }
else else
{ {
uow.Quotes.Remove(q); uow._context.Quotes.Remove(q);
await uow.SaveChangesAsync(); await uow.SaveChangesAsync();
success = true; success = true;
response = GetText("quote_deleted", id); response = GetText("quote_deleted", id);
@@ -253,7 +255,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.GetDbContext()) 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(); await uow.SaveChangesAsync();
} }

View File

@@ -6,9 +6,7 @@ namespace NadekoBot.Core.Services.Database
{ {
public interface IUnitOfWork : IDisposable public interface IUnitOfWork : IDisposable
{ {
NadekoContext _context { get; } NadekoContext _context { get; }
IQuoteRepository Quotes { get; }
IGuildConfigRepository GuildConfigs { get; } IGuildConfigRepository GuildConfigs { get; }
IWaifuRepository Waifus { get; } IWaifuRepository Waifus { get; }
IDiscordUserRepository DiscordUsers { get; } IDiscordUserRepository DiscordUsers { get; }

View File

@@ -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<Quote>
{
Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword);
Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text);
IEnumerable<Quote> GetGroup(ulong guildId, int page, OrderType order);
void RemoveAllByKeyword(ulong guildId, string keyword);
}
}

View File

@@ -7,15 +7,11 @@ using NadekoBot.Common;
namespace NadekoBot.Core.Services.Database.Repositories.Impl namespace NadekoBot.Core.Services.Database.Repositories.Impl
{ {
public class QuoteRepository : Repository<Quote>, IQuoteRepository public static class QuoteExtensions
{ {
public QuoteRepository(DbContext context) : base(context) public static IEnumerable<Quote> GetGroup(this DbSet<Quote> quotes, ulong guildId, int page, OrderType order)
{ {
} var q = quotes.AsQueryable().Where(x => x.GuildId == guildId);
public IEnumerable<Quote> GetGroup(ulong guildId, int page, OrderType order)
{
var q = _set.AsQueryable().Where(x => x.GuildId == guildId);
if (order == OrderType.Keyword) if (order == OrderType.Keyword)
q = q.OrderBy(x => x.Keyword); q = q.OrderBy(x => x.Keyword);
else else
@@ -24,20 +20,20 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
return q.Skip(15 * page).Take(15).ToArray(); return q.Skip(15 * page).Take(15).ToArray();
} }
public async Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword) public static async Task<Quote> GetRandomQuoteByKeywordAsync(this DbSet<Quote> quotes, ulong guildId, string keyword)
{ {
var rng = new NadekoRandom(); var rng = new NadekoRandom();
return (await _set.AsQueryable() return (await quotes.AsQueryable()
.Where(q => q.GuildId == guildId && q.Keyword == keyword) .Where(q => q.GuildId == guildId && q.Keyword == keyword)
.ToListAsync()) .ToListAsync())
.OrderBy(q => rng.Next()) .OrderBy(q => rng.Next())
.FirstOrDefault(); .FirstOrDefault();
} }
public async Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text) public static async Task<Quote> SearchQuoteKeywordTextAsync(this DbSet<Quote> quotes, ulong guildId, string keyword, string text)
{ {
var rngk = new NadekoRandom(); var rngk = new NadekoRandom();
return (await _set.AsQueryable() return (await quotes.AsQueryable()
.Where(q => q.GuildId == guildId .Where(q => q.GuildId == guildId
&& q.Keyword == keyword && q.Keyword == keyword
&& EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%") && EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%")
@@ -48,9 +44,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
.FirstOrDefault(); .FirstOrDefault();
} }
public void RemoveAllByKeyword(ulong guildId, string keyword) public static void RemoveAllByKeyword(this DbSet<Quote> 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));
} }
} }

View File

@@ -9,9 +9,6 @@ namespace NadekoBot.Core.Services.Database
{ {
public NadekoContext _context { get; } public NadekoContext _context { get; }
private IQuoteRepository _quotes;
public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context));
private IGuildConfigRepository _guildConfigs; private IGuildConfigRepository _guildConfigs;
public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context)); public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context));