using Core.Thalos.Adapters; using Core.Thalos.Application.UseCases.Permissions.Input; using Core.Thalos.Application.UseCases.Permissions.Ports; 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.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 IThalosServiceClient _thalosDALService; public PermissionHandler( IPermissionPort port, IValidator changePermissionStatusValidator, IValidator registerPermissionValidator, IValidator updatePermissionValidator, IThalosServiceClient thalosDALService) { _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)); _thalosDALService = thalosDALService ?? throw new ArgumentNullException(nameof(thalosDALService)); } public async ValueTask ExecuteAsync(GetPermissionRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); var result = await _thalosDALService.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 _thalosDALService.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 _thalosDALService.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 _thalosDALService.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 _thalosDALService.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 _thalosDALService.UpdatePermissionAsync(request, id, cancellationToken).ConfigureAwait(false); if (result == null) { _port.NoContentSuccess(); return; } _port.Success(result); } catch (Exception ex) { ApiResponseHelper.EvaluatePort(ex, _port); } } } }