// *********************************************************************** // // AgileWebs // // *********************************************************************** using Asp.Versioning; using Core.Cerberos.Adapters; using Core.Cerberos.Adapters.Attributes; using Core.Cerberos.Adapters.Common.Constants; using Core.Cerberos.Adapters.Common.Enums; using Core.Cerberos.Domain.Contexts.Onboarding.Request; using Core.Cerberos.Provider.Contracts; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace LSA.Core.Kerberos.API.Controllers { /// /// Handles all requests for module authentication. /// [ApiVersion(MimeTypes.ApplicationVersion)] [Route("api/v{api-version:apiVersion}/[controller]")] [Produces(MimeTypes.ApplicationJson)] [Consumes(MimeTypes.ApplicationJson)] [ApiController] public class ModuleController(IModuleService service, ILogger logger) : ControllerBase { /// /// Gets all the modules. /// /// The found entities. /// The roles found. /// The roles not found error. /// The service internal error. [HttpGet] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.HeathScheme)] [Permission("ModuleManagement.Read, RoleManagement.Read")] public async Task GetAllModulesAsync() { try { var result = await service.GetAllModulesService(); return Ok(result); } catch (Exception ex) { logger.LogError(ex, "Error in GetAllModulesAsync"); return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); } } /// /// Gets all the modules by module identifiers. /// /// The list of module identifiers. /// The found entities. /// The modules found. /// The modules not found error. /// The service internal error. [HttpPost] [Route(Routes.GetModuleList)] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.HeathScheme)] [Permission("ModuleManagement.Read")] public async Task GetAllModulesByList([FromBody] string[] modules) { if (modules == null || !modules.Any()) { return BadRequest("Module identifiers are required."); } try { var result = await service.GetAllModulesByListService(modules); if (result == null || !result.Any()) { return NotFound("No modules found for the given identifiers."); } return Ok(result); } catch (Exception ex) { logger.LogError(ex, "Error in GetAllModulesByList"); return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); } } /// /// Gets the module by identifier. /// /// The module identifier. /// The found entity. /// The module found. /// The module not found error. /// The service internal error. [HttpGet] [Route(Routes.Id)] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.HeathScheme)] [Permission("ModuleManagement.Read")] public async Task GetModuleByIdAsync([FromRoute] string id) { try { var result = await service.GetModuleByIdService(id); if (result is null) return NotFound($"module with id: '{id}' not found"); return Ok(result); } catch (Exception ex) { logger.LogError(ex, "Error in GetModuleByIdAsync"); return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); } } /// /// Creates a new module. /// /// The module to be added. /// The created entity. /// The module created. /// The module could not be created. /// The service internal e|ror. [HttpPost] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status201Created)] [Authorize(AuthenticationSchemes = Schemes.HeathScheme)] [Permission("ModuleManagement.Write")] public async Task CreateModuleAsync([FromBody] ModuleRequest newModule) { try { var result = await service.CreateModuleService(newModule).ConfigureAwait(false); return Created("CreatedWithIdService", result); } catch (Exception ex) { logger.LogError(ex, "Error in CreateModuleAsync"); return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); } } /// /// Updates a full module by identifier. /// /// The module to update. /// The module identifier. /// The updated entity. /// The module updated. /// The module not found. /// The module could not be updated. /// The service internal error. [HttpPut] [Route(Routes.Id)] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.HeathScheme)] [Permission("ModuleManagement.Write")] public async Task UpdateModuleAsync(ModuleAdapter entity, string id) { try { var result = await service.UpdateModuleService(entity, id); return Ok(result); } catch (Exception ex) { logger.LogError(ex, "Error in UpdateModuleAsync"); return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); } } /// /// Changes the status of the module. /// /// The module identifier. /// The new status of the module. /// The updated entity. /// The module updates. /// The module not found. /// The module could not be deleted. /// The service internal error. [HttpPatch] [Route(Routes.ChangeStatus)] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.HeathScheme)] [Permission("ModuleManagement.Write")] public async Task ChangeModuleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus) { try { var result = await service.ChangeModuleStatusService(id, newStatus); return Ok(result); } catch (Exception ex) { logger.LogError(ex, "Error in ChangeModuleStatus"); return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); } } } }