Files
blazor-saas-hrm/Features/Organization/Employee/Cqrs/UpdateEmployeeDeductionHandler.cs
T
2026-07-21 14:22:06 +07:00

45 lines
1.6 KiB
C#

using Indotalent.ConfigBackEnd.Exceptions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Organization.Employee.Cqrs;
public class UpdateEmployeeDeductionRequest
{
public string? Id { get; set; }
public string? DeductionId { get; set; }
public decimal Amount { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; }
}
public class UpdateEmployeeDeductionResponse
{
public string? Id { get; set; }
}
public record UpdateEmployeeDeductionCommand(UpdateEmployeeDeductionRequest Data) : IRequest<UpdateEmployeeDeductionResponse>;
public class UpdateEmployeeDeductionHandler : IRequestHandler<UpdateEmployeeDeductionCommand, UpdateEmployeeDeductionResponse>
{
private readonly AppDbContext _context;
public UpdateEmployeeDeductionHandler(AppDbContext context) => _context = context;
public async Task<UpdateEmployeeDeductionResponse> Handle(UpdateEmployeeDeductionCommand request, CancellationToken cancellationToken)
{
var entity = await _context.EmployeeDeduction
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
if (entity == null) throw new NotFoundException("EmployeeDeduction", request.Data.Id ?? string.Empty);
entity.DeductionId = request.Data.DeductionId;
entity.Amount = request.Data.Amount;
entity.Description = request.Data.Description;
entity.IsActive = request.Data.IsActive;
await _context.SaveChangesAsync(cancellationToken);
return new UpdateEmployeeDeductionResponse { Id = entity.Id };
}
}