using Core.Thalos.Application.UseCases.Tenants.Input; using Core.Thalos.Application.UseCases.Tenants.Ports; using Core.Thalos.BuildingBlocks; using Core.Thalos.External.Clients; using FluentValidation; using Lib.Architecture.BuildingBlocks; using Lib.Architecture.BuildingBlocks.Helpers; namespace Core.Thalos.Application.UseCases.Tenants { public class TenantHandler : IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler, IComponentHandler { private readonly ITenantPort _port; private readonly IValidator _changeTenantStatusValidator; private readonly IValidator _registerTenantValidator; private readonly IValidator _updateTenantValidator; private readonly IThalosServiceClient _thalosDALService; public TenantHandler( ITenantPort port, IValidator changeTenantStatusValidator, IValidator registerTenantValidator, IValidator updateTenantValidator, IThalosServiceClient thalosDALService) { _port = port ?? throw new ArgumentNullException(nameof(port)); _changeTenantStatusValidator = changeTenantStatusValidator ?? throw new ArgumentNullException(nameof(changeTenantStatusValidator)); _registerTenantValidator = registerTenantValidator ?? throw new ArgumentNullException(nameof(registerTenantValidator)); _updateTenantValidator = updateTenantValidator ?? throw new ArgumentNullException(nameof(updateTenantValidator)); _thalosDALService = thalosDALService ?? throw new ArgumentNullException(nameof(thalosDALService)); } public async ValueTask ExecuteAsync(GetTenantRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); var result = await _thalosDALService.GetTenantByIdAsync(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(DeleteTenantRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); var result = await _thalosDALService.DeleteTenantAsync(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(GetAllTenantsRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); var _result = await _thalosDALService.GetAllTenantsAsync().ConfigureAwait(false); if (!_result.Any()) { _port.NoContentSuccess(); return; } _port.Success(_result.ToList()); } catch (Exception ex) { ApiResponseHelper.EvaluatePort(ex, _port); } } public async ValueTask ExecuteAsync(ChangeTenantStatusRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); if (!command.IsValid(_changeTenantStatusValidator)) { _port.ValidationErrors(command.Notifications); return; } var result = await _thalosDALService.ChangeStatusTenantAsync(command._Id, command.Status, cancellationToken).ConfigureAwait(false); if (result == null) { _port.NoContentSuccess(); return; } _port.Success(result); } catch (Exception ex) { ApiResponseHelper.EvaluatePort(ex, _port); } } public async ValueTask ExecuteAsync(CreateTenantRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); if (!command.IsValid(_registerTenantValidator)) { _port.ValidationErrors(command.Notifications); return; } var request = new TenantRequest { Name = command.Name, AddressLine1 = command.AddressLine1, AddressLine2 = command.AddressLine2, TaxIdentifier = command.TaxIdentifier, City = command.City, State = command.State, Country = command.Country, PostalCode = command.PostalCode, ContactEmail = command.ContactEmail, ContactPhone = command.ContactPhone, Website = command.Website, ConnectionString = command.ConnectionString, Isolated = command.Isolated }; var result = await _thalosDALService.CreateTenantAsync(request, cancellationToken).ConfigureAwait(false); if (result == null) { _port.NoContentSuccess(); return; } _port.Success(result); } catch (Exception ex) { ApiResponseHelper.EvaluatePort(ex, _port); } } public async ValueTask ExecuteAsync(UpdateTenantRequest command, CancellationToken cancellationToken = default) { try { ArgumentNullException.ThrowIfNull(command); if (!command.IsValid(_updateTenantValidator)) { _port.ValidationErrors(command.Notifications); return; } var request = new TenantAdapter { _Id = command._Id, Name = command.Name, AddressLine1 = command.AddressLine1, AddressLine2 = command.AddressLine2, TaxIdentifier = command.TaxIdentifier, City = command.City, State = command.State, Country = command.Country, PostalCode = command.PostalCode, ContactEmail = command.ContactEmail, ContactPhone = command.ContactPhone, Website = command.Website, ConnectionString = command.ConnectionString, Isolated = command.Isolated }; string id = command._Id; var result = await _thalosDALService.UpdateTenantAsync(request, id, cancellationToken).ConfigureAwait(false); if (result == null) { _port.NoContentSuccess(); return; } _port.Success(result); } catch (Exception ex) { ApiResponseHelper.EvaluatePort(ex, _port); } } } }