Add project files.
This commit is contained in:
		
							
								
								
									
										235
									
								
								Core.Cerberos.Application/UseCases/Roles/RoleHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										235
									
								
								Core.Cerberos.Application/UseCases/Roles/RoleHandler.cs
									
									
									
									
									
										Normal 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); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin