dev: Started cleanup of quote commands. Moving logic to the service

This commit is contained in:
Kwoth
2024-08-18 23:52:32 +00:00
parent 0b9e812d59
commit 9aaf062d78
3 changed files with 50 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ public sealed class QuoteService : IQuoteService, INService
{
_db = db;
}
/// <summary>
/// Delete all quotes created by the author in a guild
/// </summary>
@@ -24,9 +24,39 @@ public sealed class QuoteService : IQuoteService, INService
{
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<IReadOnlyCollection<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).ToArrayAsyncLinqToDB();
}
}