feat(medios,secciones): application layer + handlers TDD — ADM-001 B3+B4
- IMedioRepository, ISeccionRepository interfaces - MediosQuery, SeccionesQuery common records - TipoSeccion static AllowedTipos helper - Medios: 6 use cases (Create/Update/Deactivate/Reactivate/List/GetById) with validators, handlers and DTOs - Secciones: 6 use cases mirroring Medios; Create validates MedioId active via IMedioRepository - 52 unit tests (xUnit + NSubstitute) all green; audit LogAsync asserted per mutating handler - DI registrations for all 12 handlers and validators auto-scanned via AddValidatorsFromAssemblyContaining
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Medios.Create;
|
||||
|
||||
public sealed record CreateMedioCommand(
|
||||
string Codigo,
|
||||
string Nombre,
|
||||
TipoMedio Tipo,
|
||||
int? PlataformaEmpresaId);
|
||||
@@ -0,0 +1,63 @@
|
||||
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.Create;
|
||||
|
||||
public sealed class CreateMedioCommandHandler : ICommandHandler<CreateMedioCommand, MedioCreatedDto>
|
||||
{
|
||||
private readonly IMedioRepository _repo;
|
||||
private readonly IAuditLogger _audit;
|
||||
|
||||
public CreateMedioCommandHandler(IMedioRepository repo, IAuditLogger audit)
|
||||
{
|
||||
_repo = repo;
|
||||
_audit = audit;
|
||||
}
|
||||
|
||||
public async Task<MedioCreatedDto> Handle(CreateMedioCommand command)
|
||||
{
|
||||
var codigoNorm = command.Codigo.ToUpperInvariant();
|
||||
|
||||
var exists = await _repo.ExistsByCodigoAsync(codigoNorm);
|
||||
if (exists)
|
||||
throw new MedioCodigoDuplicadoException(codigoNorm);
|
||||
|
||||
var medio = Medio.ForCreation(codigoNorm, command.Nombre, command.Tipo, command.PlataformaEmpresaId);
|
||||
|
||||
using var tx = new TransactionScope(
|
||||
TransactionScopeOption.Required,
|
||||
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
|
||||
TransactionScopeAsyncFlowOption.Enabled);
|
||||
|
||||
var newId = await _repo.AddAsync(medio);
|
||||
|
||||
await _audit.LogAsync(
|
||||
action: "medio.create",
|
||||
targetType: "Medio",
|
||||
targetId: newId.ToString(),
|
||||
metadata: new
|
||||
{
|
||||
after = new
|
||||
{
|
||||
medio.Codigo,
|
||||
medio.Nombre,
|
||||
medio.Tipo,
|
||||
medio.PlataformaEmpresaId,
|
||||
},
|
||||
});
|
||||
|
||||
tx.Complete();
|
||||
|
||||
return new MedioCreatedDto(
|
||||
Id: newId,
|
||||
Codigo: medio.Codigo,
|
||||
Nombre: medio.Nombre,
|
||||
Tipo: medio.Tipo,
|
||||
PlataformaEmpresaId: medio.PlataformaEmpresaId,
|
||||
Activo: medio.Activo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using FluentValidation;
|
||||
using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Medios.Create;
|
||||
|
||||
public sealed class CreateMedioCommandValidator : AbstractValidator<CreateMedioCommand>
|
||||
{
|
||||
private const int CodigoMaxLength = 30;
|
||||
private const int NombreMaxLength = 100;
|
||||
|
||||
public CreateMedioCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.Codigo)
|
||||
.NotEmpty().WithMessage("El código es requerido.")
|
||||
.MaximumLength(CodigoMaxLength).WithMessage($"El código no puede superar los {CodigoMaxLength} caracteres.")
|
||||
.Matches(@"^[A-Za-z0-9_]+$").WithMessage("El código solo puede contener letras, dígitos y guiones bajos.");
|
||||
|
||||
RuleFor(x => x.Nombre)
|
||||
.NotEmpty().WithMessage("El nombre es requerido.")
|
||||
.MaximumLength(NombreMaxLength).WithMessage($"El nombre no puede superar los {NombreMaxLength} caracteres.");
|
||||
|
||||
RuleFor(x => x.Tipo)
|
||||
.IsInEnum().WithMessage("El tipo de medio no es válido.");
|
||||
}
|
||||
}
|
||||
11
src/api/SIGCM2.Application/Medios/Create/MedioCreatedDto.cs
Normal file
11
src/api/SIGCM2.Application/Medios/Create/MedioCreatedDto.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Medios.Create;
|
||||
|
||||
public sealed record MedioCreatedDto(
|
||||
int Id,
|
||||
string Codigo,
|
||||
string Nombre,
|
||||
TipoMedio Tipo,
|
||||
int? PlataformaEmpresaId,
|
||||
bool Activo);
|
||||
Reference in New Issue
Block a user