Handler.cs 1.5 KB

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