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, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler { private readonly IPermissionPort _port; private readonly IValidator _changePermissionStatusValidator; private readonly IValidator _registerPermissionValidator; private readonly IValidator _updatePermissionValidator; private readonly ICerberosServiceClient _cerberosDALService; public PermissionHandler( IPermissionPort port, IValidator changePermissionStatusValidator, IValidator registerPermissionValidator, IValidator 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); } } } }