50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
|
|
using System.Transactions;
|
||
|
|
using SIGCM2.Application.Abstractions;
|
||
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
||
|
|
using SIGCM2.Application.Audit;
|
||
|
|
using SIGCM2.Application.Secciones.Deactivate;
|
||
|
|
using SIGCM2.Domain.Entities;
|
||
|
|
using SIGCM2.Domain.Exceptions;
|
||
|
|
|
||
|
|
namespace SIGCM2.Application.Secciones.Reactivate;
|
||
|
|
|
||
|
|
public sealed class ReactivateSeccionCommandHandler : ICommandHandler<ReactivateSeccionCommand, SeccionStatusDto>
|
||
|
|
{
|
||
|
|
private readonly ISeccionRepository _repo;
|
||
|
|
private readonly IAuditLogger _audit;
|
||
|
|
|
||
|
|
public ReactivateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit)
|
||
|
|
{
|
||
|
|
_repo = repo;
|
||
|
|
_audit = audit;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<SeccionStatusDto> Handle(ReactivateSeccionCommand command)
|
||
|
|
{
|
||
|
|
var target = await _repo.GetByIdAsync(command.Id)
|
||
|
|
?? throw new SeccionNotFoundException(command.Id);
|
||
|
|
|
||
|
|
// Idempotent: already active → return as-is without writing an audit event
|
||
|
|
if (target.Activo)
|
||
|
|
return new SeccionStatusDto(target.Id, target.Codigo, target.Activo);
|
||
|
|
|
||
|
|
var updated = target.WithActivo(true);
|
||
|
|
|
||
|
|
using var tx = new TransactionScope(
|
||
|
|
TransactionScopeOption.Required,
|
||
|
|
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
|
||
|
|
TransactionScopeAsyncFlowOption.Enabled);
|
||
|
|
|
||
|
|
await _repo.UpdateAsync(updated);
|
||
|
|
|
||
|
|
await _audit.LogAsync(
|
||
|
|
action: "seccion.reactivate",
|
||
|
|
targetType: "Seccion",
|
||
|
|
targetId: command.Id.ToString());
|
||
|
|
|
||
|
|
tx.Complete();
|
||
|
|
|
||
|
|
return new SeccionStatusDto(updated.Id, updated.Codigo, updated.Activo);
|
||
|
|
}
|
||
|
|
}
|