Files
Core.Blueprint.Service/Core.Blueprint.Application/UsesCases/SQL/SQLHandler.cs
2025-05-18 15:49:46 -06:00

198 lines
7.0 KiB
C#

using Core.Blueprint.Application.UsesCases.SQL.Input;
using Core.Blueprint.Application.UsesCases.SQL.Ports;
using Core.Blueprint.Service.External.Clients;
using Core.Blueprint.Service.External.Clients.Adapters;
using Core.Blueprint.Service.External.Clients.Requests;
using FluentValidation;
using Lib.Architecture.BuildingBlocks;
using Lib.Architecture.BuildingBlocks.Helpers;
namespace Core.Blueprint.Application.UsesCases.SQL
{
public class SQLHandler :
IComponentHandler<CreateSqlSampleRequest>,
IComponentHandler<GetSqlSampleRequest>,
IComponentHandler<GetAllSqlSamplesRequest>,
IComponentHandler<UpdateSqlSampleRequest>,
IComponentHandler<DeleteSqlSampleRequest>
{
private readonly ISQLPort _port;
private readonly IValidator<CreateSqlSampleRequest> _createSampleValidator;
private readonly IValidator<GetSqlSampleRequest> _getSampleValidator;
private readonly IValidator<UpdateSqlSampleRequest> _updateSampleValidator;
private readonly IValidator<DeleteSqlSampleRequest> _deleteSampleValidator;
private readonly IBlueprintServiceClient _SQLDALService;
public SQLHandler(
ISQLPort port,
IValidator<CreateSqlSampleRequest> createSampleValidator,
IValidator<GetSqlSampleRequest> getSampleValidator,
IValidator<UpdateSqlSampleRequest> updateSampleValidator,
IValidator<DeleteSqlSampleRequest> deleteSampleValidator,
IBlueprintServiceClient SQLDALService)
{
_port = port ?? throw new ArgumentNullException(nameof(port));
_createSampleValidator = createSampleValidator ?? throw new ArgumentNullException(nameof(createSampleValidator));
_getSampleValidator = getSampleValidator ?? throw new ArgumentNullException(nameof(getSampleValidator));
_updateSampleValidator = updateSampleValidator ?? throw new ArgumentNullException(nameof(updateSampleValidator));
_deleteSampleValidator = deleteSampleValidator ?? throw new ArgumentNullException(nameof(deleteSampleValidator));
_SQLDALService = SQLDALService ?? throw new ArgumentNullException(nameof(SQLDALService));
}
public async ValueTask ExecuteAsync(CreateSqlSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_createSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new SqlSampleRequest
{
Name = command.Name,
Description = command.Description,
};
var result = await _SQLDALService.CreateSqlSampleAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllSqlSamplesRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _SQLDALService.GetAllSqlSamplesAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetSqlSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_getSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _SQLDALService.GetSqlSampleByIdAsync(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(UpdateSqlSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new SqlSampleAdapter
{
Id = command.Id,
Guid = command.Guid,
ProjectCode = command.Name,
ProjectDescription = command.Description,
CreatedAt = command.CreatedAt,
CreatedBy = command.CreatedBy,
UpdatedAt = command.UpdatedAt,
UpdatedBy = command.UpdatedBy,
Status = command.Status
};
int id = command.Id;
var result = await _SQLDALService.UpdateSqlSampleAsync(id, request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(DeleteSqlSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_deleteSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _SQLDALService.DeleteSqlSampleAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}