Merge pull request #3 from SergioMatias94/feature/blueprint-mongo-in-module-service
Use Blueprint.Mongo package in module service
This commit is contained in:
		| @@ -5,14 +5,15 @@ | |||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Asp.Versioning; | using Asp.Versioning; | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters; | using Core.Thalos.Adapters; | ||||||
| using Core.Thalos.Adapters.Attributes; | using Core.Thalos.Adapters.Attributes; | ||||||
| using Core.Thalos.Adapters.Common.Constants; | 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 Core.Thalos.Provider.Contracts; | ||||||
| using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
|  | using Microsoft.Graph; | ||||||
|  | using ModuleRequest = Core.Thalos.Domain.Contexts.Onboarding.Request.ModuleRequest; | ||||||
|  |  | ||||||
| namespace LSA.Core.Kerberos.API.Controllers | namespace LSA.Core.Kerberos.API.Controllers | ||||||
| { | { | ||||||
| @@ -24,7 +25,7 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|     [Produces(MimeTypes.ApplicationJson)] |     [Produces(MimeTypes.ApplicationJson)] | ||||||
|     [Consumes(MimeTypes.ApplicationJson)] |     [Consumes(MimeTypes.ApplicationJson)] | ||||||
|     [ApiController] |     [ApiController] | ||||||
|     public class ModuleController(IModuleService service, ILogger<ModuleController> logger) : ControllerBase |     public class ModuleController(IModuleProvider service) : ControllerBase | ||||||
|     { |     { | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the modules. |         /// Gets all the modules. | ||||||
| @@ -39,20 +40,11 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("ModuleManagement.Read, RoleManagement.Read")] |         [Permission("ModuleManagement.Read, RoleManagement.Read")] | ||||||
|         public async Task<IActionResult> GetAllModulesAsync() |         public async Task<IActionResult> GetAllModulesAsync(CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.GetAllModules(cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetAllModulesService(); |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetAllModulesAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the modules by module identifiers. |         /// Gets all the modules by module identifiers. | ||||||
| @@ -69,30 +61,16 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("ModuleManagement.Read")] |         [Permission("ModuleManagement.Read")] | ||||||
|         public async Task<IActionResult> GetAllModulesByList([FromBody] string[] modules) |         public async Task<IActionResult> GetAllModulesByList([FromBody] string[] modules, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             if (modules == null || !modules.Any()) |             if (modules == null || !modules.Any()) | ||||||
|             { |             { | ||||||
|                 return BadRequest("Module identifiers are required."); |                 return BadRequest("Module identifiers are required."); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             try |             var result = await service.GetAllModulesByList(modules, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetAllModulesByListService(modules); |  | ||||||
|  |  | ||||||
|                 if (result == null || !result.Any()) |  | ||||||
|                 { |  | ||||||
|                     return NotFound("No modules found for the given identifiers."); |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetAllModulesByList"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
| @@ -110,22 +88,17 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("ModuleManagement.Read")] |         [Permission("ModuleManagement.Read")] | ||||||
|         public async Task<IActionResult> GetModuleByIdAsync([FromRoute] string id) |         public async Task<IActionResult> GetModuleByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.GetModuleById(_id, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetModuleByIdService(id); |  | ||||||
|  |  | ||||||
|                 if (result is null) return NotFound($"module with id: '{id}' not found"); |             if (result == null) | ||||||
|  |             { | ||||||
|  |                 return NotFound("Entity not found"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetModuleByIdAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Creates a new module. |         /// Creates a new module. | ||||||
| @@ -139,18 +112,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status201Created)] |         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status201Created)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("ModuleManagement.Write")] |         [Permission("ModuleManagement.Write")] | ||||||
|         public async Task<IActionResult> CreateModuleAsync([FromBody] ModuleRequest newModule) |         public async Task<IActionResult> CreateModuleAsync([FromBody] ModuleRequest newModule, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.CreateModule(newModule, cancellationToken).ConfigureAwait(false); | ||||||
|             { |             return Created("CreatedWithIdAsync", result); | ||||||
|                 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> |         /// <summary> | ||||||
| @@ -170,20 +135,17 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("ModuleManagement.Write")] |         [Permission("ModuleManagement.Write")] | ||||||
|         public async Task<IActionResult> UpdateModuleAsync(ModuleAdapter entity, string id) |         public async Task<IActionResult> UpdateModuleAsync([FromRoute] string _id, ModuleAdapter entity, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             if (_id != entity._Id?.ToString()) | ||||||
|             { |             { | ||||||
|                 var result = await service.UpdateModuleService(entity, id); |                 return BadRequest("User ID mismatch"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             var result = await service.UpdateModule(entity, cancellationToken).ConfigureAwait(false); | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in UpdateModuleAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the module. |         /// Changes the status of the module. | ||||||
| @@ -202,19 +164,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("ModuleManagement.Write")] |         [Permission("ModuleManagement.Write")] | ||||||
|         public async Task<IActionResult> ChangeModuleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus) |         public async Task<IActionResult> ChangeModuleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.ChangeModuleStatus(id, newStatus, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.ChangeModuleStatusService(id, newStatus); |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in ChangeModuleStatus"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -3,13 +3,13 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters; | using Core.Thalos.Adapters; | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; | using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.Provider.Contracts | namespace Core.Thalos.Provider.Contracts | ||||||
| { | { | ||||||
|     public interface IModuleService |     public interface IModuleProvider | ||||||
|     { |     { | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Creates a new Module. |         /// Creates a new Module. | ||||||
| @@ -17,7 +17,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="entity">The Module to be created.</param> |         /// <param name="entity">The Module to be created.</param> | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<ModuleAdapter> CreateModuleService(ModuleRequest newModule); |         ValueTask<ModuleAdapter> CreateModule(ModuleRequest newModule, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets an Module by identifier. |         /// Gets an Module by identifier. | ||||||
| @@ -25,14 +25,14 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="id">The Module identifier.</param> |         /// <param name="id">The Module identifier.</param> | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<ModuleAdapter> GetModuleByIdService(string id); |         ValueTask<ModuleAdapter> GetModuleById(string _id, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the roles. |         /// Gets all the roles. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <returns>A <see cref="{Task{IEnumerbale{ModuleAdapter}}}"/> representing |         /// <returns>A <see cref="{Task{IEnumerbale{ModuleAdapter}}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<IEnumerable<ModuleAdapter>> GetAllModulesService(); |         ValueTask<IEnumerable<ModuleAdapter>> GetAllModules(CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the permissions by permissions identifier list. |         /// Gets all the permissions by permissions identifier list. | ||||||
| @@ -40,7 +40,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="permissions">The list of permissions identifiers.</param> |         /// <param name="permissions">The list of permissions identifiers.</param> | ||||||
|         /// <returns>A <see cref="Task{IEnumerable{ModuleAdapter}}"/> representing |         /// <returns>A <see cref="Task{IEnumerable{ModuleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<IEnumerable<ModuleAdapter>> GetAllModulesByListService(string[] permissions); |         ValueTask<IEnumerable<ModuleAdapter>> GetAllModulesByList(string[] modules, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the permission. |         /// Changes the status of the permission. | ||||||
| @@ -50,7 +50,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <returns>The <see cref="ModuleAdapter"/> updated entity.</returns> |         /// <returns>The <see cref="ModuleAdapter"/> updated entity.</returns> | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<ModuleAdapter> ChangeModuleStatusService(string id, StatusEnum newStatus); |         ValueTask<ModuleAdapter> ChangeModuleStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Updates a Module by id. |         /// Updates a Module by id. | ||||||
| @@ -59,6 +59,6 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="id">The Module identifier.</param> |         /// <param name="id">The Module identifier.</param> | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<ModuleAdapter> UpdateModuleService(ModuleAdapter entity, string id); |         ValueTask<ModuleAdapter> UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken); | ||||||
|     } |     } | ||||||
| } | } | ||||||
							
								
								
									
										153
									
								
								Core.Thalos.Provider/Providers/Onboarding/ModuleProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								Core.Thalos.Provider/Providers/Onboarding/ModuleProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,153 @@ | |||||||
|  | // *********************************************************************** | ||||||
|  | // <copyright file="ModuleService.cs"> | ||||||
|  | //     AgileWebs | ||||||
|  | // </copyright> | ||||||
|  | // *********************************************************************** | ||||||
|  | using Core.Thalos.Adapters; | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
|  | using Core.Blueprint.Redis; | ||||||
|  | using Core.Blueprint.Redis.Helpers; | ||||||
|  | using Mapster; | ||||||
|  | using Microsoft.Extensions.Options; | ||||||
|  | using MongoDB.Driver; | ||||||
|  | using Core.Thalos.Provider.Contracts; | ||||||
|  | using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||||
|  |  | ||||||
|  | namespace Core.Thalos.Provider.Providers.Onboarding | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Handles all services and business rules related to <see cref="ModuleAdapter"/>. | ||||||
|  |     /// </summary> | ||||||
|  |     public class ModuleProvider : IModuleProvider | ||||||
|  |     { | ||||||
|  |         private readonly CollectionRepository<ModuleAdapter> repository; | ||||||
|  |         private readonly CacheSettings cacheSettings; | ||||||
|  |         private readonly IRedisCacheProvider cacheProvider; | ||||||
|  |  | ||||||
|  |         public ModuleProvider(CollectionRepository<ModuleAdapter> repository, | ||||||
|  |         IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings) | ||||||
|  |         { | ||||||
|  |             this.repository = repository; | ||||||
|  |             this.repository.CollectionInitialization(); | ||||||
|  |             this.cacheSettings = cacheSettings.Value; | ||||||
|  |             this.cacheProvider = cacheProvider; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Creates a new Module. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="entity">The Module to be created.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<ModuleAdapter> CreateModule(ModuleRequest newModule, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var moduleCollection = newModule.Adapt<ModuleAdapter>(); | ||||||
|  |  | ||||||
|  |             await repository.InsertOneAsync(moduleCollection); | ||||||
|  |  | ||||||
|  |             return moduleCollection; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets an Module by identifier. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="id">The Module identifier.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns>0 | ||||||
|  |         public async ValueTask<ModuleAdapter> GetModuleById(string _id, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleById", _id); | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<ModuleAdapter>(cacheKey); | ||||||
|  |  | ||||||
|  |             if (cachedData is not null) { return cachedData; } | ||||||
|  |  | ||||||
|  |             var module = await repository.FindByIdAsync(_id); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, module); | ||||||
|  |  | ||||||
|  |             return module; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets all the modules. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <returns>A <see cref="{Task{IEnumerbale{ModuleAdapter}}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<IEnumerable<ModuleAdapter>> GetAllModules(CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModules"); | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey) ?? []; | ||||||
|  |  | ||||||
|  |             if (cachedData.Any()) return cachedData; | ||||||
|  |  | ||||||
|  |             var modules = await repository.AsQueryable(); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, modules); | ||||||
|  |  | ||||||
|  |             return modules; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets all the modules by modules identifier list. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="modules">The list of modules identifiers.</param> | ||||||
|  |         /// <returns>A <see cref="Task{IEnumerable{ModuleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<IEnumerable<ModuleAdapter>> GetAllModulesByList(string[] modules, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByList", modules); | ||||||
|  |  | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey) ?? []; | ||||||
|  |  | ||||||
|  |             if (cachedData.Any()) return cachedData; | ||||||
|  |  | ||||||
|  |             var builder = Builders<ModuleAdapter>.Filter; | ||||||
|  |             var filters = new List<FilterDefinition<ModuleAdapter>>(); | ||||||
|  |  | ||||||
|  |             if (modules == null || !modules.Any()) | ||||||
|  |             { | ||||||
|  |                 filters.Add(builder.In(x => x.Id, modules)); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty; | ||||||
|  |  | ||||||
|  |             var modulesList = await repository.FilterByMongoFilterAsync(finalFilter); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, modulesList); | ||||||
|  |  | ||||||
|  |             return modulesList; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         /// <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>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<ModuleAdapter> ChangeModuleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var entity = await repository.FindByIdAsync(id); | ||||||
|  |             entity.Status = newStatus; | ||||||
|  |  | ||||||
|  |             await repository.ReplaceOneAsync(entity); | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Updates a Module by id. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="entity">The Module to be updated.</param> | ||||||
|  |         /// <param name="id">The Module identifier.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<ModuleAdapter> UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             await repository.ReplaceOneAsync(entity); | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,256 +0,0 @@ | |||||||
| // *********************************************************************** |  | ||||||
| // <copyright file="ModuleService.cs"> |  | ||||||
| //     AgileWebs |  | ||||||
| // </copyright> |  | ||||||
| // *********************************************************************** |  | ||||||
| using Core.Thalos.Adapters; |  | ||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Mappers; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; |  | ||||||
| using Core.Thalos.Infraestructure.Caching.Configs; |  | ||||||
| using Core.Thalos.Infraestructure.Caching.Contracts; |  | ||||||
| using Core.Thalos.Provider.Contracts; |  | ||||||
| using LSA.Core.Dapper.Service.Caching; |  | ||||||
| using Microsoft.AspNetCore.Http; |  | ||||||
| using Microsoft.Extensions.Logging; |  | ||||||
| using Microsoft.Extensions.Options; |  | ||||||
| using MongoDB.Bson; |  | ||||||
| using MongoDB.Driver; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.Provider.Providers.Onboarding |  | ||||||
| { |  | ||||||
|     /// <summary> |  | ||||||
|     /// Handles all services and business rules related to <see cref="ModuleAdapter"/>. |  | ||||||
|     /// </summary> |  | ||||||
|     public class ModuleService(ILogger<ModuleService> logger, IHttpContextAccessor httpContextAccessor, ICacheService cacheService, |  | ||||||
|         IOptions<CacheSettings> cacheSettings, IMongoDatabase database) : IModuleService |  | ||||||
|     { |  | ||||||
|         private readonly CacheSettings _cacheSettings = cacheSettings.Value; |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Creates a new Module. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="entity">The Module to be created.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<ModuleAdapter> CreateModuleService(ModuleRequest newModule) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var entity = newModule.ToAdapter(httpContextAccessor); |  | ||||||
|                 entity.Order = (entity.Order is not null) ? entity.Order : await GetLastOrderModule(newModule); |  | ||||||
|                 await database.GetCollection<ModuleAdapter>(CollectionNames.Module).InsertOneAsync(entity); |  | ||||||
|                 entity.Id = (entity as dynamic ?? "").Id.ToString(); |  | ||||||
|  |  | ||||||
|                 return entity; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"CreateModuleService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets an Module by identifier. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="id">The Module identifier.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns>0 |  | ||||||
|         public async Task<ModuleAdapter> GetModuleByIdService(string id) |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleByIdService", id); |  | ||||||
|             var cachedData = await cacheService.GetAsync<ModuleAdapter>(cacheKey); |  | ||||||
|  |  | ||||||
|             if (cachedData is not null) { return cachedData; } |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<ModuleAdapter>.Filter.And( |  | ||||||
|                     Builders<ModuleAdapter>.Filter.Eq("_id", ObjectId.Parse(id)), |  | ||||||
|                     Builders<ModuleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()) |  | ||||||
|                 ); |  | ||||||
|  |  | ||||||
|                 var module = await database.GetCollection<ModuleAdapter>(CollectionNames.Module) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, module, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return module; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetModuleByIdService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets all the modules. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <returns>A <see cref="{Task{IEnumerbale{ModuleAdapter}}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<IEnumerable<ModuleAdapter>> GetAllModulesService() |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesService"); |  | ||||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey) ?? []; |  | ||||||
|  |  | ||||||
|             if (cachedData.Any()) return cachedData; |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<ModuleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); |  | ||||||
|  |  | ||||||
|                 var roles = await database.GetCollection<ModuleAdapter>(CollectionNames.Module) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .SortBy(m => m.Application) |  | ||||||
|                                     .ThenBy(m => m.Order) |  | ||||||
|                                     .ToListAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return roles; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetAllModulesService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets all the modules by modules identifier list. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="modules">The list of modules identifiers.</param> |  | ||||||
|         /// <returns>A <see cref="Task{IEnumerable{ModuleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<IEnumerable<ModuleAdapter>> GetAllModulesByListService(string[] modules) |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByListService", modules); |  | ||||||
|  |  | ||||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey); |  | ||||||
|  |  | ||||||
|             if (cachedData != null && cachedData.Any()) return cachedData; |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var objectIds = modules.Select(id => ObjectId.Parse(id)).ToArray(); |  | ||||||
|  |  | ||||||
|                 var filter = Builders<ModuleAdapter>.Filter.In("_id", objectIds) |  | ||||||
|                                 & Builders<ModuleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); |  | ||||||
|  |  | ||||||
|                 var roles = await database.GetCollection<ModuleAdapter>(CollectionNames.Module) |  | ||||||
|                                           .Find(filter) |  | ||||||
|                                           .SortBy(m => m.Application) |  | ||||||
|                                           .ThenBy(m => m.Order) |  | ||||||
|                                           .ToListAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return roles; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetAllModulesByListService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         /// <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>A <see cref="{Task{ModuleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<ModuleAdapter> ChangeModuleStatusService(string id, StatusEnum newStatus) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<ModuleAdapter>.Filter |  | ||||||
|                     .Eq("_id", ObjectId.Parse(id)); |  | ||||||
|  |  | ||||||
|                 var update = Builders<ModuleAdapter>.Update |  | ||||||
|                             //.Set(v => v.Status, newStatus) |  | ||||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) |  | ||||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<ModuleAdapter>(CollectionNames.Module).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedModule = await database.GetCollection<ModuleAdapter>(CollectionNames.Module) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 return updatedModule; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"ChangeModuleStatusService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Updates a Module by id. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="entity">The Module to be updated.</param> |  | ||||||
|         /// <param name="id">The Module identifier.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{ModuleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<ModuleAdapter> UpdateModuleService(ModuleAdapter entity, string id) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<ModuleAdapter>.Filter |  | ||||||
|                     .Eq("_id", ObjectId.Parse(id)); |  | ||||||
|  |  | ||||||
|                 var update = Builders<ModuleAdapter>.Update |  | ||||||
|                             .Set(v => v.Name, entity.Name) |  | ||||||
|                             .Set(v => v.Description, entity.Description) |  | ||||||
|                             .Set(v => v.Icon, entity.Icon) |  | ||||||
|                             .Set(v => v.Route, entity.Route) |  | ||||||
|                             .Set(v => v.Order, entity.Order) |  | ||||||
|                             .Set(v => v.Application, entity.Application) |  | ||||||
|                             .Set(v => v.Status, entity.Status) |  | ||||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) |  | ||||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<ModuleAdapter>(CollectionNames.Module).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedModule = await database.GetCollection<ModuleAdapter>(CollectionNames.Module) |  | ||||||
|                                             .Find(filter) |  | ||||||
|                                             .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 return updatedModule; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"UpdateModuleService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private async Task<int?> GetLastOrderModule(ModuleRequest newModule) |  | ||||||
|         { |  | ||||||
|             var filter = Builders<ModuleAdapter>.Filter.And( |  | ||||||
|                 Builders<ModuleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()), |  | ||||||
|                 Builders<ModuleAdapter>.Filter.Eq("application", newModule.Application.ToString())); |  | ||||||
|  |  | ||||||
|             var maxOrderModule = await database.GetCollection<ModuleAdapter>(CollectionNames.Module) |  | ||||||
|                                                .Find(filter) |  | ||||||
|                                                .SortByDescending(m => m.Order) |  | ||||||
|                                                .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|             return (maxOrderModule is not null && maxOrderModule.Order is not null) ? maxOrderModule.Order : 0; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -67,7 +67,7 @@ namespace Core.Thalos.Provider | |||||||
|             services.AddScoped<IRoleService, RoleService>(); |             services.AddScoped<IRoleService, RoleService>(); | ||||||
|             services.AddScoped<IPermissionService, PermissionService>(); |             services.AddScoped<IPermissionService, PermissionService>(); | ||||||
|             services.AddScoped<IPermissionService, PermissionService>(); |             services.AddScoped<IPermissionService, PermissionService>(); | ||||||
|             services.AddScoped<IModuleService, ModuleService>(); |             services.AddScoped<IModuleProvider, ModuleProvider>(); | ||||||
|             return services; |             return services; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 GitHub
						GitHub