Add project files.
This commit is contained in:
		| @@ -0,0 +1,19 @@ | ||||
| using Core.Cerberos.Adapters; | ||||
| using Core.Cerberos.Application.UseCases.Permissions.Ports; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Adapter | ||||
| { | ||||
|     public class PermissionPort : BasePresenter, IPermissionPort | ||||
|     { | ||||
|         public void Success(PermissionAdapter output) | ||||
|         { | ||||
|             ViewModel = new OkObjectResult(output); | ||||
|         } | ||||
|         public void Success(List<PermissionAdapter> output) | ||||
|         { | ||||
|             ViewModel = new OkObjectResult(output); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Core.Cerberos.Adapters.Common.Enums; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Input | ||||
| { | ||||
|     public class ChangePermissionStatusRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string Id { get; set; } | ||||
|         public StatusEnum Status { get; set; } | ||||
|  | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Id != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,17 @@ | ||||
| using Core.Cerberos.Adapters.Common.Constants; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Input | ||||
| { | ||||
|     public class CreatePermissionRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string Name { get; set; } = null!; | ||||
|         public string Description { get; set; } = null!; | ||||
|         public AccessLevelEnum? AccessLevel { get; set; } = null!; | ||||
|  | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Name != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Input | ||||
| { | ||||
|     public class GetAllPermissionsByListRequest : ICommand | ||||
|     { | ||||
|         public string[]? Permissions { get; set; } | ||||
|  | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Permissions != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Input | ||||
| { | ||||
|     public class GetAllPermissionsRequest : ICommand | ||||
|     { | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return true; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,13 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Input | ||||
| { | ||||
|     public class GetPermissionRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string Id { get; set; } | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Id != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,19 @@ | ||||
| using Core.Cerberos.Adapters.Common.Constants; | ||||
| using Core.Cerberos.Adapters.Common.Enums; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Input | ||||
| { | ||||
|     public class UpdatePermissionRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string Id { get; set; } = null!; | ||||
|         public string Name { get; set; } = null!; | ||||
|         public string? Description { get; set; } | ||||
|         public AccessLevelEnum? AccessLevel { get; set; } = null!; | ||||
|         public StatusEnum Status { get; set; } | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Id != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,204 @@ | ||||
| using Core.Cerberos.Adapters; | ||||
| using Core.Cerberos.Application.UseCases.Permissions.Input; | ||||
| using Core.Cerberos.Application.UseCases.Permissions.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.Permissions | ||||
| { | ||||
|     public class PermissionHandler : | ||||
|         IComponentHandler<ChangePermissionStatusRequest>, | ||||
|         IComponentHandler<GetAllPermissionsRequest>, | ||||
|         IComponentHandler<GetAllPermissionsByListRequest>, | ||||
|         IComponentHandler<UpdatePermissionRequest>, | ||||
|         IComponentHandler<GetPermissionRequest>, | ||||
|         IComponentHandler<CreatePermissionRequest> | ||||
|     { | ||||
|         private readonly IPermissionPort _port; | ||||
|         private readonly IValidator<ChangePermissionStatusRequest> _changePermissionStatusValidator; | ||||
|         private readonly IValidator<CreatePermissionRequest> _registerPermissionValidator; | ||||
|         private readonly IValidator<UpdatePermissionRequest> _updatePermissionValidator; | ||||
|         private readonly ICerberosServiceClient _cerberosDALService; | ||||
|  | ||||
|         public PermissionHandler( | ||||
|             IPermissionPort port, | ||||
|             IValidator<ChangePermissionStatusRequest> changePermissionStatusValidator, | ||||
|             IValidator<CreatePermissionRequest> registerPermissionValidator, | ||||
|             IValidator<UpdatePermissionRequest> updatePermissionValidator, | ||||
|             ICerberosServiceClient cerberosDALService) | ||||
|         { | ||||
|             _port = port ?? throw new ArgumentNullException(nameof(port)); | ||||
|             _changePermissionStatusValidator = changePermissionStatusValidator ?? throw new ArgumentNullException(nameof(changePermissionStatusValidator)); | ||||
|             _registerPermissionValidator = registerPermissionValidator ?? throw new ArgumentNullException(nameof(registerPermissionValidator)); | ||||
|             _updatePermissionValidator = updatePermissionValidator ?? throw new ArgumentNullException(nameof(updatePermissionValidator)); | ||||
|             _cerberosDALService = cerberosDALService ?? throw new ArgumentNullException(nameof(cerberosDALService)); | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(GetPermissionRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 var result = await _cerberosDALService.GetPermissionByIdAsync(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(GetAllPermissionsRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 var _result = await _cerberosDALService.GetAllPermissionsAsync().ConfigureAwait(false); | ||||
|                 if (!_result.Any()) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|                 _port.Success(_result.ToList()); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(GetAllPermissionsByListRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 var _result = await _cerberosDALService.GetAllPermissionsByListAsync(command.Permissions, cancellationToken).ConfigureAwait(false); | ||||
|                 if (!_result.Any()) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|                 _port.Success(_result.ToList()); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(ChangePermissionStatusRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_changePermissionStatusValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var result = await _cerberosDALService.ChangeStatusPermissionAsync(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(CreatePermissionRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_registerPermissionValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var request = new PermissionRequest | ||||
|                 { | ||||
|                     Name = command.Name, | ||||
|                     Description = command.Description, | ||||
|                     AccessLevel = command.AccessLevel | ||||
|                 }; | ||||
|  | ||||
|                 var result = await _cerberosDALService.CreatePermissionAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result == null) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _port.Success(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(UpdatePermissionRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_updatePermissionValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var request = new PermissionAdapter | ||||
|                 { | ||||
|                     Id = command.Id, | ||||
|                     Name = command.Name, | ||||
|                     Description = command.Description, | ||||
|                     AccessLevel = command.AccessLevel, | ||||
|                     Status = command.Status | ||||
|                 }; | ||||
|  | ||||
|                 string id = command.Id; | ||||
|  | ||||
|                 var result = await _cerberosDALService.UpdatePermissionAsync(request, id, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result == null) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _port.Success(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,13 @@ | ||||
| using Core.Cerberos.Adapters; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Ports | ||||
| { | ||||
|     public interface IPermissionPort : IBasePort, | ||||
|         ICommandSuccessPort<PermissionAdapter>, | ||||
|         ICommandSuccessPort<List<PermissionAdapter>>, | ||||
|         INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort, | ||||
|         INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort | ||||
|     { | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,15 @@ | ||||
| using Core.Cerberos.Application.UseCases.Permissions.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Validator | ||||
| { | ||||
|     public class ChangePermissionStatusValidator : AbstractValidator<ChangePermissionStatusRequest> | ||||
|     { | ||||
|         public ChangePermissionStatusValidator() | ||||
|         { | ||||
|             RuleFor(i => i.Id).NotEmpty().NotNull().OverridePropertyName(x => x.Id).WithName("Permission ID").WithMessage("Permission ID is Obligatory."); | ||||
|             RuleFor(i => i.Status).NotNull().OverridePropertyName(x => x.Status).WithName("Status").WithMessage("Status is Obligatory."); | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,15 @@ | ||||
| using Core.Cerberos.Application.UseCases.Permissions.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Validator | ||||
| { | ||||
|     public class CreatePermissionValidator : AbstractValidator<CreatePermissionRequest> | ||||
|     { | ||||
|         public CreatePermissionValidator() | ||||
|         { | ||||
|             RuleFor(i => i.Name).NotEmpty().NotNull().OverridePropertyName(x => x.Name).WithName("Permission Name").WithMessage("Permission Name is Obligatory."); | ||||
|             RuleFor(i => i.Description).NotEmpty().NotNull().OverridePropertyName(x => x.Description).WithName("Permission Description").WithMessage("Permission Description is Obligatory."); | ||||
|             RuleFor(i => i.AccessLevel).NotEmpty().NotNull().OverridePropertyName(x => x.AccessLevel).WithName("AccesLevel").WithMessage("AccesLevel is Obligatory."); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Core.Cerberos.Application.UseCases.Permissions.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Cerberos.Application.UseCases.Permissions.Validator | ||||
| { | ||||
|     public class UpdatePermissionValidator : AbstractValidator<UpdatePermissionRequest> | ||||
|     { | ||||
|         public UpdatePermissionValidator() | ||||
|         { | ||||
|             RuleFor(i => i.Name).NotEmpty().NotNull().OverridePropertyName(x => x.Name).WithName("Permission Name").WithMessage("Permission Name is Obligatory."); | ||||
|             RuleFor(i => i.Description).NotEmpty().NotNull().OverridePropertyName(x => x.Description).WithName("Permission Description").WithMessage("Permission Description is Obligatory."); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin