Core.Thalos.Service.API/Core.Thalos.Application/UseCases/Permissions/PermissionHandler.cs
2025-07-27 19:33:19 -06:00

204 lines
7.3 KiB
C#

using Core.Thalos.Application.UseCases.Permissions.Input;
using Core.Thalos.Application.UseCases.Permissions.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.Permissions
{
public class PermissionHandler :
IComponentHandler<ChangePermissionStatusRequest>,
IComponentHandler<GetAllPermissionsRequest>,
IComponentHandler<GetAllPermissionsByListRequest>,
IComponentHandler<UpdatePermissionRequest>,
IComponentHandler<GetPermissionRequest>,
IComponentHandler<CreatePermissionRequest>
{
private readonly IPermissionPort _port;
private readonly IValidator<ChangePermissionStatusRequest> _changePermissionStatusValidator;
private readonly IValidator<CreatePermissionRequest> _registerPermissionValidator;
private readonly IValidator<UpdatePermissionRequest> _updatePermissionValidator;
private readonly IThalosServiceClient _thalosDALService;
public PermissionHandler(
IPermissionPort port,
IValidator<ChangePermissionStatusRequest> changePermissionStatusValidator,
IValidator<CreatePermissionRequest> registerPermissionValidator,
IValidator<UpdatePermissionRequest> 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
};
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);
}
}
}
}