using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Leave.LeaveBalance.Cqrs; public class GetLeaveBalanceByIdResponse { public string? Id { get; set; } public string? AutoNumber { get; set; } public string? EmployeeId { get; set; } public string? EmployeeName { 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 DateTime ValidFrom { get; set; } public DateTime ValidTo { get; set; } public int CarryForward { get; set; } public DateTimeOffset? CreatedAt { get; set; } public string? CreatedBy { get; set; } public DateTimeOffset? UpdatedAt { get; set; } public string? UpdatedBy { get; set; } } public record GetLeaveBalanceByIdQuery(string Id) : IRequest; public class GetLeaveBalanceByIdHandler : IRequestHandler { private readonly AppDbContext _context; public GetLeaveBalanceByIdHandler(AppDbContext context) => _context = context; public async Task Handle(GetLeaveBalanceByIdQuery request, CancellationToken cancellationToken) { return await _context.LeaveBalance .AsNoTracking() .Include(x => x.Employee) .Include(x => x.LeaveCategory) .Where(x => x.Id == request.Id) .Select(x => new GetLeaveBalanceByIdResponse { Id = x.Id, AutoNumber = x.AutoNumber, EmployeeId = x.EmployeeId, EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : 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, ValidFrom = x.ValidFrom, ValidTo = x.ValidTo, CarryForward = x.CarryForward, CreatedAt = x.CreatedAt, CreatedBy = x.CreatedBy, UpdatedAt = x.UpdatedAt, UpdatedBy = x.UpdatedBy }) .FirstOrDefaultAsync(cancellationToken); } }