Files
blazor-hrm/Features/Organization/Employee/Cqrs/GetEmployeeDeductionListByEmployeeIdHandler.cs
T
2026-07-21 14:08:10 +07:00

46 lines
1.9 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Organization.Employee.Cqrs;
public class GetEmployeeDeductionListResponse
{
public string? Id { get; set; }
public string? DeductionId { get; set; }
public string? EmployeeCode { get; set; }
public string? DeductionName { get; set; }
public string? DeductionCategory { get; set; }
public decimal Amount { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; }
}
public record GetEmployeeDeductionListByEmployeeIdQuery(string EmployeeId) : IRequest<List<GetEmployeeDeductionListResponse>>;
public class GetEmployeeDeductionListByEmployeeIdHandler : IRequestHandler<GetEmployeeDeductionListByEmployeeIdQuery, List<GetEmployeeDeductionListResponse>>
{
private readonly AppDbContext _context;
public GetEmployeeDeductionListByEmployeeIdHandler(AppDbContext context) => _context = context;
public async Task<List<GetEmployeeDeductionListResponse>> Handle(GetEmployeeDeductionListByEmployeeIdQuery request, CancellationToken cancellationToken)
{
var data = await _context.EmployeeDeduction
.Where(x => x.EmployeeId == request.EmployeeId)
.Select(x => new GetEmployeeDeductionListResponse
{
Id = x.Id,
DeductionId = x.DeductionId,
EmployeeCode = x.Employee != null ? x.Employee.Code : string.Empty,
DeductionName = x.Deduction != null ? x.Deduction.Name : string.Empty,
DeductionCategory = x.Deduction != null ? x.Deduction.Category : string.Empty,
Amount = x.Amount,
Description = x.Description,
IsActive = x.IsActive
})
.OrderBy(x => x.DeductionName)
.ToListAsync(cancellationToken);
return data;
}
}