Files
blazor-hrm/Features/Leave/LeaveBalance/Cqrs/GetLeaveBalanceListHandler.cs
T
2026-07-21 14:08:10 +07:00

58 lines
2.2 KiB
C#

using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class GetLeaveBalanceListResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
public string? EmployeeId { get; set; }
public string? EmployeeName { get; set; }
public string? EmployeeCode { get; set; }
public string? LeaveCategoryId { get; set; }
public string? LeaveCategoryName { get; set; }
public int Entitlement { get; set; }
public int Used { get; set; }
public int Pending { get; set; }
public int Remaining { get; set; }
public int Year { get; set; }
}
public record GetLeaveBalanceListQuery() : IRequest<List<GetLeaveBalanceListResponse>>;
public class GetLeaveBalanceListHandler : IRequestHandler<GetLeaveBalanceListQuery, List<GetLeaveBalanceListResponse>>
{
private readonly AppDbContext _context;
public GetLeaveBalanceListHandler(AppDbContext context) => _context = context;
public async Task<List<GetLeaveBalanceListResponse>> Handle(GetLeaveBalanceListQuery request, CancellationToken cancellationToken)
{
return await _context.LeaveBalance
.AsNoTracking()
.NotDeletedOnly()
.Include(x => x.Employee)
.Include(x => x.LeaveCategory)
.OrderByDescending(x => x.Year)
.ThenBy(x => x.Employee!.FirstName)
.Select(x => new GetLeaveBalanceListResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
EmployeeId = x.EmployeeId,
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
EmployeeCode = x.Employee != null ? $"{x.Employee.Code}" : string.Empty,
LeaveCategoryId = x.LeaveCategoryId,
LeaveCategoryName = x.LeaveCategory != null ? x.LeaveCategory.Name : string.Empty,
Entitlement = x.Entitlement,
Used = x.Used,
Pending = x.Pending,
Remaining = x.Remaining,
Year = x.Year
})
.ToListAsync(cancellationToken);
}
}