Handler.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Application.Common;
  4. using Domain.Entities.Payments.ValueObject;
  5. using SharedKernel.Results;
  6. using Microsoft.EntityFrameworkCore;
  7. namespace Application.Features.Api.MyPage.ChargeLogs;
  8. internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
  9. {
  10. public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
  11. {
  12. var page = request.Page < 1 ? 1 : request.Page;
  13. var perPage = request.PerPage is < 1 or > 50 ? 20 : request.PerPage;
  14. var now = DateTime.UtcNow;
  15. var startDate = request.Type switch
  16. {
  17. SearchDateType.Week => now.AddDays(-7),
  18. SearchDateType.Month => now.AddMonths(-1),
  19. SearchDateType.QuarterYear => now.AddMonths(-3),
  20. SearchDateType.HalfYear => now.AddMonths(-6),
  21. _ => now.Date
  22. };
  23. var query = db.PaymentOrder.AsNoTracking().Where(o => o.MemberID == request.MemberID && o.Status == PaymentStatus.Paid && o.PaidAt >= startDate).OrderByDescending(o => o.PaidAt);
  24. var total = await query.CountAsync(ct);
  25. var list = await query
  26. .Skip((page - 1) * perPage)
  27. .Take(perPage)
  28. .Select(o => new ChargeLogItem(
  29. o.ID,
  30. o.OrderID,
  31. o.Amount,
  32. o.PointAmount,
  33. o.VatAmount,
  34. o.PaymentMethod.ToString(),
  35. o.Status.ToString(),
  36. o.CreatedAt,
  37. o.PaidAt
  38. ))
  39. .ToListAsync(ct);
  40. return Result.Success(new Response(list, total, page, perPage));
  41. }
  42. }