Add project files.

This commit is contained in:
Sergio Matias Urquin
2025-04-29 18:56:29 -06:00
parent ad652bc070
commit 7d760b589e
91 changed files with 4366 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
using Core.Cerberos.Adapters;
using Core.Cerberos.Application.UseCases.Roles.Ports;
using Lib.Architecture.BuildingBlocks;
using Microsoft.AspNetCore.Mvc;
namespace Core.Cerberos.Application.UseCases.Roles.Adapter
{
public class RolePort : BasePresenter, IRolePort
{
public void Success(RoleAdapter output)
{
ViewModel = new OkObjectResult(output);
}
public void Success(List<RoleAdapter> output)
{
ViewModel = new OkObjectResult(output);
}
}
}

View File

@@ -0,0 +1,15 @@
using Core.Cerberos.Adapters.Common.Enums;
using Lib.Architecture.BuildingBlocks;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class AddApplicationToRoleRequest : Notificator, ICommand
{
public string RoleId { get; set; }
public ApplicationsEnum Application { get; set; }
public bool Validate()
{
return true;
}
}
}

View File

@@ -0,0 +1,15 @@
using Core.Cerberos.Adapters.Common.Enums;
using Lib.Architecture.BuildingBlocks;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class ChangeRoleStatusRequest : Notificator, ICommand
{
public string Id { get; set; }
public StatusEnum Status { get; set; }
public bool Validate()
{
return Id != null;
}
}
}

View File

@@ -0,0 +1,22 @@
using Core.Cerberos.Adapters.Common.Enums;
using Lib.Architecture.BuildingBlocks;
using System.Text.Json.Serialization;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class CreateRoleRequest : Notificator, ICommand
{
public string Name { get; set; } = null!;
public string Description { get; set; } = null!;
[JsonConverter(typeof(EnumArrayJsonConverter<ApplicationsEnum>))]
public ApplicationsEnum[]? Applications { get; set; } = null!;
public string[] Modules { get; set; } = null!;
public string[] Permissions { get; set; } = null!;
public bool Validate()
{
return Name != null;
}
}
}

View File

@@ -0,0 +1,12 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class GetAllRolesRequest : ICommand
{
public bool Validate()
{
return true;
}
}
}

View File

@@ -0,0 +1,13 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class GetRoleRequest : Notificator, ICommand
{
public string Id { get; set; }
public bool Validate()
{
return Id != null;
}
}
}

View File

@@ -0,0 +1,15 @@
using Core.Cerberos.Adapters.Common.Enums;
using Lib.Architecture.BuildingBlocks;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class RemoveApplicationFromRoleRequest : Notificator, ICommand
{
public string RoleId { get; set; }
public ApplicationsEnum Application { get; set; }
public bool Validate()
{
return true;
}
}
}

View File

@@ -0,0 +1,24 @@
using Core.Cerberos.Adapters.Common.Enums;
using Lib.Architecture.BuildingBlocks;
using System.Text.Json.Serialization;
namespace Core.Cerberos.Application.UseCases.Roles.Input
{
public class UpdateRoleRequest : Notificator, ICommand
{
public string Id { get; set; } = null!;
public string Name { get; set; } = null!;
public string? Description { get; set; }
[JsonConverter(typeof(EnumArrayJsonConverter<ApplicationsEnum>))]
public ApplicationsEnum[]? Applications { get; set; }
public string[] Modules { get; set; } = null!;
public string[] Permissions { get; set; } = null!;
public StatusEnum Status { get; set; }
public bool Validate()
{
return Id != null;
}
}
}

View File

@@ -0,0 +1,12 @@
using Core.Cerberos.Adapters;
using Lib.Architecture.BuildingBlocks;
namespace Core.Cerberos.Application.UseCases.Roles.Ports
{
public interface IRolePort : IBasePort,
ICommandSuccessPort<RoleAdapter>, ICommandSuccessPort<List<RoleAdapter>>,
INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort,
INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort
{
}
}

View File

@@ -0,0 +1,235 @@
using Core.Cerberos.Adapters;
using Core.Cerberos.Application.UseCases.Roles.Input;
using Core.Cerberos.Application.UseCases.Roles.Ports;
using Core.Cerberos.External.Clients;
using Core.Cerberos.External.Clients.Requests;
using FluentValidation;
using Lib.Architecture.BuildingBlocks;
using Lib.Architecture.BuildingBlocks.Helpers;
namespace Core.Cerberos.Application.UseCases.Role
{
public class RoleHandler :
IComponentHandler<ChangeRoleStatusRequest>,
IComponentHandler<GetAllRolesRequest>,
IComponentHandler<UpdateRoleRequest>,
IComponentHandler<CreateRoleRequest>,
IComponentHandler<GetRoleRequest>,
IComponentHandler<AddApplicationToRoleRequest>,
IComponentHandler<RemoveApplicationFromRoleRequest>
{
private readonly IRolePort _port;
private readonly IValidator<ChangeRoleStatusRequest> _changeRoleStatusValidator;
private readonly IValidator<CreateRoleRequest> _registerRoleValidator;
private readonly IValidator<UpdateRoleRequest> _updateRoleValidator;
private readonly ICerberosServiceClient _cerberosDALService;
public RoleHandler(
IRolePort port,
IValidator<ChangeRoleStatusRequest> changeRoleStatusValidator,
IValidator<CreateRoleRequest> registerRoleValidator,
IValidator<UpdateRoleRequest> updateRoleValidator,
ICerberosServiceClient cerberosDALService)
{
_port = port ?? throw new ArgumentNullException(nameof(port));
_changeRoleStatusValidator = changeRoleStatusValidator ?? throw new ArgumentNullException(nameof(changeRoleStatusValidator));
_registerRoleValidator = registerRoleValidator ?? throw new ArgumentNullException(nameof(registerRoleValidator));
_updateRoleValidator = updateRoleValidator ?? throw new ArgumentNullException(nameof(updateRoleValidator));
_cerberosDALService = cerberosDALService ?? throw new ArgumentNullException(nameof(cerberosDALService));
}
public async ValueTask ExecuteAsync(GetRoleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _cerberosDALService.GetRoleByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllRolesRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _cerberosDALService.GetAllRolesAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(ChangeRoleStatusRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_changeRoleStatusValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _cerberosDALService.ChangeRoleStatusAsync(command.Id, command.Status, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(CreateRoleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_registerRoleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new RoleRequest
{
Name = command.Name,
Description = command.Description,
Applications = command.Applications,
Modules = command.Modules,
Permissions = command.Permissions
};
var result = await _cerberosDALService.CreateRoleAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(UpdateRoleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateRoleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new RoleAdapter
{
Id = command.Id,
Name = command.Name,
Description = command.Description,
Applications = command.Applications,
Modules = command.Modules,
Permissions = command.Permissions,
Status = command.Status
};
string id = command.Id;
var result = await _cerberosDALService.UpdateRoleAsync(request, id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(AddApplicationToRoleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _cerberosDALService.AddApplicationToRoleAsync(command.RoleId, command.Application, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(RemoveApplicationFromRoleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _cerberosDALService.RemoveApplicationToRoleAsync(command.RoleId, command.Application, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}

View File

@@ -0,0 +1,16 @@
using Core.Cerberos.Application.UseCases.Roles.Input;
using FluentValidation;
namespace Core.Cerberos.Application.UseCases.Roles.Validator
{
public class ChangeRoleStatusValidator : AbstractValidator<ChangeRoleStatusRequest>
{
public ChangeRoleStatusValidator()
{
RuleFor(i => i.Id).NotEmpty().NotNull().OverridePropertyName(x => x.Id).WithName("Role ID").WithMessage("Role ID is Obligatory.");
RuleFor(i => i.Status).NotNull().OverridePropertyName(x => x.Status).WithName("Status").WithMessage("Status is Obligatory.");
}
}
}

View File

@@ -0,0 +1,16 @@
using Core.Cerberos.Application.UseCases.Roles.Input;
using FluentValidation;
namespace Core.Cerberos.Application.UseCases.Roles.Validator
{
public class CreateRoleValidator : AbstractValidator<CreateRoleRequest>
{
public CreateRoleValidator()
{
RuleFor(i => i.Name).NotEmpty().NotNull().OverridePropertyName(x => x.Name).WithName("Role Name").WithMessage("Role Name is Obligatory.");
RuleFor(i => i.Description).NotEmpty().NotNull().OverridePropertyName(x => x.Description).WithName("Role Description").WithMessage("Role Description is Obligatory.");
RuleFor(i => i.Applications).NotNull().OverridePropertyName(x => x.Applications).WithName("AccesLevel").WithMessage("Role must have at least one application.");
RuleFor(i => i.Permissions).NotEmpty().NotNull().OverridePropertyName(x => x.Applications).WithName("Role permissions").WithMessage("Role Permissions are Obligatory.");
}
}
}

View File

@@ -0,0 +1,16 @@
using Core.Cerberos.Application.UseCases.Roles.Input;
using FluentValidation;
namespace Core.Cerberos.Application.UseCases.Roles.Validator
{
public class UpdateRoleValidator : AbstractValidator<UpdateRoleRequest>
{
public UpdateRoleValidator()
{
RuleFor(i => i.Name).NotEmpty().NotNull().OverridePropertyName(x => x.Name).WithName("Role Name").WithMessage("Role Name is Obligatory.");
RuleFor(i => i.Description).NotEmpty().NotNull().OverridePropertyName(x => x.Description).WithName("Role Description").WithMessage("Role Description is Obligatory.");
RuleFor(i => i.Applications).NotEmpty().NotNull().OverridePropertyName(x => x.Applications).WithName("Role applications").WithMessage("Role Applications are Obligatory.");
RuleFor(i => i.Permissions).NotEmpty().NotNull().OverridePropertyName(x => x.Applications).WithName("Role permissions").WithMessage("Role Permissions are Obligatory.");
}
}
}