2026-04-16 18:53:57 -03:00
|
|
|
using System.Transactions;
|
|
|
|
|
using SIGCM2.Application.Abstractions;
|
|
|
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
|
|
|
|
using SIGCM2.Application.Audit;
|
|
|
|
|
using SIGCM2.Domain.Entities;
|
|
|
|
|
using SIGCM2.Domain.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace SIGCM2.Application.Medios.Update;
|
|
|
|
|
|
|
|
|
|
public sealed class UpdateMedioCommandHandler : ICommandHandler<UpdateMedioCommand, MedioUpdatedDto>
|
|
|
|
|
{
|
|
|
|
|
private readonly IMedioRepository _repo;
|
|
|
|
|
private readonly IAuditLogger _audit;
|
2026-04-18 10:12:17 -03:00
|
|
|
private readonly TimeProvider _timeProvider;
|
2026-04-16 18:53:57 -03:00
|
|
|
|
2026-04-18 10:12:17 -03:00
|
|
|
public UpdateMedioCommandHandler(IMedioRepository repo, IAuditLogger audit, TimeProvider timeProvider)
|
2026-04-16 18:53:57 -03:00
|
|
|
{
|
|
|
|
|
_repo = repo;
|
|
|
|
|
_audit = audit;
|
2026-04-18 10:12:17 -03:00
|
|
|
_timeProvider = timeProvider;
|
2026-04-16 18:53:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<MedioUpdatedDto> Handle(UpdateMedioCommand command)
|
|
|
|
|
{
|
|
|
|
|
var target = await _repo.GetByIdAsync(command.Id)
|
|
|
|
|
?? throw new MedioNotFoundException(command.Id);
|
|
|
|
|
|
2026-04-18 10:12:17 -03:00
|
|
|
var now = _timeProvider.GetUtcNow().UtcDateTime;
|
|
|
|
|
var updated = target.WithUpdatedProfile(command.Nombre, command.Tipo, command.PlataformaEmpresaId, now);
|
2026-04-16 18:53:57 -03:00
|
|
|
|
|
|
|
|
using var tx = new TransactionScope(
|
|
|
|
|
TransactionScopeOption.Required,
|
|
|
|
|
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
|
|
|
|
|
TransactionScopeAsyncFlowOption.Enabled);
|
|
|
|
|
|
|
|
|
|
await _repo.UpdateAsync(updated);
|
|
|
|
|
|
|
|
|
|
await _audit.LogAsync(
|
|
|
|
|
action: "medio.update",
|
|
|
|
|
targetType: "Medio",
|
|
|
|
|
targetId: command.Id.ToString(),
|
|
|
|
|
metadata: new
|
|
|
|
|
{
|
|
|
|
|
before = new { target.Nombre, target.Tipo, target.PlataformaEmpresaId },
|
|
|
|
|
after = new { updated.Nombre, updated.Tipo, updated.PlataformaEmpresaId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tx.Complete();
|
|
|
|
|
|
|
|
|
|
return new MedioUpdatedDto(
|
|
|
|
|
Id: updated.Id,
|
|
|
|
|
Codigo: updated.Codigo,
|
|
|
|
|
Nombre: updated.Nombre,
|
|
|
|
|
Tipo: updated.Tipo,
|
|
|
|
|
PlataformaEmpresaId: updated.PlataformaEmpresaId,
|
|
|
|
|
Activo: updated.Activo);
|
|
|
|
|
}
|
|
|
|
|
}
|