Files
2026-07-21 14:08:10 +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 UpdateEmployeeIncomeRequest
{
public string? Id { get; set; }
public string? IncomeId { get; set; }
public decimal Amount { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; }
}
public class UpdateEmployeeIncomeResponse
{
public string? Id { get; set; }
}
public record UpdateEmployeeIncomeCommand(UpdateEmployeeIncomeRequest Data) : IRequest<UpdateEmployeeIncomeResponse>;
public class UpdateEmployeeIncomeHandler : IRequestHandler<UpdateEmployeeIncomeCommand, UpdateEmployeeIncomeResponse>
{
private readonly AppDbContext _context;
public UpdateEmployeeIncomeHandler(AppDbContext context) => _context = context;
public async Task<UpdateEmployeeIncomeResponse> Handle(UpdateEmployeeIncomeCommand request, CancellationToken cancellationToken)
{
var entity = await _context.EmployeeIncome
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
if (entity == null) throw new NotFoundException("EmployeeIncome", request.Data.Id ?? string.Empty);
entity.IncomeId = request.Data.IncomeId;
entity.Amount = request.Data.Amount;
entity.Description = request.Data.Description;
entity.IsActive = request.Data.IsActive;
await _context.SaveChangesAsync(cancellationToken);
return new UpdateEmployeeIncomeResponse { Id = entity.Id };
}
}