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:
2026-04-16 18:53:57 -03:00
parent bb98dbf217
commit f672de78ce
56 changed files with 1844 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
using SIGCM2.Application.Common;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Abstractions.Persistence;
public interface IMedioRepository
{
Task<int> AddAsync(Medio m, CancellationToken ct = default);
Task<Medio?> GetByIdAsync(int id, CancellationToken ct = default);
Task<bool> ExistsByCodigoAsync(string codigo, CancellationToken ct = default);
Task UpdateAsync(Medio m, CancellationToken ct = default);
Task<PagedResult<Medio>> GetPagedAsync(MediosQuery q, CancellationToken ct = default);
}

View File

@@ -0,0 +1,13 @@
using SIGCM2.Application.Common;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Abstractions.Persistence;
public interface ISeccionRepository
{
Task<int> AddAsync(Seccion s, CancellationToken ct = default);
Task<Seccion?> GetByIdAsync(int id, CancellationToken ct = default);
Task<bool> ExistsByCodigoInMedioAsync(int medioId, string codigo, CancellationToken ct = default);
Task UpdateAsync(Seccion s, CancellationToken ct = default);
Task<PagedResult<Seccion>> GetPagedAsync(SeccionesQuery q, CancellationToken ct = default);
}

View File

@@ -0,0 +1,12 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Common;
/// <summary>Query parameters for listing medios with optional filters and paging.</summary>
public sealed record MediosQuery(
int Page,
int PageSize,
bool? Activo,
TipoMedio? Tipo,
string? Search
);

View File

@@ -0,0 +1,11 @@
namespace SIGCM2.Application.Common;
/// <summary>Query parameters for listing secciones with optional filters and paging.</summary>
public sealed record SeccionesQuery(
int Page,
int PageSize,
int? MedioId,
string? Tipo,
bool? Activo,
string? Search
);

View File

@@ -5,6 +5,12 @@ using SIGCM2.Application.Auth.Login;
using SIGCM2.Application.Auth.Logout;
using SIGCM2.Application.Auth.Refresh;
using SIGCM2.Application.Common;
using SIGCM2.Application.Medios.Create;
using SIGCM2.Application.Medios.Deactivate;
using SIGCM2.Application.Medios.GetById;
using SIGCM2.Application.Medios.List;
using SIGCM2.Application.Medios.Reactivate;
using SIGCM2.Application.Medios.Update;
using SIGCM2.Application.Permisos.Assign;
using SIGCM2.Application.Permisos.Dtos;
using SIGCM2.Application.Permisos.GetByRol;
@@ -15,6 +21,12 @@ using SIGCM2.Application.Roles.Dtos;
using SIGCM2.Application.Roles.Get;
using SIGCM2.Application.Roles.List;
using SIGCM2.Application.Roles.Update;
using SIGCM2.Application.Secciones.Create;
using SIGCM2.Application.Secciones.Deactivate;
using SIGCM2.Application.Secciones.GetById;
using SIGCM2.Application.Secciones.List;
using SIGCM2.Application.Secciones.Reactivate;
using SIGCM2.Application.Secciones.Update;
using SIGCM2.Application.Usuarios.ChangeMyPassword;
using SIGCM2.Application.Usuarios.Create;
using SIGCM2.Application.Usuarios.Deactivate;
@@ -62,6 +74,22 @@ public static class DependencyInjection
services.AddScoped<ICommandHandler<GetUsuarioPermisosQuery, UsuarioPermisosDto>, GetUsuarioPermisosQueryHandler>();
services.AddScoped<ICommandHandler<UpdateUsuarioPermisosOverridesCommand, UsuarioPermisosDto>, UpdateUsuarioPermisosOverridesCommandHandler>();
// Medios (ADM-001)
services.AddScoped<ICommandHandler<CreateMedioCommand, MedioCreatedDto>, CreateMedioCommandHandler>();
services.AddScoped<ICommandHandler<UpdateMedioCommand, MedioUpdatedDto>, UpdateMedioCommandHandler>();
services.AddScoped<ICommandHandler<DeactivateMedioCommand, MedioStatusDto>, DeactivateMedioCommandHandler>();
services.AddScoped<ICommandHandler<ReactivateMedioCommand, MedioStatusDto>, ReactivateMedioCommandHandler>();
services.AddScoped<ICommandHandler<ListMediosQuery, PagedResult<MedioListItemDto>>, ListMediosQueryHandler>();
services.AddScoped<ICommandHandler<GetMedioByIdQuery, MedioDetailDto>, GetMedioByIdQueryHandler>();
// Secciones (ADM-001)
services.AddScoped<ICommandHandler<CreateSeccionCommand, SeccionCreatedDto>, CreateSeccionCommandHandler>();
services.AddScoped<ICommandHandler<UpdateSeccionCommand, SeccionUpdatedDto>, UpdateSeccionCommandHandler>();
services.AddScoped<ICommandHandler<DeactivateSeccionCommand, SeccionStatusDto>, DeactivateSeccionCommandHandler>();
services.AddScoped<ICommandHandler<ReactivateSeccionCommand, SeccionStatusDto>, ReactivateSeccionCommandHandler>();
services.AddScoped<ICommandHandler<ListSeccionesQuery, PagedResult<SeccionListItemDto>>, ListSeccionesQueryHandler>();
services.AddScoped<ICommandHandler<GetSeccionByIdQuery, SeccionDetailDto>, GetSeccionByIdQueryHandler>();
// FluentValidation validators (scans entire Application assembly)
services.AddValidatorsFromAssemblyContaining<LoginCommandValidator>();

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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.");
}
}

View 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);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Medios.Deactivate;
public sealed record DeactivateMedioCommand(int Id);

View File

@@ -0,0 +1,48 @@
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.Deactivate;
public sealed class DeactivateMedioCommandHandler : ICommandHandler<DeactivateMedioCommand, MedioStatusDto>
{
private readonly IMedioRepository _repo;
private readonly IAuditLogger _audit;
public DeactivateMedioCommandHandler(IMedioRepository repo, IAuditLogger audit)
{
_repo = repo;
_audit = audit;
}
public async Task<MedioStatusDto> Handle(DeactivateMedioCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new MedioNotFoundException(command.Id);
// Idempotent: already inactive → return as-is without writing an audit event
if (!target.Activo)
return new MedioStatusDto(target.Id, target.Codigo, target.Activo);
var updated = target.WithActivo(false);
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.deactivate",
targetType: "Medio",
targetId: command.Id.ToString());
tx.Complete();
return new MedioStatusDto(updated.Id, updated.Codigo, updated.Activo);
}
}

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Medios.Deactivate;
public sealed record MedioStatusDto(int Id, string Codigo, bool Activo);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Medios.GetById;
public sealed record GetMedioByIdQuery(int Id);

View File

@@ -0,0 +1,31 @@
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Medios.GetById;
public sealed class GetMedioByIdQueryHandler : ICommandHandler<GetMedioByIdQuery, MedioDetailDto>
{
private readonly IMedioRepository _repo;
public GetMedioByIdQueryHandler(IMedioRepository repo)
{
_repo = repo;
}
public async Task<MedioDetailDto> Handle(GetMedioByIdQuery query)
{
var medio = await _repo.GetByIdAsync(query.Id)
?? throw new MedioNotFoundException(query.Id);
return new MedioDetailDto(
Id: medio.Id,
Codigo: medio.Codigo,
Nombre: medio.Nombre,
Tipo: medio.Tipo,
PlataformaEmpresaId: medio.PlataformaEmpresaId,
Activo: medio.Activo,
FechaCreacion: medio.FechaCreacion,
FechaModificacion: medio.FechaModificacion);
}
}

View File

@@ -0,0 +1,13 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Medios.GetById;
public sealed record MedioDetailDto(
int Id,
string Codigo,
string Nombre,
TipoMedio Tipo,
int? PlataformaEmpresaId,
bool Activo,
DateTime FechaCreacion,
DateTime? FechaModificacion);

View File

@@ -0,0 +1,10 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Medios.List;
public sealed record ListMediosQuery(
int Page = 1,
int PageSize = 20,
bool? Activo = true,
TipoMedio? Tipo = null,
string? Search = null);

View File

@@ -0,0 +1,31 @@
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Medios.List;
public sealed class ListMediosQueryHandler : ICommandHandler<ListMediosQuery, PagedResult<MedioListItemDto>>
{
private readonly IMedioRepository _repo;
public ListMediosQueryHandler(IMedioRepository repo)
{
_repo = repo;
}
public async Task<PagedResult<MedioListItemDto>> Handle(ListMediosQuery query)
{
var page = Math.Max(1, query.Page);
var pageSize = Math.Clamp(query.PageSize, 1, 100);
var repoQuery = new MediosQuery(page, pageSize, query.Activo, query.Tipo, query.Search);
var paged = await _repo.GetPagedAsync(repoQuery);
var items = paged.Items
.Select(m => new MedioListItemDto(m.Id, m.Codigo, m.Nombre, m.Tipo, m.PlataformaEmpresaId, m.Activo))
.ToList();
return new PagedResult<MedioListItemDto>(items, paged.Page, paged.PageSize, paged.Total);
}
}

View File

@@ -0,0 +1,11 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Medios.List;
public sealed record MedioListItemDto(
int Id,
string Codigo,
string Nombre,
TipoMedio Tipo,
int? PlataformaEmpresaId,
bool Activo);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Medios.Reactivate;
public sealed record ReactivateMedioCommand(int Id);

View File

@@ -0,0 +1,49 @@
using System.Transactions;
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Medios.Deactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Medios.Reactivate;
public sealed class ReactivateMedioCommandHandler : ICommandHandler<ReactivateMedioCommand, MedioStatusDto>
{
private readonly IMedioRepository _repo;
private readonly IAuditLogger _audit;
public ReactivateMedioCommandHandler(IMedioRepository repo, IAuditLogger audit)
{
_repo = repo;
_audit = audit;
}
public async Task<MedioStatusDto> Handle(ReactivateMedioCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new MedioNotFoundException(command.Id);
// Idempotent: already active → return as-is without writing an audit event
if (target.Activo)
return new MedioStatusDto(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: "medio.reactivate",
targetType: "Medio",
targetId: command.Id.ToString());
tx.Complete();
return new MedioStatusDto(updated.Id, updated.Codigo, updated.Activo);
}
}

View File

@@ -0,0 +1,11 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Medios.Update;
public sealed record MedioUpdatedDto(
int Id,
string Codigo,
string Nombre,
TipoMedio Tipo,
int? PlataformaEmpresaId,
bool Activo);

View File

@@ -0,0 +1,9 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Medios.Update;
public sealed record UpdateMedioCommand(
int Id,
string Nombre,
TipoMedio Tipo,
int? PlataformaEmpresaId);

View File

@@ -0,0 +1,55 @@
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;
public UpdateMedioCommandHandler(IMedioRepository repo, IAuditLogger audit)
{
_repo = repo;
_audit = audit;
}
public async Task<MedioUpdatedDto> Handle(UpdateMedioCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new MedioNotFoundException(command.Id);
var updated = target.WithUpdatedProfile(command.Nombre, command.Tipo, command.PlataformaEmpresaId);
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);
}
}

View File

@@ -0,0 +1,21 @@
using FluentValidation;
namespace SIGCM2.Application.Medios.Update;
public sealed class UpdateMedioCommandValidator : AbstractValidator<UpdateMedioCommand>
{
private const int NombreMaxLength = 100;
public UpdateMedioCommandValidator()
{
RuleFor(x => x.Id)
.GreaterThan(0).WithMessage("El id debe ser mayor a 0.");
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.");
}
}

View File

@@ -0,0 +1,7 @@
namespace SIGCM2.Application.Secciones.Create;
public sealed record CreateSeccionCommand(
int MedioId,
string Codigo,
string Nombre,
string Tipo);

View File

@@ -0,0 +1,68 @@
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.Secciones.Create;
public sealed class CreateSeccionCommandHandler : ICommandHandler<CreateSeccionCommand, SeccionCreatedDto>
{
private readonly ISeccionRepository _repo;
private readonly IMedioRepository _medioRepo;
private readonly IAuditLogger _audit;
public CreateSeccionCommandHandler(ISeccionRepository repo, IMedioRepository medioRepo, IAuditLogger audit)
{
_repo = repo;
_medioRepo = medioRepo;
_audit = audit;
}
public async Task<SeccionCreatedDto> Handle(CreateSeccionCommand command)
{
// Validate medio exists and is active (REQ-SEC-001)
var medio = await _medioRepo.GetByIdAsync(command.MedioId);
if (medio is null || !medio.Activo)
throw new MedioNotFoundException(command.MedioId);
var exists = await _repo.ExistsByCodigoInMedioAsync(command.MedioId, command.Codigo);
if (exists)
throw new SeccionCodigoDuplicadoEnMedioException(command.MedioId, command.Codigo);
var seccion = Seccion.ForCreation(command.MedioId, command.Codigo, command.Nombre, command.Tipo);
using var tx = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
TransactionScopeAsyncFlowOption.Enabled);
var newId = await _repo.AddAsync(seccion);
await _audit.LogAsync(
action: "seccion.create",
targetType: "Seccion",
targetId: newId.ToString(),
metadata: new
{
after = new
{
seccion.MedioId,
seccion.Codigo,
seccion.Nombre,
seccion.Tipo,
},
});
tx.Complete();
return new SeccionCreatedDto(
Id: newId,
MedioId: seccion.MedioId,
Codigo: seccion.Codigo,
Nombre: seccion.Nombre,
Tipo: seccion.Tipo,
Activo: seccion.Activo);
}
}

View File

@@ -0,0 +1,29 @@
using FluentValidation;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Secciones.Create;
public sealed class CreateSeccionCommandValidator : AbstractValidator<CreateSeccionCommand>
{
private const int CodigoMaxLength = 30;
private const int NombreMaxLength = 100;
public CreateSeccionCommandValidator()
{
RuleFor(x => x.MedioId)
.GreaterThan(0).WithMessage("El medioId debe ser mayor a 0.");
RuleFor(x => x.Codigo)
.NotEmpty().WithMessage("El código es requerido.")
.MaximumLength(CodigoMaxLength).WithMessage($"El código no puede superar los {CodigoMaxLength} caracteres.");
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)
.NotEmpty().WithMessage("El tipo es requerido.")
.Must(t => TipoSeccion.AllowedTipos.Contains(t))
.WithMessage($"El tipo debe ser uno de: {string.Join(", ", TipoSeccion.AllowedTipos)}.");
}
}

View File

@@ -0,0 +1,9 @@
namespace SIGCM2.Application.Secciones.Create;
public sealed record SeccionCreatedDto(
int Id,
int MedioId,
string Codigo,
string Nombre,
string Tipo,
bool Activo);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Secciones.Deactivate;
public sealed record DeactivateSeccionCommand(int Id);

View File

@@ -0,0 +1,48 @@
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.Secciones.Deactivate;
public sealed class DeactivateSeccionCommandHandler : ICommandHandler<DeactivateSeccionCommand, SeccionStatusDto>
{
private readonly ISeccionRepository _repo;
private readonly IAuditLogger _audit;
public DeactivateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit)
{
_repo = repo;
_audit = audit;
}
public async Task<SeccionStatusDto> Handle(DeactivateSeccionCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new SeccionNotFoundException(command.Id);
// Idempotent: already inactive → return as-is without writing an audit event
if (!target.Activo)
return new SeccionStatusDto(target.Id, target.Codigo, target.Activo);
var updated = target.WithActivo(false);
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.deactivate",
targetType: "Seccion",
targetId: command.Id.ToString());
tx.Complete();
return new SeccionStatusDto(updated.Id, updated.Codigo, updated.Activo);
}
}

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Secciones.Deactivate;
public sealed record SeccionStatusDto(int Id, string Codigo, bool Activo);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Secciones.GetById;
public sealed record GetSeccionByIdQuery(int Id);

View File

@@ -0,0 +1,31 @@
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Secciones.GetById;
public sealed class GetSeccionByIdQueryHandler : ICommandHandler<GetSeccionByIdQuery, SeccionDetailDto>
{
private readonly ISeccionRepository _repo;
public GetSeccionByIdQueryHandler(ISeccionRepository repo)
{
_repo = repo;
}
public async Task<SeccionDetailDto> Handle(GetSeccionByIdQuery query)
{
var seccion = await _repo.GetByIdAsync(query.Id)
?? throw new SeccionNotFoundException(query.Id);
return new SeccionDetailDto(
Id: seccion.Id,
MedioId: seccion.MedioId,
Codigo: seccion.Codigo,
Nombre: seccion.Nombre,
Tipo: seccion.Tipo,
Activo: seccion.Activo,
FechaCreacion: seccion.FechaCreacion,
FechaModificacion: seccion.FechaModificacion);
}
}

View File

@@ -0,0 +1,11 @@
namespace SIGCM2.Application.Secciones.GetById;
public sealed record SeccionDetailDto(
int Id,
int MedioId,
string Codigo,
string Nombre,
string Tipo,
bool Activo,
DateTime FechaCreacion,
DateTime? FechaModificacion);

View File

@@ -0,0 +1,9 @@
namespace SIGCM2.Application.Secciones.List;
public sealed record ListSeccionesQuery(
int Page = 1,
int PageSize = 20,
int? MedioId = null,
string? Tipo = null,
bool? Activo = true,
string? Search = null);

View File

@@ -0,0 +1,30 @@
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
namespace SIGCM2.Application.Secciones.List;
public sealed class ListSeccionesQueryHandler : ICommandHandler<ListSeccionesQuery, PagedResult<SeccionListItemDto>>
{
private readonly ISeccionRepository _repo;
public ListSeccionesQueryHandler(ISeccionRepository repo)
{
_repo = repo;
}
public async Task<PagedResult<SeccionListItemDto>> Handle(ListSeccionesQuery query)
{
var page = Math.Max(1, query.Page);
var pageSize = Math.Clamp(query.PageSize, 1, 100);
var repoQuery = new SeccionesQuery(page, pageSize, query.MedioId, query.Tipo, query.Activo, query.Search);
var paged = await _repo.GetPagedAsync(repoQuery);
var items = paged.Items
.Select(s => new SeccionListItemDto(s.Id, s.MedioId, s.Codigo, s.Nombre, s.Tipo, s.Activo))
.ToList();
return new PagedResult<SeccionListItemDto>(items, paged.Page, paged.PageSize, paged.Total);
}
}

View File

@@ -0,0 +1,9 @@
namespace SIGCM2.Application.Secciones.List;
public sealed record SeccionListItemDto(
int Id,
int MedioId,
string Codigo,
string Nombre,
string Tipo,
bool Activo);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Secciones.Reactivate;
public sealed record ReactivateSeccionCommand(int Id);

View File

@@ -0,0 +1,49 @@
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);
}
}

View File

@@ -0,0 +1,9 @@
namespace SIGCM2.Application.Secciones.Update;
public sealed record SeccionUpdatedDto(
int Id,
int MedioId,
string Codigo,
string Nombre,
string Tipo,
bool Activo);

View File

@@ -0,0 +1,6 @@
namespace SIGCM2.Application.Secciones.Update;
public sealed record UpdateSeccionCommand(
int Id,
string Nombre,
string Tipo);

View File

@@ -0,0 +1,55 @@
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.Secciones.Update;
public sealed class UpdateSeccionCommandHandler : ICommandHandler<UpdateSeccionCommand, SeccionUpdatedDto>
{
private readonly ISeccionRepository _repo;
private readonly IAuditLogger _audit;
public UpdateSeccionCommandHandler(ISeccionRepository repo, IAuditLogger audit)
{
_repo = repo;
_audit = audit;
}
public async Task<SeccionUpdatedDto> Handle(UpdateSeccionCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new SeccionNotFoundException(command.Id);
var updated = target.WithUpdatedProfile(command.Nombre, command.Tipo);
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.update",
targetType: "Seccion",
targetId: command.Id.ToString(),
metadata: new
{
before = new { target.Nombre, target.Tipo },
after = new { updated.Nombre, updated.Tipo },
});
tx.Complete();
return new SeccionUpdatedDto(
Id: updated.Id,
MedioId: updated.MedioId,
Codigo: updated.Codigo,
Nombre: updated.Nombre,
Tipo: updated.Tipo,
Activo: updated.Activo);
}
}

View File

@@ -0,0 +1,24 @@
using FluentValidation;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Secciones.Update;
public sealed class UpdateSeccionCommandValidator : AbstractValidator<UpdateSeccionCommand>
{
private const int NombreMaxLength = 100;
public UpdateSeccionCommandValidator()
{
RuleFor(x => x.Id)
.GreaterThan(0).WithMessage("El id debe ser mayor a 0.");
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)
.NotEmpty().WithMessage("El tipo es requerido.")
.Must(t => TipoSeccion.AllowedTipos.Contains(t))
.WithMessage($"El tipo debe ser uno de: {string.Join(", ", TipoSeccion.AllowedTipos)}.");
}
}

View File

@@ -0,0 +1,10 @@
namespace SIGCM2.Domain.Entities;
/// <summary>
/// Allowed string values for Seccion.Tipo.
/// Enforced at application layer (validator) and database layer (CHECK constraint).
/// </summary>
public static class TipoSeccion
{
public static readonly string[] AllowedTipos = { "clasificados", "notables", "suplementos" };
}