added pagination to .curtrs

This commit is contained in:
Toastie 2025-03-09 17:27:00 +13:00
parent 37986ed0b2
commit ad472fd52e
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
2 changed files with 152 additions and 130 deletions
src/EllieBot
Db/Extensions
Modules/Gambling

View file

@ -7,14 +7,23 @@ namespace EllieBot.Db;
public static class CurrencyTransactionExtensions
{
public static Task<List<CurrencyTransaction>> GetPageFor(
public static async Task<IReadOnlyCollection<CurrencyTransaction>> GetPageFor(
this DbSet<CurrencyTransaction> set,
ulong userId,
int page)
=> set.ToLinqToDBTable()
{
var items = await set.ToLinqToDBTable()
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.DateAdded)
.Skip(15 * page)
.Take(15)
.ToListAsyncLinqToDB();
return items;
}
public static async Task<int> GetCountFor(this DbSet<CurrencyTransaction> set, ulong userId)
=> await set.ToLinqToDBTable()
.Where(x => x.UserId == userId)
.CountAsyncLinqToDB();
}

View file

@ -373,18 +373,31 @@ public partial class Gambling : GamblingModule<GamblingService>
return;
}
List<CurrencyTransaction> trs;
await using (var uow = _db.GetDbContext())
{
trs = await uow.Set<CurrencyTransaction>().GetPageFor(userId, page);
}
var embed = CreateEmbed()
.WithTitle(GetText(strs.transactions(
((SocketGuild)ctx.Guild)?.GetUser(userId)?.ToString()
?? $"{userId}")))
.WithOkColor();
int count;
await using (var uow = _db.GetDbContext())
{
count = await uow.Set<CurrencyTransaction>()
.GetCountFor(userId);
}
await Response()
.Paginated()
.PageItems(async (curPage) =>
{
await using var uow = _db.GetDbContext();
return await uow.Set<CurrencyTransaction>()
.GetPageFor(userId, curPage);
})
.PageSize(15)
.TotalElements(count)
.Page((trs, _) =>
{
var sb = new StringBuilder();
foreach (var tr in trs)
{
@ -406,8 +419,8 @@ public partial class Gambling : GamblingModule<GamblingService>
}
embed.WithDescription(sb.ToString());
embed.WithFooter(GetText(strs.page(page + 1)));
await Response().Embed(embed).SendAsync();
return Task.FromResult(embed);
}).SendAsync();
}
private static string GetFormattedCurtrDate(CurrencyTransaction ct)