Files
blazor-hrm/Features/Payroll/Deduction/Cqrs/GetDeductionByIdHandler.cs
T
2026-07-21 14:08:10 +07:00

53 lines
1.8 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Payroll.Deduction.Cqrs;
public class GetDeductionByIdResponse
{
public string? Id { get; set; }
public string? Code { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public string? Category { get; set; }
public bool PreTax { get; set; }
public string? CalculationMethod { get; set; }
public string? Status { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
}
public record GetDeductionByIdQuery(string Id) : IRequest<GetDeductionByIdResponse?>;
public class GetDeductionByIdHandler : IRequestHandler<GetDeductionByIdQuery, GetDeductionByIdResponse?>
{
private readonly AppDbContext _context;
public GetDeductionByIdHandler(AppDbContext context) => _context = context;
public async Task<GetDeductionByIdResponse?> Handle(GetDeductionByIdQuery request, CancellationToken cancellationToken)
{
return await _context.Deduction
.AsNoTracking()
.Where(x => x.Id == request.Id)
.Select(x => new GetDeductionByIdResponse
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
Description = x.Description,
Category = x.Category,
PreTax = x.PreTax,
CalculationMethod = x.CalculationMethod,
Status = x.Status,
CreatedAt = x.CreatedAt,
CreatedBy = x.CreatedBy,
UpdatedAt = x.UpdatedAt,
UpdatedBy = x.UpdatedBy
})
.FirstOrDefaultAsync(cancellationToken);
}
}