224 lines
8.1 KiB
C#
224 lines
8.1 KiB
C#
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<ChangeTenantStatusRequest>,
|
|
IComponentHandler<GetAllTenantsRequest>,
|
|
IComponentHandler<UpdateTenantRequest>,
|
|
IComponentHandler<GetTenantRequest>,
|
|
IComponentHandler<DeleteTenantRequest>,
|
|
IComponentHandler<CreateTenantRequest>
|
|
{
|
|
private readonly ITenantPort _port;
|
|
private readonly IValidator<ChangeTenantStatusRequest> _changeTenantStatusValidator;
|
|
private readonly IValidator<CreateTenantRequest> _registerTenantValidator;
|
|
private readonly IValidator<UpdateTenantRequest> _updateTenantValidator;
|
|
private readonly IThalosServiceClient _thalosDALService;
|
|
|
|
public TenantHandler(
|
|
ITenantPort port,
|
|
IValidator<ChangeTenantStatusRequest> changeTenantStatusValidator,
|
|
IValidator<CreateTenantRequest> registerTenantValidator,
|
|
IValidator<UpdateTenantRequest> 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);
|
|
}
|
|
}
|
|
}
|
|
}
|