235 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			235 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Thalos.Application.UseCases.Roles.Input;
 | |
| using Core.Thalos.Application.UseCases.Roles.Ports;
 | |
| using Core.Thalos.BuildingBlocks;
 | |
| using Core.Thalos.External.Clients;
 | |
| using Core.Thalos.External.Clients.Requests;
 | |
| using FluentValidation;
 | |
| using Lib.Architecture.BuildingBlocks;
 | |
| using Lib.Architecture.BuildingBlocks.Helpers;
 | |
| 
 | |
| namespace Core.Thalos.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 IThalosServiceClient _thalosDALService;
 | |
| 
 | |
|         public RoleHandler(
 | |
|             IRolePort port,
 | |
|             IValidator<ChangeRoleStatusRequest> changeRoleStatusValidator,
 | |
|             IValidator<CreateRoleRequest> registerRoleValidator,
 | |
|             IValidator<UpdateRoleRequest> updateRoleValidator,
 | |
|             IThalosServiceClient thalosDALService)
 | |
|         {
 | |
|             _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));
 | |
|             _thalosDALService = thalosDALService ?? throw new ArgumentNullException(nameof(thalosDALService));
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(GetRoleRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.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 _thalosDALService.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 _thalosDALService.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 _thalosDALService.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
 | |
|                 };
 | |
| 
 | |
|                 string id = command.Id;
 | |
| 
 | |
|                 var result = await _thalosDALService.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 _thalosDALService.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 _thalosDALService.RemoveApplicationToRoleAsync(command.RoleId, command.Application, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | 
