Handler.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Api.Member.Wallet.GetMy;
  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().Include(w => w.Balances).FirstOrDefaultAsync(w => w.MemberID == request.MemberID, ct);
  10. if (wallet is null)
  11. {
  12. return null;
  13. }
  14. return new Response
  15. {
  16. ID = wallet.ID,
  17. WalletKey = wallet.WalletKey,
  18. MemberID = wallet.MemberID,
  19. Balance = (long)wallet.GetTotalAvailable().Value,
  20. Balances = [..wallet.Balances.Select(b => new Response.BalanceRow
  21. {
  22. Type = b.Type.ToString(),
  23. Amount = (long)b.Amount.Value
  24. })],
  25. CreatedAt = wallet.CreatedAt,
  26. UpdatedAt = wallet.UpdatedAt
  27. };
  28. }
  29. }