221 lines
		
	
	
		
			9.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			221 lines
		
	
	
		
			9.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| // ***********************************************************************
 | |
| // <copyright file="ModuleController.cs">
 | |
| //     AgileWebs
 | |
| // </copyright>
 | |
| // ***********************************************************************
 | |
| 
 | |
| 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
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Handles all requests for module authentication.
 | |
|     /// </summary>
 | |
|     [ApiVersion(MimeTypes.ApplicationVersion)]
 | |
|     [Route("api/v{api-version:apiVersion}/[controller]")]
 | |
|     [Produces(MimeTypes.ApplicationJson)]
 | |
|     [Consumes(MimeTypes.ApplicationJson)]
 | |
|     [ApiController]
 | |
|     public class ModuleController(IModuleService service, ILogger<ModuleController> logger) : ControllerBase
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Gets all the modules.
 | |
|         /// </summary>
 | |
|         /// <returns>The <see cref="IEnumerable{ModuleAdapter}"/> 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]
 | |
|         [Consumes(MimeTypes.ApplicationJson)]
 | |
|         [Produces(MimeTypes.ApplicationJson)]
 | |
|         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
 | |
|         [Permission("ModuleManagement.Read, RoleManagement.Read")]
 | |
|         public async Task<IActionResult> 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}");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets all the modules by module identifiers.
 | |
|         /// </summary>
 | |
|         /// <param name="modules">The list of module identifiers.</param>
 | |
|         /// <returns>The <see cref="IEnumerable{ModuleAdapter}"/> found entities.</returns>
 | |
|         /// <response code="200">The modules found.</response>
 | |
|         /// <response code="404">The modules not found error.</response>
 | |
|         /// <response code="500">The service internal error.</response>
 | |
|         [HttpPost]
 | |
|         [Route(Routes.GetModuleList)]
 | |
|         [Consumes(MimeTypes.ApplicationJson)]
 | |
|         [Produces(MimeTypes.ApplicationJson)]
 | |
|         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
 | |
|         [Permission("ModuleManagement.Read")]
 | |
|         public async Task<IActionResult> 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}");
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets the module by identifier.
 | |
|         /// </summary>
 | |
|         /// <param name="id">The module identifier.</param>
 | |
|         /// <returns>The <see cref="ModuleAdapter"/> found entity.</returns>
 | |
|         /// <response code="200">The module found.</response>
 | |
|         /// <response code="404">The module not found error.</response>
 | |
|         /// <response code="500">The service internal error.</response>
 | |
|         [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<IActionResult> 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}");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Creates a new module.
 | |
|         /// </summary>
 | |
|         /// <param name="newModule">The module to be added.</param>
 | |
|         /// <returns>The <see cref="ModuleAdapter"/> created entity.</returns>
 | |
|         /// <response code="201">The module created.</response>
 | |
|         /// <response code="422">The module could not be created.</response>
 | |
|         /// <response code="500">The service internal e|ror.</response>
 | |
|         [HttpPost]
 | |
|         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status201Created)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
 | |
|         [Permission("ModuleManagement.Write")]
 | |
|         public async Task<IActionResult> 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}");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Updates a full module by identifier.
 | |
|         /// </summary>
 | |
|         /// <param name="entity">The module to update.</param>
 | |
|         /// <param name="id">The module identifier.</param>
 | |
|         /// <returns>The <see cref="ModuleAdapter"/> updated entity.</returns>
 | |
|         /// <response code="200">The module updated.</response>
 | |
|         /// <response code="404">The module not found.</response>
 | |
|         /// <response code="422">The module could not be updated.</response>
 | |
|         /// <response code="500">The service internal error.</response>
 | |
|         [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<IActionResult> 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}");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Changes the status of the module.
 | |
|         /// </summary>
 | |
|         /// <param name="id">The module identifier.</param>
 | |
|         /// <param name="newStatus">The new status of the module.</param>
 | |
|         /// <returns>The <see cref="ModuleAdapter"/> updated entity.</returns>
 | |
|         /// <response code="200">The module updates.</response>
 | |
|         /// <response code="404">The module not found.</response>
 | |
|         /// <response code="422">The module could not be deleted.</response>
 | |
|         /// <response code="500">The service internal error.</response>
 | |
|         [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<IActionResult> 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}");
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | 
