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
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using EllieBot.Db.Models;
@ -9,21 +10,6 @@ public static class QuoteExtensions
public static IEnumerable<Quote> GetForGuild(this DbSet<Quote> quotes, ulong 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(
this DbSet<Quote> quotes,
ulong guildId,

View file

@ -11,7 +11,7 @@ namespace EllieBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class QuoteCommands : EllieModule
public partial class QuoteCommands : EllieModule<QuoteService>
{
private const string PREPEND_EXPORT =
"""
@ -60,24 +60,21 @@ public partial class Utility
if (page < 0)
return;
IEnumerable<Quote> quotes;
await using (var uow = _db.GetDbContext())
var quotes = await _service.GetAllQuotesAsync(ctx.Guild.Id, page, order);
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()
.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()}")))
.Confirm(GetText(strs.quotes_page(page + 1)), list)
.SendAsync();
}
else
await Response().Error(strs.quotes_page_none).SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
@ -157,7 +154,7 @@ public partial class Utility
{
var msg = sm.Data.Components.FirstOrDefault()?.Value;
if(!string.IsNullOrWhiteSpace(msg))
if (!string.IsNullOrWhiteSpace(msg))
await QuoteEdit(id, msg);
}
);

View file

@ -29,4 +29,34 @@ public sealed class QuoteService : IQuoteService, IEService
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();
}
}