Started cleanup of quote commands. Moving logic to the service
This commit is contained in:
parent
3ca832090e
commit
afd5be89d1
3 changed files with 51 additions and 38 deletions
|
@ -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,
|
||||
|
@ -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<Quote> quotes, ulong guildId, string keyword)
|
||||
|
|
|
@ -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,23 +60,20 @@ 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())
|
||||
{
|
||||
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]
|
||||
|
@ -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);
|
||||
}
|
||||
);
|
||||
|
|
|
@ -24,9 +24,39 @@ public sealed class QuoteService : IQuoteService, IEService
|
|||
{
|
||||
await using var ctx = _db.GetDbContext();
|
||||
var deleted = await ctx.GetTable<Quote>()
|
||||
.Where(x => x.GuildId == guildId && x.AuthorId == userId)
|
||||
.DeleteAsync();
|
||||
.Where(x => x.GuildId == guildId && x.AuthorId == userId)
|
||||
.DeleteAsync();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue