Started cleanup of quote commands. Moving logic to the service

This commit is contained in:
Toastie (DCS Team) 2024-08-19 23:09:13 +12:00
parent 3ca832090e
commit afd5be89d1
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
3 changed files with 51 additions and 38 deletions

View file

@ -1,4 +1,5 @@
#nullable disable #nullable disable
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using EllieBot.Db.Models; using EllieBot.Db.Models;
@ -9,21 +10,6 @@ public static class QuoteExtensions
public static IEnumerable<Quote> GetForGuild(this DbSet<Quote> quotes, ulong guildId) public static IEnumerable<Quote> GetForGuild(this DbSet<Quote> quotes, ulong guildId)
=> quotes.AsQueryable().Where(x => x.GuildId == guildId); => quotes.AsQueryable().Where(x => x.GuildId == guildId);
public static IReadOnlyCollection<Quote> GetGroup(
this DbSet<Quote> 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<Quote> GetRandomQuoteByKeywordAsync( public static async Task<Quote> GetRandomQuoteByKeywordAsync(
this DbSet<Quote> quotes, this DbSet<Quote> quotes,
ulong guildId, ulong guildId,

View file

@ -11,7 +11,7 @@ namespace EllieBot.Modules.Utility;
public partial class Utility public partial class Utility
{ {
[Group] [Group]
public partial class QuoteCommands : EllieModule public partial class QuoteCommands : EllieModule<QuoteService>
{ {
private const string PREPEND_EXPORT = private const string PREPEND_EXPORT =
""" """
@ -60,24 +60,21 @@ public partial class Utility
if (page < 0) if (page < 0)
return; return;
IEnumerable<Quote> quotes; var quotes = await _service.GetAllQuotesAsync(ctx.Guild.Id, page, order);
await using (var uow = _db.GetDbContext())
if (quotes.Count == 0)
{ {
quotes = uow.Set<Quote>().GetGroup(ctx.Guild.Id, page, order); await Response().Error(strs.quotes_page_none).SendAsync();
return;
} }
if (quotes.Any()) var list = quotes.Select(q => $"`{new kwum(q.Id)}` {Format.Bold(q.Keyword),-20} by {q.AuthorName}")
{ .Join("\n");
await Response() await Response()
.Confirm(GetText(strs.quotes_page(page + 1)), .Confirm(GetText(strs.quotes_page(page + 1)), list)
string.Join("\n",
quotes.Select(q
=> $"`{new kwum(q.Id)}` {Format.Bold(q.Keyword.SanitizeAllMentions()),-20} by {q.AuthorName.SanitizeAllMentions()}")))
.SendAsync(); .SendAsync();
} }
else
await Response().Error(strs.quotes_page_none).SendAsync();
}
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]

View file

@ -29,4 +29,34 @@ public sealed class QuoteService : IQuoteService, IEService
return deleted; return deleted;
} }
/// <summary>
/// Delete all quotes in a guild
/// </summary>
/// <param name="guildId">ID of the guild</param>
/// <returns>Number of deleted qutoes</returns>
public async Task<int> DeleteAllQuotesAsync(ulong guildId)
{
await using var ctx = _db.GetDbContext();
var deleted = await ctx.GetTable<Quote>()
.Where(x => x.GuildId == guildId)
.DeleteAsync();
return deleted;
}
public async Task<IReadOnlyList<Quote>> GetAllQuotesAsync(ulong guildId, int page, OrderType order)
{
await using var uow = _db.GetDbContext();
var q = uow.Set<Quote>()
.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();
}
} }