Files
Core.Thalos.DAL.API/Core.Thalos.DAL.API/Controllers/RoleController.cs
2025-06-10 23:12:43 -06:00

178 lines
8.6 KiB
C#

// ***********************************************************************
// <copyright file="RoleController.cs">
// AgileWebs
// </copyright>
// ***********************************************************************
using Asp.Versioning;
using Core.Thalos.Adapters;
using Core.Thalos.Adapters.Attributes;
using Core.Thalos.Adapters.Common.Constants;
using Core.Thalos.Adapters.Common.Enums;
using Core.Thalos.Domain.Contexts.Onboarding.Request;
using Core.Thalos.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StatusEnum = Core.Blueprint.Mongo.StatusEnum;
namespace LSA.Core.Thalos.API.Controllers
{
/// <summary>
/// Handles all requests for role authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class RoleController(IRoleProvider service) : ControllerBase
{
/// <summary>
/// Gets all the roles.
/// </summary>
/// <returns>The rol found entities.</returns>
/// <response code="200">The roles found.</response>
/// <response code="404">The roles not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<RoleAdapter>), StatusCodes.Status200OK)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Read")]
public async Task<IActionResult> GetAllRolesAsync(CancellationToken cancellationToken)
{
var result = await service.GetAllRoles(cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets the role by identifier.
/// </summary>
/// <param name="id">The role identifier.</param>
/// <returns>The <see cref="RoleAdapter"/> found entity.</returns>
/// <response code="200">The role found.</response>
/// <response code="404">The role not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Read")]
public async Task<IActionResult> GetRoleByIdAsync([FromRoute] string id, CancellationToken cancellationToken)
{
var result = await service.GetRoleById(id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return NotFound("Entity not found");
}
return Ok(result);
}
/// <summary>
/// Creates a new role.
/// </summary>
/// <param name="newRole">The role to be added.</param>
/// <returns>The <see cref="RoleAdapter"/> created entity.</returns>
/// <response code="201">The role created.</response>
/// <response code="422">The role could not be created.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status201Created)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Write")]
public async Task<IActionResult> CreateRoleAsync([FromBody] RoleRequest newRole, CancellationToken cancellationToken)
{
var result = await service.CreateRole(newRole, cancellationToken).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
/// <summary>
/// Updates a full role by identifier.
/// </summary>
/// <param name="entity">The role to update.</param>
/// <param name="id">The role identifier.</param>
/// <returns>The <see cref="RoleAdapter"/> updated entity.</returns>
/// <response code="200">The role updated.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Write")]
public async Task<IActionResult> UpdateRoleAsync([FromRoute] string id, [FromBody] RoleAdapter entity, CancellationToken cancellationToken)
{
if (id != entity.Id?.ToString())
{
return BadRequest("Role ID mismatch");
}
var result = await service.UpdateRole(entity, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Changes the status of the role.
/// </summary>
/// <param name="id">The role identifier.</param>
/// <param name="newStatus">The new status of the role.</param>
/// <returns>The <see cref="RoleAdapter"/> updated entity.</returns>
/// <response code="200">The role updates.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route(Routes.ChangeStatus)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Write")]
public async Task<IActionResult> ChangeRoleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{
var result = await service.ChangeRoleStatus(id, newStatus, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Adds an application to the role's list of applications.
/// </summary>
/// <param name="roleId">The identifier of the role to which the application will be added.</param>
/// <param name="application">The application enum value to add.</param>
/// <returns>A <see cref="Task{RoleAdapter}"/> representing the asynchronous operation, with the updated role object.</returns>
/// <response code="200">The role updates.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPost(Routes.AddApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Write")]
public async Task<IActionResult> AddApplicationToRoleAsync([FromRoute] string roleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken)
{
var result = await service.AddApplicationToRole(roleId, application, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Removes an application from the role's list of applications.
/// </summary>
/// <param name="roleId">The identifier of the role from which the application will be removed.</param>
/// <param name="application">The application enum value to remove.</param>
/// <returns>A <see cref="Task{RoleAdapter}"/> representing the asynchronous operation, with the updated role object.</returns>
/// <response code="200">The role updates.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete(Routes.RemoveApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
//[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
//[Permission("RoleManagement.Write")]
public async Task<IActionResult> RemoveApplicationFromRoleAsync([FromRoute] string roleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken)
{
var result = await service.RemoveApplicationFromRole(roleId, application, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
}
}