Files
Core.Thalos.DAL.API/Core.Thalos.DAL.API/Controllers/RoleController.cs

196 lines
9.4 KiB
C#

// ***********************************************************************
// <copyright file="RoleController.cs">
// AgileWebs
// </copyright>
// ***********************************************************************
using Asp.Versioning;
using Core.Thalos.BuildingBlocks;
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]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
public class RoleController(IRoleProvider service) : ControllerBase
{
/// <summary>
/// Gets all roles.
/// </summary>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The <see cref="IEnumerable{RoleAdapter}"/> found entities.</returns>
/// <response code="200">The roles found.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<RoleAdapter>), StatusCodes.Status200OK)]
[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 mongo identifier.
/// </summary>
/// <param name="_id">The role mongo identifier.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The <see cref="RoleAdapter"/> found entity.</returns>
/// <response code="200">The role found.</response>
/// <response code="404">The role not found.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[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("Role not found");
}
return Ok(result);
}
/// <summary>
/// Creates a new role.
/// </summary>
/// <param name="newRole">The role to be added.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</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)]
[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 mongo identifier.
/// </summary>
/// <param name="_id">The role mongo identifier.</param>
/// <param name="entity">The role to update.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The <see cref="RoleAdapter"/> updated entity.</returns>
/// <response code="200">The role updated.</response>
/// <response code="400">Bad request if role ID mismatches.</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)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> UpdateRoleAsync([FromRoute] string _id, [FromBody] RoleAdapter entity, CancellationToken cancellationToken)
{
if (_id != entity._Id)
{
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 mongo identifier.</param>
/// <param name="newStatus">The new status of the role.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The <see cref="RoleAdapter"/> updated entity.</returns>
/// <response code="200">The role status updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route(Routes.ChangeStatus)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[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);
if (result == null)
return NotFound("Role not found");
return Ok(result);
}
/// <summary>
/// Adds an application to the role's list of applications.
/// </summary>
/// <param name="roleId">The mongo identifier of the role to which the application will be added.</param>
/// <param name="application">The application enum value to add.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The updated <see cref="RoleAdapter"/> object.</returns>
/// <response code="200">The application was added to the role.</response>
/// <response code="500">The service internal error.</response>
[HttpPost(Routes.AddApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[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 mongo identifier of the role from which the application will be removed.</param>
/// <param name="application">The application enum value to remove.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The updated <see cref="RoleAdapter"/> object.</returns>
/// <response code="200">The application was removed from the role.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete(Routes.RemoveApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[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);
}
/// <summary>
/// Deletes a role by its mongo identifier.
/// </summary>
/// <param name="_id">The role mongo identifier.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>The deleted <see cref="RoleAdapter"/> entity.</returns>
/// <response code="200">The role was deleted successfully.</response>
/// <response code="404">The role was not found.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> DeleteRoleAsync([FromRoute] string _id, CancellationToken cancellationToken)
{
var result = await service.DeleteRole(_id, cancellationToken).ConfigureAwait(false);
if (result is null)
return NotFound("Role not found.");
return Ok(result);
}
}
}