Files
Core.Thalos.BFF/Core.Thalos.BFF.Api/Controllers/TenantController.cs

197 lines
9.6 KiB
C#

using Asp.Versioning;
using Core.Thalos.BuildingBlocks;
using Core.Thalos.External.Clients.Thalos.Requests.Tenants;
using Lib.Architecture.BuildingBlocks;
using LSA.Dashboard.External.Clients.Dashboard;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace Core.Thalos.BFF.Api.Controllers
{
/// <summary>
/// Handles all requests for Tenant authentication.
/// </summary>
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Consumes("application/json")]
[Produces("application/json")]
[ApiController]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
public class TenantController(IThalosServiceClient thalosServiceClient, ILogger<TenantController> logger) : BaseController(logger)
{
/// <summary>
/// Gets all Tenants.
/// </summary>
[HttpGet("GetAll")]
[ProducesResponseType(typeof(IEnumerable<TenantAdapter>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Permission("TenantManagement.Read, RoleManagement.Read")]
public async Task<IActionResult> GetAllTenantsService(CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetAllTenantsService)} - Request received - Payload: ");
return await Handle(() => thalosServiceClient.GetAllTenantsService(new GetAllTenantsRequest { }, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(GetAllTenantsService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
throw;
}
}
/// <summary>
/// Creates a new Tenant.
/// </summary>
[HttpPost("Create")]
[ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> CreateTenantService(CreateTenantRequest newTenant, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(CreateTenantService)} - Request received - Payload: {JsonSerializer.Serialize(newTenant)}");
if (newTenant == null) return BadRequest("Invalid Tenant object");
if (string.IsNullOrEmpty(newTenant.Name)) return BadRequest("Invalid Tenant name");
return await Handle(() => thalosServiceClient.CreateTenantService(newTenant, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(CreateTenantService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newTenant)}");
throw;
}
}
/// <summary>
/// Gets the Tenant by identifier.
/// </summary>
[HttpPost("GetById")]
[ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Permission("TenantManagement.Read")]
public async Task<IActionResult> GetTenantByIdService(GetTenantRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetTenantByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request._Id)) return BadRequest("Invalid Tenant identifier");
return await Handle(() => thalosServiceClient.GetTenantByIdService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(GetTenantByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
/// <summary>
/// Updates a full Tenant by identifier.
/// </summary>
[HttpPut("Update")]
[ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> UpdateTenantService(UpdateTenantRequest newTenant, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(UpdateTenantService)} - Request received - Payload: {JsonSerializer.Serialize(newTenant)}");
if (newTenant == null) return BadRequest("Invalid Tenant object");
if (string.IsNullOrEmpty(newTenant.Name)) return BadRequest("Invalid Tenant name");
return await Handle(() => thalosServiceClient.UpdateTenantService(newTenant, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(UpdateTenantService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newTenant)}");
throw;
}
}
/// <summary>
/// Changes the status of the Tenant.
/// </summary>
[HttpPatch]
[Route("ChangeStatus")]
[ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> ChangeTenantStatusService([FromBody] ChangeTenantStatusRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(ChangeTenantStatusService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request._Id)) { return BadRequest("Invalid Tenant identifier"); }
return await Handle(() => thalosServiceClient.ChangeTenantStatusService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(ChangeTenantStatusService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
/// <summary>
/// Deletes the Tenant by identifier.
/// </summary>
[HttpDelete("Delete")]
[ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> DeleteTenantByIdService(DeleteTenantRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(DeleteTenantByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request._Id)) return BadRequest("Invalid Tenant identifier");
return await Handle(() => thalosServiceClient.DeleteTenantByIdService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(DeleteTenantByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
}
}