diff --git a/src/EllieBot/Db/Extensions/QuoteExtensions.cs b/src/EllieBot/Db/Extensions/QuoteExtensions.cs deleted file mode 100644 index afd3509..0000000 --- a/src/EllieBot/Db/Extensions/QuoteExtensions.cs +++ /dev/null @@ -1,39 +0,0 @@ -#nullable disable -using LinqToDB.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore; -using EllieBot.Db.Models; - -namespace EllieBot.Db; - -public static class QuoteExtensions -{ - public static IEnumerable GetForGuild(this DbSet quotes, ulong guildId) - => quotes.AsQueryable().Where(x => x.GuildId == guildId); - - public static async Task GetRandomQuoteByKeywordAsync( - this DbSet quotes, - ulong guildId, - string keyword) - { - return (await quotes.AsQueryable().Where(q => q.GuildId == guildId && q.Keyword == keyword).ToArrayAsync()) - .RandomOrDefault(); - } - - public static async Task SearchQuoteKeywordTextAsync( - this DbSet quotes, - ulong guildId, - string keyword, - string text) - { - return (await quotes.AsQueryable() - .Where(q => q.GuildId == guildId - && (keyword == null || q.Keyword == keyword) - && (EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%") - || EF.Functions.Like(q.AuthorName, text))) - .ToArrayAsync()) - .RandomOrDefault(); - } - - public static void RemoveAllByKeyword(this DbSet quotes, ulong guildId, string keyword) - => quotes.RemoveRange(quotes.AsQueryable().Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword)); -} \ No newline at end of file diff --git a/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs b/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs index 1fe50c3..09218c2 100644 --- a/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs +++ b/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs @@ -85,16 +85,7 @@ public partial class Utility keyword = keyword.ToUpperInvariant(); - Quote quote; - await using (var uow = _db.GetDbContext()) - { - quote = await uow.Set().GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword); - //if (quote is not null) - //{ - // quote.UseCount += 1; - // uow.Complete(); - //} - } + var quote = await _service.GetQuoteByKeywordAsync(ctx.Guild.Id, keyword); if (quote is null) return; @@ -110,6 +101,7 @@ public partial class Utility .SendAsync(); } + [Cmd] [RequireContext(ContextType.Guild)] public async Task QuoteShow(kwum quoteId) @@ -154,7 +146,7 @@ public partial class Utility { var msg = sm.Data.Components.FirstOrDefault()?.Value; - if (!string.IsNullOrWhiteSpace(msg)) + if (!string.IsNullOrWhiteSpace(msg)) await QuoteEdit(id, msg); } ); @@ -186,27 +178,30 @@ public partial class Utility .SendAsync(); } - private async Task QuoteSearchinternalAsync(string? keyword, string textOrAuthor) + private async Task QuoteSearchInternalAsync(string? keyword, string textOrAuthor) { if (string.IsNullOrWhiteSpace(textOrAuthor)) return; keyword = keyword?.ToUpperInvariant(); - Quote quote; - await using (var uow = _db.GetDbContext()) - { - quote = await uow.Set().SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor); - } - - if (quote is null) - return; + var quotes = await _service.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor); await Response() - .Confirm($"`{new kwum(quote.Id)}` 💬 ", - quote.Keyword.ToLowerInvariant() - + ": " - + quote.Text.SanitizeAllMentions()) + .Paginated() + .Items(quotes) + .PageSize(1) + .Page((pageQuotes, _) => + { + var quote = pageQuotes[0]; + + var text = quote.Keyword.ToLowerInvariant() + ": " + quote.Text; + + return _sender.CreateEmbed() + .WithOkColor() + .WithTitle($"{new kwum(quote.Id)} 💬 ") + .WithDescription(text); + }) .SendAsync(); } @@ -214,13 +209,13 @@ public partial class Utility [RequireContext(ContextType.Guild)] [Priority(0)] public Task QuoteSearch(string textOrAuthor) - => QuoteSearchinternalAsync(null, textOrAuthor); + => QuoteSearchInternalAsync(null, textOrAuthor); [Cmd] [RequireContext(ContextType.Guild)] [Priority(1)] public Task QuoteSearch(string keyword, [Leftover] string textOrAuthor) - => QuoteSearchinternalAsync(keyword, textOrAuthor); + => QuoteSearchInternalAsync(keyword, textOrAuthor); [Cmd] [RequireContext(ContextType.Guild)] @@ -229,14 +224,7 @@ public partial class Utility if (quoteId < 0) return; - Quote quote; - - var repCtx = new ReplacementContext(Context); - - await using (var uow = _db.GetDbContext()) - { - quote = uow.Set().GetById(quoteId); - } + var quote = await _service.GetQuoteByIdAsync(quoteId); if (quote is null || quote.GuildId != ctx.Guild.Id) { @@ -249,6 +237,7 @@ public partial class Utility + ":\n"; + var repCtx = new ReplacementContext(Context); var text = SmartText.CreateFrom(quote.Text); text = await repSvc.ReplaceAsync(text, repCtx); await Response() @@ -257,6 +246,7 @@ public partial class Utility .SendAsync(); } + [Cmd] [RequireContext(ContextType.Guild)] public async Task QuoteAdd(string keyword, [Leftover] string text) @@ -388,7 +378,7 @@ public partial class Utility await using (var uow = _db.GetDbContext()) { - uow.Set().RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant()); + await _service.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant()); await uow.SaveChangesAsync(); } @@ -401,11 +391,7 @@ public partial class Utility [UserPerm(GuildPerm.Administrator)] public async Task QuotesExport() { - IEnumerable quotes; - await using (var uow = _db.GetDbContext()) - { - quotes = uow.Set().GetForGuild(ctx.Guild.Id).ToList(); - } + var quotes = _service.GetForGuild(ctx.Guild.Id).ToList(); var exprsDict = quotes.GroupBy(x => x.Keyword) .ToDictionary(x => x.Key, x => x.Select(ExportedQuote.FromModel)); diff --git a/src/EllieBot/Modules/Utility/Quote/QuoteService.cs b/src/EllieBot/Modules/Utility/Quote/QuoteService.cs index 7c44a15..9c547b7 100644 --- a/src/EllieBot/Modules/Utility/Quote/QuoteService.cs +++ b/src/EllieBot/Modules/Utility/Quote/QuoteService.cs @@ -59,4 +59,68 @@ public sealed class QuoteService : IQuoteService, IEService return await q.Skip(15 * page).Take(15).ToArrayAsync(); } + + public async Task GetQuoteByKeywordAsync(ulong guildId, string keyword) + { + await using var uow = _db.GetDbContext(); + var quotes = await uow.GetTable() + .Where(q => q.GuildId == guildId && q.Keyword == keyword) + .ToArrayAsyncLinqToDB(); + + return quotes.RandomOrDefault(); + } + + public async Task> SearchQuoteKeywordTextAsync( + ulong guildId, + string? keyword, + string text) + { + keyword = keyword?.ToUpperInvariant(); + await using var uow = _db.GetDbContext(); + + var quotes = await uow.GetTable() + .Where(q => q.GuildId == guildId + && (keyword == null || q.Keyword == keyword)) + .ToArrayAsync(); + + var toReturn = new List(quotes.Length); + + foreach (var q in quotes) + { + if (q.AuthorName.Contains(text, StringComparison.InvariantCultureIgnoreCase) + || q.Text.Contains(text, StringComparison.InvariantCultureIgnoreCase)) + { + toReturn.Add(q); + } + } + + return toReturn; + } + + public IEnumerable GetForGuild(ulong guildId) + { + using var uow = _db.GetDbContext(); + var quotes = uow.GetTable() + .Where(x => x.GuildId == guildId); + return quotes; + } + + public Task RemoveAllByKeyword(ulong guildId, string keyword) + { + keyword = keyword.ToUpperInvariant(); + + using var uow = _db.GetDbContext(); + + var count = uow.GetTable() + .Where(x => x.GuildId == guildId && x.Keyword == keyword) + .DeleteAsync(); + + return count; + } + + public async Task GetQuoteByIdAsync(kwum quoteId) + { + await using var uow = _db.GetDbContext(); + return uow.Set().GetById(quoteId); + } } \ No newline at end of file