Core.Blueprint.Service/Core.Blueprint.Application/UsesCases/SQL/SQLHandler.cs
Sergio Matias Urquin b2635193dc Add project files.
2025-04-29 18:44:41 -06:00

194 lines
7.1 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.Cerberos.Application.UseCases.UserProjects
{
public class SQLHandler :
IComponentHandler<CreateUserProjectRequest>,
IComponentHandler<GetUserProjectRequest>,
IComponentHandler<GetAllUserProjectsRequest>,
IComponentHandler<UpdateUserProjectRequest>,
IComponentHandler<DeleteUserProjectRequest>
{
private readonly ISQLPort _port;
private readonly IValidator<CreateUserProjectRequest> _createUserProjectValidator;
private readonly IValidator<GetUserProjectRequest> _getUserProjectValidator;
private readonly IValidator<UpdateUserProjectRequest> _updateUserProjectValidator;
private readonly IValidator<DeleteUserProjectRequest> _deleteUserProjectValidator;
private readonly IBlueprintServiceClient _SQLDALService;
public SQLHandler(
ISQLPort port,
IValidator<CreateUserProjectRequest> createUserProjectValidator,
IValidator<GetUserProjectRequest> getUserProjectValidator,
IValidator<UpdateUserProjectRequest> updateUserProjectValidator,
IValidator<DeleteUserProjectRequest> deleteUserProjectValidator,
IBlueprintServiceClient SQLDALService)
{
_port = port ?? throw new ArgumentNullException(nameof(port));
_createUserProjectValidator = createUserProjectValidator ?? throw new ArgumentNullException(nameof(createUserProjectValidator));
_getUserProjectValidator = getUserProjectValidator ?? throw new ArgumentNullException(nameof(getUserProjectValidator));
_updateUserProjectValidator = updateUserProjectValidator ?? throw new ArgumentNullException(nameof(updateUserProjectValidator));
_deleteUserProjectValidator = deleteUserProjectValidator ?? throw new ArgumentNullException(nameof(deleteUserProjectValidator));
_SQLDALService = SQLDALService ?? throw new ArgumentNullException(nameof(SQLDALService));
}
public async ValueTask ExecuteAsync(CreateUserProjectRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_createUserProjectValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new UserProjectRequest
{
ProjectCode = command.ProjectCode,
ProjectDescription = command.ProjectDescription,
UserId = command.UserId,
};
var result = await _SQLDALService.CreateUserProjectAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllUserProjectsRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _SQLDALService.GetAllUserProjectsAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetUserProjectRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _SQLDALService.GetUserProjectByIdAsync(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(UpdateUserProjectRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateUserProjectValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new UserProjectAdapter
{
Id = command.Id,
Guid = command.Guid,
UserId = command.UserId,
ProjectCode = command.ProjectCode,
ProjectDescription = command.ProjectDescription,
CreatedAt = command.CreatedAt,
CreatedBy = command.CreatedBy,
UpdatedAt = command.UpdatedAt,
UpdatedBy = command.UpdatedBy,
Status = command.Status
};
int id = command.Id;
var result = await _SQLDALService.UpdateUserProjectAsync(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(DeleteUserProjectRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_deleteUserProjectValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _SQLDALService.DeleteUserProjectAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}