Core.Thalos.Service.API/Core.Thalos.Application/UseCases/Modules/ModuleHandler.cs
2025-06-10 23:13:54 -06:00

219 lines
7.8 KiB
C#

using Core.Thalos.Adapters;
using Core.Thalos.Application.UseCases.Modules.Input;
using Core.Thalos.Application.UseCases.Modules.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.Modules
{
public class ModuleHandler :
IComponentHandler<ChangeModuleStatusRequest>,
IComponentHandler<GetAllModulesRequest>,
IComponentHandler<GetAllModulesByListRequest>,
IComponentHandler<UpdateModuleRequest>,
IComponentHandler<GetModuleRequest>,
IComponentHandler<CreateModuleRequest>
{
private readonly IModulePort _port;
private readonly IValidator<ChangeModuleStatusRequest> _changeModuleStatusValidator;
private readonly IValidator<CreateModuleRequest> _registerModuleValidator;
private readonly IValidator<UpdateModuleRequest> _updateModuleValidator;
private readonly IValidator<GetAllModulesByListRequest> _modulesByListValidator;
private readonly IThalosServiceClient _thalosDALService;
public ModuleHandler(
IModulePort port,
IValidator<ChangeModuleStatusRequest> changeModuleStatusValidator,
IValidator<CreateModuleRequest> registerModuleValidator,
IValidator<UpdateModuleRequest> updateModuleValidator,
IValidator<GetAllModulesByListRequest> modulesByListValidator,
IThalosServiceClient thalosDALService)
{
_port = port ?? throw new ArgumentNullException(nameof(port));
_changeModuleStatusValidator = changeModuleStatusValidator ?? throw new ArgumentNullException(nameof(changeModuleStatusValidator));
_registerModuleValidator = registerModuleValidator ?? throw new ArgumentNullException(nameof(registerModuleValidator));
_updateModuleValidator = updateModuleValidator ?? throw new ArgumentNullException(nameof(updateModuleValidator));
_thalosDALService = thalosDALService ?? throw new ArgumentNullException(nameof(thalosDALService));
_modulesByListValidator = modulesByListValidator ?? throw new ArgumentNullException(nameof(modulesByListValidator));
}
public async ValueTask ExecuteAsync(GetModuleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _thalosDALService.GetModuleByIdAsync(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(GetAllModulesRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _thalosDALService.GetAllModulesAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllModulesByListRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_modulesByListValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var _result = await _thalosDALService.GetAllModulesByListAsync(command.Modules, cancellationToken).ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(ChangeModuleStatusRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_changeModuleStatusValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _thalosDALService.ChangeStatusModuleAsync(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(CreateModuleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_registerModuleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new ModuleRequest
{
Name = command.Name,
Description = command.Description,
Icon = command.Icon,
Route = command.Route,
Order = command.Order,
Application = command.Application,
};
var result = await _thalosDALService.CreateModuleAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(UpdateModuleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateModuleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new ModuleAdapter
{
Id = command.Id,
Name = command.Name,
Description = command.Description,
Application = command.Application,
Route = command.Route,
Order = command.Order,
Icon = command.Icon
};
string id = command.Id;
var result = await _thalosDALService.UpdateModuleAsync(request, id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}