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

198 lines
7.1 KiB
C#

using Core.Blueprint.Application.UsesCases.Mongo.Input;
using Core.Blueprint.Application.UsesCases.Mongo.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.Mongo
{
public class MongoHandler :
IComponentHandler<CreateMongoSampleRequest>,
IComponentHandler<GetMongoSampleRequest>,
IComponentHandler<GetAllMongoSamplesRequest>,
IComponentHandler<UpdateMongoSampleRequest>,
IComponentHandler<DeleteMongoSampleRequest>
{
private readonly IMongoPort _port;
private readonly IValidator<CreateMongoSampleRequest> _createSampleValidator;
private readonly IValidator<GetMongoSampleRequest> _getSampleValidator;
private readonly IValidator<UpdateMongoSampleRequest> _updateSampleValidator;
private readonly IValidator<DeleteMongoSampleRequest> _deleteSampleValidator;
private readonly IBlueprintServiceClient _blueprintServiceClient;
public MongoHandler(
IMongoPort port,
IValidator<CreateMongoSampleRequest> createSampleValidator,
IValidator<GetMongoSampleRequest> getSampleValidator,
IValidator<UpdateMongoSampleRequest> updateSampleValidator,
IValidator<DeleteMongoSampleRequest> deleteSampleValidator,
IBlueprintServiceClient blueprintServiceClient)
{
_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));
_blueprintServiceClient = blueprintServiceClient ?? throw new ArgumentNullException(nameof(blueprintServiceClient));
}
public async ValueTask ExecuteAsync(CreateMongoSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_createSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new MongoSampleRequest
{
Name = command.Name,
Description = command.Description,
};
var result = await _blueprintServiceClient.CreateMongoSampleAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllMongoSamplesRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _blueprintServiceClient.GetAllMongoSamplesAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetMongoSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_getSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _blueprintServiceClient.GetMongoSampleByIdAsync(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(UpdateMongoSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new MongoSampleAdapter
{
Id = command.Id,
_Id = command._Id,
Name = command.Name,
Description = command.Description,
CreatedAt = command.CreatedAt,
CreatedBy = command.CreatedBy,
UpdatedAt = command.UpdatedAt,
UpdatedBy = command.UpdatedBy,
Status = command.Status
};
string _id = command._Id;
var result = await _blueprintServiceClient.UpdateMongoSampleAsync(_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(DeleteMongoSampleRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_deleteSampleValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _blueprintServiceClient.DeleteMongoSampleAsync(command._Id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}