Add project files.
This commit is contained in:
		
							
								
								
									
										256
									
								
								Core.Cerberos.Provider/Providers/Onboarding/ModuleService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										256
									
								
								Core.Cerberos.Provider/Providers/Onboarding/ModuleService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,256 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="ModuleService.cs"> | ||||
| //     Heath | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
| using Core.Cerberos.Adapters; | ||||
| using Core.Cerberos.Adapters.Common.Constants; | ||||
| using Core.Cerberos.Adapters.Common.Enums; | ||||
| using Core.Cerberos.Domain.Contexts.Onboarding.Mappers; | ||||
| using Core.Cerberos.Domain.Contexts.Onboarding.Request; | ||||
| using Core.Cerberos.Infraestructure.Caching.Configs; | ||||
| using Core.Cerberos.Infraestructure.Caching.Contracts; | ||||
| using Core.Cerberos.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.Cerberos.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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin