72 lines
2.3 KiB
C#
72 lines
2.3 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
|
|
|
public class UpdateIncomeRequest
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Code { get; set; }
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public string? Type { get; set; }
|
|
public bool IsTaxable { get; set; }
|
|
public string? CalculationBase { 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 class UpdateIncomeResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public bool Success { get; set; }
|
|
}
|
|
|
|
public record UpdateIncomeCommand(UpdateIncomeRequest Data) : IRequest<UpdateIncomeResponse>;
|
|
|
|
public class UpdateIncomeHandler : IRequestHandler<UpdateIncomeCommand, UpdateIncomeResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdateIncomeHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdateIncomeResponse> Handle(UpdateIncomeCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.Income
|
|
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Income Component", request.Data.Code ?? string.Empty);
|
|
}
|
|
|
|
var entity = await _context.Income
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
return new UpdateIncomeResponse { Id = request.Data.Id, Success = false };
|
|
}
|
|
|
|
entity.Code = request.Data.Code;
|
|
entity.Name = request.Data.Name;
|
|
entity.Description = request.Data.Description;
|
|
entity.Type = request.Data.Type;
|
|
entity.IsTaxable = request.Data.IsTaxable;
|
|
entity.CalculationBase = request.Data.CalculationBase;
|
|
entity.Status = request.Data.Status;
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new UpdateIncomeResponse
|
|
{
|
|
Id = entity.Id,
|
|
Success = true
|
|
};
|
|
}
|
|
} |