Use Blueprint.Mongo package in Role and Permission services
This commit is contained in:
		
							
								
								
									
										170
									
								
								Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										170
									
								
								Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,170 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="RoleService.cs"> | ||||
| //     AgileWebs | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
| using Core.Thalos.Adapters; | ||||
| using Core.Thalos.Adapters.Common.Enums; | ||||
| 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 MongoDB.Bson; | ||||
| using System.Text.RegularExpressions; | ||||
| using MongoDB.Bson.Serialization; | ||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||
| using Microsoft.Graph; | ||||
| using System.ComponentModel.Design; | ||||
|  | ||||
| namespace Core.Thalos.Provider.Providers.Onboarding | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all services and business rules related to <see cref="RoleAdapter"/>. | ||||
|     /// </summary> | ||||
|     public class RoleProvider : IRoleProvider | ||||
|     { | ||||
|         private readonly CollectionRepository<RoleAdapter> repository; | ||||
|         private readonly CacheSettings cacheSettings; | ||||
|         private readonly IRedisCacheProvider cacheProvider; | ||||
|  | ||||
|         public RoleProvider(CollectionRepository<RoleAdapter> repository, | ||||
|         IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings) | ||||
|         { | ||||
|             this.repository = repository; | ||||
|             this.repository.CollectionInitialization(); | ||||
|             this.cacheSettings = cacheSettings.Value; | ||||
|             this.cacheProvider = cacheProvider; | ||||
|         } | ||||
|  | ||||
|         /// <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 ValueTask<RoleAdapter> CreateRole(RoleRequest newRole, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var roleCollection = newRole.Adapt<RoleAdapter>(); | ||||
|  | ||||
|             await repository.InsertOneAsync(roleCollection); | ||||
|  | ||||
|             return roleCollection; | ||||
|         } | ||||
|  | ||||
|         /// <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 ValueTask<RoleAdapter> GetRoleById(string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleById", _id); | ||||
|             var cachedData = await cacheProvider.GetAsync<RoleAdapter>(cacheKey); | ||||
|  | ||||
|             if (cachedData is not null) { return cachedData; } | ||||
|  | ||||
|             var role = await repository.FindByIdAsync(_id); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, role); | ||||
|  | ||||
|             return role; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the roles. | ||||
|         /// </summary> | ||||
|         /// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<IEnumerable<RoleAdapter>> GetAllRoles(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRoles"); | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<RoleAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var roles = await repository.AsQueryable(); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, roles); | ||||
|  | ||||
|             return roles; | ||||
|         } | ||||
|  | ||||
|         /// <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 ValueTask<RoleAdapter> ChangeRoleStatus(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 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 ValueTask<RoleAdapter> UpdateRole(RoleAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|  | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <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 ValueTask<RoleAdapter> AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var role = await repository.FindOneAsync( | ||||
|                 u => u.Id == roleId && | ||||
|                 u.Status == Core.Blueprint.Mongo.StatusEnum.Active); | ||||
|  | ||||
|             var updatedApplications = role.Applications.Append(application).Distinct().ToArray(); | ||||
|             role.Applications = updatedApplications; | ||||
|  | ||||
|             await repository.ReplaceOneAsync(role); | ||||
|  | ||||
|             return role; | ||||
|         } | ||||
|  | ||||
|         /// <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 ValueTask<RoleAdapter> RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var role = await repository.FindOneAsync( | ||||
|                 u => u.Id == roleId && | ||||
|                 u.Status == Core.Blueprint.Mongo.StatusEnum.Active); | ||||
|  | ||||
|             var updatedApplications = role.Applications | ||||
|                     ?.Where(c => c != application) | ||||
|                     .ToArray(); | ||||
|  | ||||
|             role.Applications = updatedApplications; | ||||
|  | ||||
|             await repository.ReplaceOneAsync(role); | ||||
|  | ||||
|             return role; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales