227 lines
8.0 KiB
C#
227 lines
8.0 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<DeletePermissionRequest>,
|
|
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(DeletePermissionRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
ArgumentNullException.ThrowIfNull(command);
|
|
|
|
var result = await _thalosDALService.DeletePermissionAsync(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);
|
|
}
|
|
}
|
|
}
|
|
}
|