Handler.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Api.Member.Wallet.GetMyTransactions;
  5. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  6. {
  7. public async Task<Response> Handle(Query request, CancellationToken ct)
  8. {
  9. var wallet = await db.Wallet.AsNoTracking().FirstOrDefaultAsync(w => w.MemberID == request.MemberID, ct);
  10. if (wallet is null)
  11. {
  12. return new Response
  13. {
  14. Total = 0,
  15. List = []
  16. };
  17. }
  18. var query = db.WalletTransaction.AsNoTracking().Where(t => t.WalletKey == wallet.WalletKey);
  19. if (request.Type.HasValue)
  20. {
  21. query = query.Where(t => t.TxType == request.Type.Value);
  22. }
  23. var total = await query.CountAsync(ct);
  24. var skip = (request.Page - 1) * request.PerPage;
  25. var list = await query
  26. .OrderByDescending(t => t.ID)
  27. .Skip(skip)
  28. .Take(request.PerPage)
  29. .Select(t => new
  30. {
  31. t.ID,
  32. TxType = t.TxType.ToString(),
  33. BalanceType = t.BalanceType.ToString(),
  34. Amount = (long)t.Amount.Value,
  35. BalanceAfter = (long)t.BalanceAfter.Value,
  36. t.Reason,
  37. t.RefID,
  38. t.Memo,
  39. t.CreatedAt
  40. })
  41. .ToListAsync(ct);
  42. return new Response
  43. {
  44. Total = total,
  45. List = [..list.Select(t => new Response.Row
  46. {
  47. ID = t.ID,
  48. TxType = t.TxType,
  49. BalanceType = t.BalanceType,
  50. Amount = t.Amount,
  51. BalanceAfter = t.BalanceAfter,
  52. Reason = t.Reason,
  53. RefID = t.RefID,
  54. Memo = t.Memo,
  55. CreatedAt = t.CreatedAt
  56. })]
  57. };
  58. }
  59. }