Core.Thalos.Service.API/Core.Thalos.Service.API/Controllers/TenantController.cs
2025-08-29 16:02:45 -06:00

177 lines
8.4 KiB
C#

using Asp.Versioning;
using Core.Thalos.Application.UseCases.Tenants.Input;
using Core.Thalos.Application.UseCases.Tenants.Ports;
using Core.Thalos.BuildingBlocks;
using Lib.Architecture.BuildingBlocks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Core.Thalos.Service.API.Controllers
{
/// <summary>
/// Handles all services and business rules related to <see cref="TenantController"/>.
/// </summary>
[ApiVersion("1.0")]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces("application/json")]
[ApiController]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
public class TenantController : ControllerBase
{
private readonly IComponentHandler<GetTenantRequest> getTenantHandler;
private readonly IComponentHandler<GetAllTenantsRequest> getAllTenantsHandler;
private readonly IComponentHandler<CreateTenantRequest> createTenantHandler;
private readonly IComponentHandler<UpdateTenantRequest> updateTenantHandler;
private readonly IComponentHandler<DeleteTenantRequest> deleteTenantHandler;
private readonly IComponentHandler<ChangeTenantStatusRequest> changeTenantStatusHandler;
private readonly ITenantPort port;
/// <summary>
/// Handles all services and business rules related to <see cref="TenantController"/>.
/// </summary>
public TenantController(
IComponentHandler<GetTenantRequest> getTenantHandler,
IComponentHandler<GetAllTenantsRequest> getAllTenantsHandler,
IComponentHandler<CreateTenantRequest> createTenantHandler,
IComponentHandler<UpdateTenantRequest> updateTenantHandler,
IComponentHandler<DeleteTenantRequest> deleteTenantHandler,
IComponentHandler<ChangeTenantStatusRequest> changeTenantStatusHandler,
ITenantPort port
)
{
this.createTenantHandler = createTenantHandler;
this.updateTenantHandler = updateTenantHandler;
this.deleteTenantHandler = deleteTenantHandler;
this.changeTenantStatusHandler = changeTenantStatusHandler;
this.getAllTenantsHandler = getAllTenantsHandler;
this.getTenantHandler = getTenantHandler;
this.port = port;
}
/// <summary>
/// Gets all the Tenants.
/// </summary>
[HttpGet("GetAll")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Permission("TenantManagement.Read, RoleManagement.Read")]
public async Task<IActionResult> GetAllTenantsAsync(CancellationToken cancellationToken)
{
await getAllTenantsHandler.ExecuteAsync(new GetAllTenantsRequest { }, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Gets the Tenant by mongo identifier.
/// </summary>
[HttpPost]
[Route("GetById")]
[ProducesResponseType(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.Read")]
public async Task<IActionResult> GetTenantById([FromBody] GetTenantRequest request, CancellationToken cancellationToken)
{
if (request._Id == null || !request._Id.Any())
{
return BadRequest("Invalid Tenant _Id");
}
await getTenantHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Creates a new Tenant.
/// </summary>
[HttpPost("Create")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> CreateTenantAsync([FromBody] CreateTenantRequest newTenant, CancellationToken cancellationToken = default)
{
await createTenantHandler.ExecuteAsync(newTenant, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Updates a full Tenant by mongo identifier.
/// </summary>
[HttpPut("Update")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> UpdateTenantAsync([FromBody] UpdateTenantRequest request, CancellationToken cancellationToken = default)
{
await updateTenantHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Deletes a full Tenant by mongo identifier.
/// </summary>
[HttpDelete("Delete")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Permission("TenantManagement.Write")]
public async Task<IActionResult> DeleteTenantAsync([FromBody] DeleteTenantRequest request, CancellationToken cancellationToken = default)
{
await deleteTenantHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Changes the status of the Tenant.
/// </summary>
[HttpPatch]
[Route("ChangeStatus")]
[ProducesResponseType(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> ChangeTenantStatusAsync([FromBody] ChangeTenantStatusRequest request,
CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(request._Id)) { return BadRequest("Invalid Tenant mongo identifier"); }
await changeTenantStatusHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
}
}