2026-04-16 13:54:47 -03:00
|
|
|
using System.Transactions;
|
2026-04-15 12:31:29 -03:00
|
|
|
using SIGCM2.Application.Abstractions;
|
|
|
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
2026-04-16 13:54:47 -03:00
|
|
|
using SIGCM2.Application.Audit;
|
2026-04-15 12:31:29 -03:00
|
|
|
using SIGCM2.Application.Roles.Dtos;
|
|
|
|
|
using SIGCM2.Domain.Entities;
|
|
|
|
|
using SIGCM2.Domain.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace SIGCM2.Application.Roles.Create;
|
|
|
|
|
|
|
|
|
|
public sealed class CreateRolCommandHandler : ICommandHandler<CreateRolCommand, RolCreatedDto>
|
|
|
|
|
{
|
|
|
|
|
private readonly IRolRepository _repository;
|
2026-04-16 13:54:47 -03:00
|
|
|
private readonly IAuditLogger _audit;
|
2026-04-15 12:31:29 -03:00
|
|
|
|
2026-04-16 13:54:47 -03:00
|
|
|
public CreateRolCommandHandler(IRolRepository repository, IAuditLogger audit)
|
2026-04-15 12:31:29 -03:00
|
|
|
{
|
|
|
|
|
_repository = repository;
|
2026-04-16 13:54:47 -03:00
|
|
|
_audit = audit;
|
2026-04-15 12:31:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<RolCreatedDto> Handle(CreateRolCommand command)
|
|
|
|
|
{
|
|
|
|
|
// Check-then-insert: explicit check produces a clear 409 message.
|
|
|
|
|
// SqlException 2627 (UQ violation) acts as race-condition fallback — caught in ExceptionFilter.
|
|
|
|
|
var existing = await _repository.GetByCodigoAsync(command.Codigo);
|
|
|
|
|
if (existing is not null)
|
|
|
|
|
throw new RolAlreadyExistsException(command.Codigo);
|
|
|
|
|
|
|
|
|
|
var rol = Rol.ForCreation(command.Codigo, command.Nombre, command.Descripcion);
|
2026-04-16 13:54:47 -03:00
|
|
|
|
|
|
|
|
int newId;
|
|
|
|
|
using (var tx = new TransactionScope(
|
|
|
|
|
TransactionScopeOption.Required,
|
|
|
|
|
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
|
|
|
|
|
TransactionScopeAsyncFlowOption.Enabled))
|
|
|
|
|
{
|
|
|
|
|
newId = await _repository.AddAsync(rol);
|
|
|
|
|
|
|
|
|
|
await _audit.LogAsync(
|
|
|
|
|
action: "rol.create",
|
|
|
|
|
targetType: "Rol",
|
|
|
|
|
targetId: newId.ToString(),
|
|
|
|
|
metadata: new { after = new { rol.Codigo, rol.Nombre, rol.Descripcion } });
|
|
|
|
|
|
|
|
|
|
tx.Complete();
|
|
|
|
|
}
|
2026-04-15 12:31:29 -03:00
|
|
|
|
|
|
|
|
return new RolCreatedDto(
|
|
|
|
|
Id: newId,
|
|
|
|
|
Codigo: rol.Codigo,
|
|
|
|
|
Nombre: rol.Nombre,
|
|
|
|
|
Descripcion: rol.Descripcion,
|
|
|
|
|
Activo: rol.Activo);
|
|
|
|
|
}
|
|
|
|
|
}
|