using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Leave.LeaveRequest.Cqrs; public class GetLeaveRequestListResponse { public string? Id { get; set; } public string? AutoNumber { get; set; } public string? EmployeeId { get; set; } public string? EmployeeName { get; set; } public string? LeaveType { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public double Days { get; set; } public string? Reason { get; set; } public string? Status { get; set; } } public record GetLeaveRequestListQuery() : IRequest>; public class GetLeaveRequestListHandler : IRequestHandler> { private readonly AppDbContext _context; public GetLeaveRequestListHandler(AppDbContext context) => _context = context; public async Task> Handle(GetLeaveRequestListQuery request, CancellationToken cancellationToken) { return await _context.LeaveRequest .AsNoTracking() .NotDeletedOnly() .Include(x => x.Employee) .Include(x => x.LeaveCategory) .OrderByDescending(x => x.CreatedAt) .Select(x => new GetLeaveRequestListResponse { Id = x.Id, AutoNumber = x.AutoNumber, EmployeeId = x.Employee != null ? x.Employee.Code : string.Empty, EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty, LeaveType = x.LeaveCategory != null ? x.LeaveCategory.Name : string.Empty, StartDate = x.StartDate, EndDate = x.EndDate, Days = x.Days, Reason = x.Reason, Status = x.Status }) .ToListAsync(cancellationToken); } }