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, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler { private readonly ISQLPort _port; private readonly IValidator _createUserProjectValidator; private readonly IValidator _getUserProjectValidator; private readonly IValidator _updateUserProjectValidator; private readonly IValidator _deleteUserProjectValidator; private readonly IBlueprintServiceClient _SQLDALService; public SQLHandler( ISQLPort port, IValidator createUserProjectValidator, IValidator getUserProjectValidator, IValidator updateUserProjectValidator, IValidator 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); } } } }