Use Blueprint.Mongo package in Role and Permission services
This commit is contained in:
		| @@ -5,14 +5,15 @@ | ||||
| // *********************************************************************** | ||||
|  | ||||
| using Asp.Versioning; | ||||
| using Core.Blueprint.Mongo; | ||||
| 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 Microsoft.Graph; | ||||
| using PermissionRequest = Core.Thalos.Domain.Contexts.Onboarding.Request.PermissionRequest; | ||||
|  | ||||
| namespace LSA.Core.Kerberos.API.Controllers | ||||
| { | ||||
| @@ -24,7 +25,7 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|     [Produces(MimeTypes.ApplicationJson)] | ||||
|     [Consumes(MimeTypes.ApplicationJson)] | ||||
|     [ApiController] | ||||
|     public class PermissionController(IPermissionService service, ILogger<PermissionController> logger) : ControllerBase | ||||
|     public class PermissionController(IPermissionProvider service) : ControllerBase | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets all the permissions. | ||||
| @@ -39,19 +40,10 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|         [ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)] | ||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||
|         [Permission("PermissionManagement.Read, RoleManagement.Read")] | ||||
|         public async Task<IActionResult> GetAllPermissionsAsync() | ||||
|         public async Task<IActionResult> GetAllPermissionsAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var result = await service.GetAllPermissionsService(); | ||||
|  | ||||
|                 return Ok(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, "Error in GetAllPermissionsAsync"); | ||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); | ||||
|             } | ||||
|             var result = await service.GetAllPermissions(cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -69,29 +61,15 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|         [ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)] | ||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||
|         [Permission("PermissionManagement.Read")] | ||||
|         public async Task<IActionResult> GetAllPermissionsByList([FromBody] string[] permissions) | ||||
|         public async Task<IActionResult> GetAllPermissionsByList([FromBody] string[] permissions, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (permissions == null || !permissions.Any()) | ||||
|             { | ||||
|                 return BadRequest("Permission identifiers are required."); | ||||
|                 return BadRequest("Module identifiers are required."); | ||||
|             } | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 var result = await service.GetAllPermissionsByListService(permissions); | ||||
|  | ||||
|                 if (result == null || !result.Any()) | ||||
|                 { | ||||
|                     return NotFound("No permissions found for the given identifiers."); | ||||
|                 } | ||||
|  | ||||
|                 return Ok(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, "Error in GetAllPermissionsByList"); | ||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); | ||||
|             } | ||||
|             var result = await service.GetAllPermissionsByList(permissions, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -109,21 +87,16 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] | ||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||
|         [Permission("PermissionManagement.Read")] | ||||
|         public async Task<IActionResult> GetPermissionByIdAsync([FromRoute] string id) | ||||
|         public async Task<IActionResult> GetPermissionByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var result = await service.GetPermissionByIdService(id); | ||||
|             var result = await service.GetPermissionById(_id, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result is null) return NotFound($"permission with id: '{id}' not found"); | ||||
|  | ||||
|                 return Ok(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             if (result == null) | ||||
|             { | ||||
|                 logger.LogError(ex, "Error in GetPermissionByIdAsync"); | ||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); | ||||
|                 return NotFound("Entity not found"); | ||||
|             } | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -138,18 +111,10 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status201Created)] | ||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||
|         [Permission("PermissionManagement.Write")] | ||||
|         public async Task<IActionResult> CreatePermissionAsync([FromBody] PermissionRequest newPermission) | ||||
|         public async Task<IActionResult> CreatePermissionAsync([FromBody] PermissionRequest newPermission, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var result = await service.CreatePermissionService(newPermission).ConfigureAwait(false); | ||||
|                 return Created("CreatedWithIdService", result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, "Error in CreatePermissionAsync"); | ||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); | ||||
|             } | ||||
|             var result = await service.CreatePermission(newPermission, cancellationToken).ConfigureAwait(false); | ||||
|             return Created("CreatedWithIdAsync", result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -169,19 +134,16 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] | ||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||
|         [Permission("PermissionManagement.Write")] | ||||
|         public async Task<IActionResult> UpdatePermissionAsync(PermissionAdapter entity, string id) | ||||
|         public async Task<IActionResult> UpdatePermissionAsync([FromRoute] string _id, PermissionAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             if (_id != entity._Id?.ToString()) | ||||
|             { | ||||
|                 var result = await service.UpdatePermissionService(entity, id); | ||||
|                 return BadRequest("Permission ID mismatch"); | ||||
|             } | ||||
|  | ||||
|                 return Ok(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, "Error in UpdatePermissionAsync"); | ||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); | ||||
|             } | ||||
|             var result = await service.UpdatePermission(entity, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -201,19 +163,10 @@ namespace LSA.Core.Kerberos.API.Controllers | ||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] | ||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||
|         [Permission("PermissionManagement.Write")] | ||||
|         public async Task<IActionResult> ChangePermissionStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus) | ||||
|         public async Task<IActionResult> ChangePermissionStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var result = await service.ChangePermissionStatusService(id, newStatus); | ||||
|  | ||||
|                 return Ok(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, "Error in ChangePermissionStatus"); | ||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); | ||||
|             } | ||||
|             var result = await service.ChangePermissionStatus(id, newStatus, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales