220 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			220 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Asp.Versioning;
 | |
| using Core.Thalos.Adapters;
 | |
| using Core.Thalos.Adapters.Attributes;
 | |
| using Core.Thalos.Adapters.Common.Constants;
 | |
| using Core.Thalos.External.Clients.Thalos.Requests.Permissions;
 | |
| 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 module authentication.
 | |
|     /// </summary>
 | |
|     [ApiVersion("1.0")]
 | |
|     [Route("api/v{version:apiVersion}/[controller]")]
 | |
|     [Consumes("application/json")]
 | |
|     [Produces("application/json")]
 | |
|     [ApiController]
 | |
|     //[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|     public class ModuleController(IThalosServiceClient thalosServiceClient, ILogger<ModuleController> logger) : BaseController(logger)
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Gets all the modules.
 | |
|         /// </summary>
 | |
|         [HttpGet("GetAll")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         //[Permission("ModuleManagement.Read, RoleManagement.Read")]
 | |
|         public async Task<IActionResult> GetAllModulesService(CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetAllModulesService)} - Request received - Payload: ");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.GetAllModulesService(new GetAllModulesRequest { }, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetAllModulesService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets all the modules by module identifiers.
 | |
|         /// </summary>
 | |
|         /// <param name="request">The request containing the list of module identifiers.</param>
 | |
|         /// <param name="cancellationToken">Cancellation token for the asynchronous operation.</param>
 | |
|         /// <returns>The <see cref="IActionResult"/> representing the result of the service call.</returns>
 | |
|         /// <response code="200">The modules found.</response>
 | |
|         /// <response code="204">No content if no modules are found.</response>
 | |
|         /// <response code="400">Bad request if the module identifiers are missing or invalid.</response>
 | |
|         /// <response code="401">Unauthorized if the user is not authenticated.</response>
 | |
|         /// <response code="500">Internal server error if an unexpected error occurs.</response>
 | |
|         [HttpPost("GetAllByList")]
 | |
|         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         //[Permission("ModuleManagement.Read")]
 | |
|         public async Task<IActionResult> GetAllModulesByListAsync([FromBody] GetAllModulesByListRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetAllModulesByListAsync)} - Request received - Payload: {request}");
 | |
| 
 | |
|                 if (request == null || request.Modules == null || !request.Modules.Any())
 | |
|                 {
 | |
|                     return BadRequest("Module identifiers are required.");
 | |
|                 }
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.GetAllModulesByListService(request, cancellationToken)).ConfigureAwait(false);
 | |
| 
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError(ex, $"{nameof(GetAllModulesByListAsync)} - An error occurred - {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload: {request}");
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Internal server error");
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Creates a new module.
 | |
|         /// </summary>
 | |
|         [HttpPost("Create")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         //[Permission("ModuleManagement.Write")]
 | |
|         public async Task<IActionResult> CreateModuleService(CreateModuleRequest newModule, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(CreateModuleService)} - Request received - Payload: {JsonSerializer.Serialize(newModule)}");
 | |
| 
 | |
|                 if (newModule == null) return BadRequest("Invalid module object");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newModule.Name)) return BadRequest("Invalid module name");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newModule.Description)) return BadRequest("Invalid module description");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newModule.Route)) return BadRequest("Invalid module route");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.CreateModuleService(newModule, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(CreateModuleService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newModule)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets the module by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPost("GetById")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         //[Permission("ModuleManagement.Read")]
 | |
|         public async Task<IActionResult> GetModuleByIdService(GetModuleRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetModuleByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Id)) return BadRequest("Invalid module identifier");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.GetModuleByIdService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetModuleByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Updates a full module by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPut("Update")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         //[Permission("ModuleManagement.Write")]
 | |
|         public async Task<IActionResult> UpdateModuleService(UpdateModuleRequest newModule, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(UpdateModuleService)} - Request received - Payload: {JsonSerializer.Serialize(newModule)}");
 | |
| 
 | |
|                 if (newModule == null) return BadRequest("Invalid module object");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newModule.Name)) return BadRequest("Invalid module name");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newModule.Description)) return BadRequest("Invalid module description");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newModule.Route)) return BadRequest("Invalid module route");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.UpdateModuleService(newModule, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(UpdateModuleService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newModule)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Changes the status of the module.
 | |
|         /// </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("ModuleManagement.Write")]
 | |
|         public async Task<IActionResult> ChangeModuleStatusService([FromBody] ChangeModuleStatusRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(ChangeModuleStatusService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid module identifier"); }
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.ChangeModuleStatusService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(ChangeModuleStatusService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|     }
 | |
| }
 | 
