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, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler { private readonly IRolePort _port; private readonly IValidator _changeRoleStatusValidator; private readonly IValidator _registerRoleValidator; private readonly IValidator _updateRoleValidator; private readonly ICerberosServiceClient _cerberosDALService; public RoleHandler( IRolePort port, IValidator changeRoleStatusValidator, IValidator registerRoleValidator, IValidator 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); } } } }