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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										234
									
								
								Core.Cerberos.Provider/Providers/Onboarding/PermissionService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										234
									
								
								Core.Cerberos.Provider/Providers/Onboarding/PermissionService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,234 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="PermissionService.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="PermissionAdapter"/>. | ||||
|     /// </summary> | ||||
|     public class PermissionService(ILogger<PermissionService> logger, IHttpContextAccessor httpContextAccessor, ICacheService cacheService, | ||||
|         IOptions<CacheSettings> cacheSettings, IMongoDatabase database) : IPermissionService | ||||
|     { | ||||
|         private readonly CacheSettings _cacheSettings = cacheSettings.Value; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Permission. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Permission to be created.</param> | ||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<PermissionAdapter> CreatePermissionService(PermissionRequest newPermission) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var entity = newPermission.ToAdapter(httpContextAccessor); | ||||
|                 await database.GetCollection<PermissionAdapter>(CollectionNames.Permission).InsertOneAsync(entity); | ||||
|                 entity.Id = (entity as dynamic ?? "").Id.ToString(); | ||||
|  | ||||
|                 return entity; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"CreatePermissionService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets an Permission by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Permission identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns>0 | ||||
|         public async Task<PermissionAdapter> GetPermissionByIdService(string id) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionByIdService", id); | ||||
|             var cachedData = await cacheService.GetAsync<PermissionAdapter>(cacheKey); | ||||
|  | ||||
|             if (cachedData is not null) { return cachedData; } | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<PermissionAdapter>.Filter.And( | ||||
|                     Builders<PermissionAdapter>.Filter.Eq("_id", ObjectId.Parse(id)), | ||||
|                     Builders<PermissionAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()) | ||||
|                 ); | ||||
|  | ||||
|                 var permission = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) | ||||
|                                     .Find(filter) | ||||
|                                     .FirstOrDefaultAsync(); | ||||
|  | ||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); | ||||
|  | ||||
|                 await cacheService.SetAsync(cacheKey, permission, cacheDuration); | ||||
|  | ||||
|                 return permission; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"GetPermissionByIdService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the permissions. | ||||
|         /// </summary> | ||||
|         /// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<IEnumerable<PermissionAdapter>> GetAllPermissionsService() | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsService"); | ||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<PermissionAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); | ||||
|  | ||||
|                 var roles = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) | ||||
|                                     .Find(filter) | ||||
|                                     .ToListAsync(); | ||||
|  | ||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); | ||||
|  | ||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); | ||||
|  | ||||
|                 return roles; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"GetAllPermissionsService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the permissions by permissions identifier list. | ||||
|         /// </summary> | ||||
|         /// <param name="permissions">The list of permissions identifiers.</param> | ||||
|         /// <returns>A <see cref="Task{IEnumerable{PermissionAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<IEnumerable<PermissionAdapter>> GetAllPermissionsByListService(string[] permissions) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByListService", permissions); | ||||
|  | ||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey); | ||||
|  | ||||
|             if (cachedData != null && cachedData.Any()) return cachedData; | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 var objectIds = permissions.Select(id => ObjectId.Parse(id)).ToArray(); | ||||
|  | ||||
|                 var filter = Builders<PermissionAdapter>.Filter.In("_id", objectIds) | ||||
|                                 & Builders<PermissionAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); | ||||
|  | ||||
|                 var roles = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) | ||||
|                                           .Find(filter) | ||||
|                                           .ToListAsync(); | ||||
|  | ||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); | ||||
|  | ||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); | ||||
|  | ||||
|                 return roles; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"GetAllPermissionsByListService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the permission. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The permission identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the permission.</param> | ||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<PermissionAdapter> ChangePermissionStatusService(string id, StatusEnum newStatus) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<PermissionAdapter>.Filter | ||||
|                     .Eq("_id", ObjectId.Parse(id)); | ||||
|  | ||||
|                 var update = Builders<PermissionAdapter>.Update | ||||
|                             .Set(v => v.Status, newStatus) | ||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) | ||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); | ||||
|  | ||||
|                 await database.GetCollection<PermissionAdapter>(CollectionNames.Permission).UpdateOneAsync(filter, update); | ||||
|  | ||||
|                 var updatedPermission = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) | ||||
|                                     .Find(filter) | ||||
|                                     .FirstOrDefaultAsync(); | ||||
|  | ||||
|                 return updatedPermission; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"ChangePermissionStatusService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a Permission by id. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Permission to be updated.</param> | ||||
|         /// <param name="id">The Permission identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<PermissionAdapter> UpdatePermissionService(PermissionAdapter entity, string id) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<PermissionAdapter>.Filter | ||||
|                     .Eq("_id", ObjectId.Parse(id)); | ||||
|  | ||||
|                 var update = Builders<PermissionAdapter>.Update | ||||
|                             .Set(v => v.Name, entity.Name) | ||||
|                             .Set(v => v.Description, entity.Description) | ||||
|                             .Set(v => v.AccessLevel, entity.AccessLevel) | ||||
|                             .Set(v => v.Status, entity.Status) | ||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) | ||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); | ||||
|  | ||||
|                 await database.GetCollection<PermissionAdapter>(CollectionNames.Permission).UpdateOneAsync(filter, update); | ||||
|  | ||||
|                 var updatedPermission = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) | ||||
|                                             .Find(filter) | ||||
|                                             .FirstOrDefaultAsync(); | ||||
|  | ||||
|                 return updatedPermission; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"UpdatePermissionService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										251
									
								
								Core.Cerberos.Provider/Providers/Onboarding/RoleService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										251
									
								
								Core.Cerberos.Provider/Providers/Onboarding/RoleService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,251 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="RoleService.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="RoleAdapter"/>. | ||||
|     /// </summary> | ||||
|     public class RoleService(ILogger<RoleService> logger, IHttpContextAccessor httpContextAccessor, ICacheService cacheService, | ||||
|         IOptions<CacheSettings> cacheSettings, IMongoDatabase database) : IRoleService | ||||
|     { | ||||
|         private readonly CacheSettings _cacheSettings = cacheSettings.Value; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Role. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Role to be created.</param> | ||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<RoleAdapter> CreateRoleService(RoleRequest newRole) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var entity = newRole.ToAdapter(httpContextAccessor); | ||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).InsertOneAsync(entity); | ||||
|                 entity.Id = (entity as dynamic ?? "").Id.ToString(); | ||||
|  | ||||
|                 return entity; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"CreateRoleService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets an Role by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Role identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<RoleAdapter> GetRoleByIdService(string id) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleByIdService", id); | ||||
|             var cachedData = await cacheService.GetAsync<RoleAdapter>(cacheKey); | ||||
|  | ||||
|             if (cachedData is not null) { return cachedData; } | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<RoleAdapter>.Filter.And( | ||||
|                         Builders<RoleAdapter>.Filter.Eq("_id", ObjectId.Parse(id)), | ||||
|                         Builders<RoleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()) | ||||
|                     ); | ||||
|  | ||||
|                 var role = await database.GetCollection<RoleAdapter>(CollectionNames.Role) | ||||
|                 .Find(filter) | ||||
|                             .FirstOrDefaultAsync(); | ||||
|  | ||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); | ||||
|  | ||||
|                 await cacheService.SetAsync(cacheKey, role, cacheDuration); | ||||
|  | ||||
|                 return role; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"GetRoleByIdService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the roles. | ||||
|         /// </summary> | ||||
|         /// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<IEnumerable<RoleAdapter>> GetAllRolesService() | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRolesService"); | ||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<RoleAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<RoleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); | ||||
|  | ||||
|                 var roles = await database.GetCollection<RoleAdapter>(CollectionNames.Role) | ||||
|                                 .Find(filter) | ||||
|                                 .ToListAsync(); | ||||
|  | ||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); | ||||
|  | ||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); | ||||
|  | ||||
|                 return roles; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"GetAllRolesService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the role. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The role identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the role.</param> | ||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<RoleAdapter> ChangeRoleStatusService(string id, StatusEnum newStatus) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<RoleAdapter>.Filter | ||||
|                     .Eq("_id", ObjectId.Parse(id)); | ||||
|  | ||||
|                 var update = Builders<RoleAdapter>.Update | ||||
|                             .Set(v => v.Status, newStatus) | ||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) | ||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); | ||||
|  | ||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); | ||||
|  | ||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) | ||||
|                                     .Find(filter) | ||||
|                                     .FirstOrDefaultAsync(); | ||||
|  | ||||
|                 return updatedRole; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"ChangeRoleStatusService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a Role by id. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Role to be updated.</param> | ||||
|         /// <param name="id">The Role identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async Task<RoleAdapter> UpdateRoleService(RoleAdapter entity, string id) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<RoleAdapter>.Filter | ||||
|                     .Eq("_id", ObjectId.Parse(id)); | ||||
|  | ||||
|                 var update = Builders<RoleAdapter>.Update | ||||
|                             .Set(v => v.Name, entity.Name) | ||||
|                             .Set(v => v.Description, entity.Description) | ||||
|                             .Set(v => v.Applications, entity.Applications) | ||||
|                             .Set(v => v.Modules, entity.Modules) | ||||
|                             .Set(v => v.Permissions, entity.Permissions) | ||||
|                             .Set(v => v.Status, entity.Status) | ||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) | ||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); | ||||
|  | ||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); | ||||
|  | ||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) | ||||
|                                     .Find(filter) | ||||
|                                     .FirstOrDefaultAsync(); | ||||
|  | ||||
|                 return updatedRole; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"UpdateRoleService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds an application to the role's list of applications. | ||||
|         /// </summary> | ||||
|         /// <param name="roleId">The identifier of the role to which the application will be added.</param> | ||||
|         /// <param name="application">The application enum value to add.</param> | ||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing the asynchronous operation, with the updated role object.</returns> | ||||
|         public async Task<RoleAdapter> AddApplicationToRoleService(string roleId, ApplicationsEnum application) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<RoleAdapter>.Filter.Eq("_id", ObjectId.Parse(roleId)); | ||||
|                 var update = Builders<RoleAdapter>.Update.AddToSet(r => r.Applications, application); | ||||
|  | ||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); | ||||
|  | ||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) | ||||
|                                     .Find(filter) | ||||
|                                     .FirstOrDefaultAsync(); | ||||
|                 return updatedRole; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"AddApplicationToRoleService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Removes an application from the role's list of applications. | ||||
|         /// </summary> | ||||
|         /// <param name="roleId">The identifier of the role from which the application will be removed.</param> | ||||
|         /// <param name="application">The application enum value to remove.</param> | ||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing the asynchronous operation, with the updated role object.</returns> | ||||
|         public async Task<RoleAdapter> RemoveApplicationFromRoleService(string roleId, ApplicationsEnum application) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var filter = Builders<RoleAdapter>.Filter.Eq("_id", ObjectId.Parse(roleId)); | ||||
|                 var update = Builders<RoleAdapter>.Update.Pull(r => r.Applications, application); | ||||
|  | ||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); | ||||
|  | ||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) | ||||
|                                     .Find(filter) | ||||
|                                     .FirstOrDefaultAsync(); | ||||
|                 return updatedRole; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"RemoveApplicationFromRoleService: Error in getting data - {ex.Message}"); | ||||
|                 throw new Exception(ex.Message, ex); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin