using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Application.Common; using Domain.Entities.Payments.ValueObject; using SharedKernel.Results; using Microsoft.EntityFrameworkCore; namespace Application.Features.Api.MyPage.ChargeLogs; internal sealed class Handler(IAppDbContext db) : IQueryHandler> { public async Task> Handle(Query request, CancellationToken ct) { var page = request.Page < 1 ? 1 : request.Page; var perPage = request.PerPage is < 1 or > 50 ? 20 : request.PerPage; var now = DateTime.UtcNow; var startDate = request.Type switch { SearchDateType.Week => now.AddDays(-7), SearchDateType.Month => now.AddMonths(-1), SearchDateType.QuarterYear => now.AddMonths(-3), SearchDateType.HalfYear => now.AddMonths(-6), _ => now.Date }; var query = db.PaymentOrder.AsNoTracking().Where(o => o.MemberID == request.MemberID && o.Status == PaymentStatus.Paid && o.PaidAt >= startDate).OrderByDescending(o => o.PaidAt); var total = await query.CountAsync(ct); var list = await query .Skip((page - 1) * perPage) .Take(perPage) .Select(o => new ChargeLogItem( o.ID, o.OrderID, o.Amount, o.PointAmount, o.VatAmount, o.PaymentMethod.ToString(), o.Status.ToString(), o.CreatedAt, o.PaidAt )) .ToListAsync(ct); return Result.Success(new Response(list, total, page, perPage)); } }