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, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler { private readonly IMongoPort _port; private readonly IValidator _createBlueprintValidator; private readonly IValidator _getBlueprintValidator; private readonly IValidator _updateBlueprintValidator; private readonly IValidator _deleteBlueprintValidator; private readonly IBlueprintServiceClient _mongoDALService; public MongoHandler( IMongoPort port, IValidator createBlueprintValidator, IValidator getBlueprintValidator, IValidator updateBlueprintValidator, IValidator 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); } } } }