From afd5be89d12211231141672f38fdf9c746a0c790 Mon Sep 17 00:00:00 2001 From: Toastie Date: Mon, 19 Aug 2024 23:09:13 +1200 Subject: [PATCH] Started cleanup of quote commands. Moving logic to the service --- src/EllieBot/Db/Extensions/QuoteExtensions.cs | 20 ++--------- .../Modules/Utility/Quote/QuoteCommands.cs | 33 ++++++++--------- .../Modules/Utility/Quote/QuoteService.cs | 36 +++++++++++++++++-- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/EllieBot/Db/Extensions/QuoteExtensions.cs b/src/EllieBot/Db/Extensions/QuoteExtensions.cs index a213e6d..afd3509 100644 --- a/src/EllieBot/Db/Extensions/QuoteExtensions.cs +++ b/src/EllieBot/Db/Extensions/QuoteExtensions.cs @@ -1,4 +1,5 @@ #nullable disable +using LinqToDB.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using EllieBot.Db.Models; @@ -8,22 +9,7 @@ public static class QuoteExtensions { public static IEnumerable GetForGuild(this DbSet quotes, ulong guildId) => quotes.AsQueryable().Where(x => x.GuildId == guildId); - - public static IReadOnlyCollection GetGroup( - this DbSet quotes, - ulong guildId, - int page, - OrderType order) - { - var q = quotes.AsQueryable().Where(x => x.GuildId == guildId); - if (order == OrderType.Keyword) - q = q.OrderBy(x => x.Keyword); - else - q = q.OrderBy(x => x.Id); - - return q.Skip(15 * page).Take(15).ToArray(); - } - + public static async Task GetRandomQuoteByKeywordAsync( this DbSet quotes, ulong guildId, @@ -45,7 +31,7 @@ public static class QuoteExtensions && (EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%") || EF.Functions.Like(q.AuthorName, text))) .ToArrayAsync()) - .RandomOrDefault(); + .RandomOrDefault(); } public static void RemoveAllByKeyword(this DbSet quotes, ulong guildId, string keyword) diff --git a/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs b/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs index d0ff339..1fe50c3 100644 --- a/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs +++ b/src/EllieBot/Modules/Utility/Quote/QuoteCommands.cs @@ -11,7 +11,7 @@ namespace EllieBot.Modules.Utility; public partial class Utility { [Group] - public partial class QuoteCommands : EllieModule + public partial class QuoteCommands : EllieModule { private const string PREPEND_EXPORT = """ @@ -60,23 +60,20 @@ public partial class Utility if (page < 0) return; - IEnumerable quotes; - await using (var uow = _db.GetDbContext()) + var quotes = await _service.GetAllQuotesAsync(ctx.Guild.Id, page, order); + + if (quotes.Count == 0) { - quotes = uow.Set().GetGroup(ctx.Guild.Id, page, order); + await Response().Error(strs.quotes_page_none).SendAsync(); + return; } - if (quotes.Any()) - { - await Response() - .Confirm(GetText(strs.quotes_page(page + 1)), - string.Join("\n", - quotes.Select(q - => $"`{new kwum(q.Id)}` {Format.Bold(q.Keyword.SanitizeAllMentions()),-20} by {q.AuthorName.SanitizeAllMentions()}"))) - .SendAsync(); - } - else - await Response().Error(strs.quotes_page_none).SendAsync(); + var list = quotes.Select(q => $"`{new kwum(q.Id)}` {Format.Bold(q.Keyword),-20} by {q.AuthorName}") + .Join("\n"); + + await Response() + .Confirm(GetText(strs.quotes_page(page + 1)), list) + .SendAsync(); } [Cmd] @@ -156,8 +153,8 @@ public partial class Utility async (sm) => { var msg = sm.Data.Components.FirstOrDefault()?.Value; - - if(!string.IsNullOrWhiteSpace(msg)) + + if (!string.IsNullOrWhiteSpace(msg)) await QuoteEdit(id, msg); } ); @@ -306,7 +303,7 @@ public partial class Utility .UpdateWithOutputAsync((del, ins) => ins); q = result.FirstOrDefault(); - + await uow.SaveChangesAsync(); } diff --git a/src/EllieBot/Modules/Utility/Quote/QuoteService.cs b/src/EllieBot/Modules/Utility/Quote/QuoteService.cs index db49648..7c44a15 100644 --- a/src/EllieBot/Modules/Utility/Quote/QuoteService.cs +++ b/src/EllieBot/Modules/Utility/Quote/QuoteService.cs @@ -13,7 +13,7 @@ public sealed class QuoteService : IQuoteService, IEService { _db = db; } - + /// /// Delete all quotes created by the author in a guild /// @@ -24,9 +24,39 @@ public sealed class QuoteService : IQuoteService, IEService { await using var ctx = _db.GetDbContext(); var deleted = await ctx.GetTable() - .Where(x => x.GuildId == guildId && x.AuthorId == userId) - .DeleteAsync(); + .Where(x => x.GuildId == guildId && x.AuthorId == userId) + .DeleteAsync(); return deleted; } + + /// + /// Delete all quotes in a guild + /// + /// ID of the guild + /// Number of deleted qutoes + public async Task DeleteAllQuotesAsync(ulong guildId) + { + await using var ctx = _db.GetDbContext(); + var deleted = await ctx.GetTable() + .Where(x => x.GuildId == guildId) + .DeleteAsync(); + + return deleted; + } + + public async Task> GetAllQuotesAsync(ulong guildId, int page, OrderType order) + { + await using var uow = _db.GetDbContext(); + var q = uow.Set() + .ToLinqToDBTable() + .Where(x => x.GuildId == guildId); + + if (order == OrderType.Keyword) + q = q.OrderBy(x => x.Keyword); + else + q = q.OrderBy(x => x.Id); + + return await q.Skip(15 * page).Take(15).ToArrayAsync(); + } } \ No newline at end of file