Files
Core.Blueprint.Service/Core.Blueprint.Application/UsesCases/Mongo/MongoHandler.cs
Sergio Matias Urquin b2635193dc Add project files.
2025-04-29 18:44:41 -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.Cerberos.Application.UseCases.Blueprints
{
public class MongoHandler :
IComponentHandler<CreateBlueprintRequest>,
IComponentHandler<GetBlueprintRequest>,
IComponentHandler<GetAllBlueprintsRequest>,
IComponentHandler<UpdateBlueprintRequest>,
IComponentHandler<DeleteBlueprintRequest>
{
private readonly IMongoPort _port;
private readonly IValidator<CreateBlueprintRequest> _createBlueprintValidator;
private readonly IValidator<GetBlueprintRequest> _getBlueprintValidator;
private readonly IValidator<UpdateBlueprintRequest> _updateBlueprintValidator;
private readonly IValidator<DeleteBlueprintRequest> _deleteBlueprintValidator;
private readonly IBlueprintServiceClient _mongoDALService;
public MongoHandler(
IMongoPort port,
IValidator<CreateBlueprintRequest> createBlueprintValidator,
IValidator<GetBlueprintRequest> getBlueprintValidator,
IValidator<UpdateBlueprintRequest> updateBlueprintValidator,
IValidator<DeleteBlueprintRequest> deleteBlueprintValidator,
IBlueprintServiceClient mongoDALService)
{
_port = port ?? throw new ArgumentNullException(nameof(port));
_createBlueprintValidator = createBlueprintValidator ?? throw new ArgumentNullException(nameof(createBlueprintValidator));
_getBlueprintValidator = getBlueprintValidator ?? throw new ArgumentNullException(nameof(getBlueprintValidator));
_updateBlueprintValidator = updateBlueprintValidator ?? throw new ArgumentNullException(nameof(updateBlueprintValidator));
_deleteBlueprintValidator = deleteBlueprintValidator ?? throw new ArgumentNullException(nameof(deleteBlueprintValidator));
_mongoDALService = mongoDALService ?? throw new ArgumentNullException(nameof(mongoDALService));
}
public async ValueTask ExecuteAsync(CreateBlueprintRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_createBlueprintValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new BlueprintRequest
{
Name = command.Name,
Description = command.Description,
};
var result = await _mongoDALService.CreateBlueprintAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllBlueprintsRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _mongoDALService.GetAllBlueprintsAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetBlueprintRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_getBlueprintValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _mongoDALService.GetBlueprintByIdAsync(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(UpdateBlueprintRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateBlueprintValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new BlueprintAdapter
{
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 _mongoDALService.UpdateBlueprintAsync(_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(DeleteBlueprintRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_deleteBlueprintValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _mongoDALService.DeleteBlueprintAsync(command._Id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}