31 lines
1012 B
C#
31 lines
1012 B
C#
|
|
using SIGCM2.Application.Abstractions;
|
||
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
||
|
|
using SIGCM2.Application.Roles.Dtos;
|
||
|
|
using SIGCM2.Domain.Exceptions;
|
||
|
|
|
||
|
|
namespace SIGCM2.Application.Roles.Update;
|
||
|
|
|
||
|
|
public sealed class UpdateRolCommandHandler : ICommandHandler<UpdateRolCommand, RolDto>
|
||
|
|
{
|
||
|
|
private readonly IRolRepository _repository;
|
||
|
|
|
||
|
|
public UpdateRolCommandHandler(IRolRepository repository)
|
||
|
|
{
|
||
|
|
_repository = repository;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<RolDto> Handle(UpdateRolCommand command)
|
||
|
|
{
|
||
|
|
var updated = await _repository.UpdateAsync(
|
||
|
|
command.Codigo, command.Nombre, command.Descripcion, command.Activo);
|
||
|
|
|
||
|
|
if (!updated)
|
||
|
|
throw new RolNotFoundException(command.Codigo);
|
||
|
|
|
||
|
|
var rol = await _repository.GetByCodigoAsync(command.Codigo)
|
||
|
|
?? throw new RolNotFoundException(command.Codigo);
|
||
|
|
|
||
|
|
return new RolDto(rol.Id, rol.Codigo, rol.Nombre, rol.Descripcion, rol.Activo, rol.FechaCreacion, rol.FechaModificacion);
|
||
|
|
}
|
||
|
|
}
|