Core.Thalos.Service.API/Core.Cerberos.Application/UseCases/Modules/ModuleHandler.cs
Sergio Matias Urquin 7d760b589e Add project files.
2025-04-29 18:56:29 -06:00

220 lines
7.9 KiB
C#

using Core.Cerberos.Adapters;
using Core.Cerberos.Application.UseCases.Modules.Input;
using Core.Cerberos.Application.UseCases.Modules.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.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 ICerberosServiceClient _cerberosDALService;
public ModuleHandler(
IModulePort port,
IValidator<ChangeModuleStatusRequest> changeModuleStatusValidator,
IValidator<CreateModuleRequest> registerModuleValidator,
IValidator<UpdateModuleRequest> updateModuleValidator,
IValidator<GetAllModulesByListRequest> modulesByListValidator,
ICerberosServiceClient cerberosDALService)
{
_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));
_cerberosDALService = cerberosDALService ?? throw new ArgumentNullException(nameof(cerberosDALService));
_modulesByListValidator = modulesByListValidator ?? throw new ArgumentNullException(nameof(modulesByListValidator));
}
public async ValueTask ExecuteAsync(GetModuleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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,
Status = command.Status
};
string id = command.Id;
var result = await _cerberosDALService.UpdateModuleAsync(request, id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}