191 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			191 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| // ***********************************************************************
 | |
| // <copyright file="RoleService.cs">
 | |
| //     AgileWebs
 | |
| // </copyright>
 | |
| // ***********************************************************************
 | |
| 
 | |
| using Core.Blueprint.Mongo;
 | |
| using Core.Blueprint.Redis;
 | |
| using Core.Blueprint.Redis.Helpers;
 | |
| using Core.Thalos.BuildingBlocks;
 | |
| using Core.Thalos.Domain.Contexts.Onboarding.Request;
 | |
| using Core.Thalos.Provider.Contracts;
 | |
| using Mapster;
 | |
| using MongoDB.Driver;
 | |
| 
 | |
| 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 ICacheSettings cacheSettings;
 | |
|         private readonly IRedisCacheProvider cacheProvider;
 | |
| 
 | |
|         public RoleProvider(
 | |
|             CollectionRepository<RoleAdapter> repository,
 | |
|             IRedisCacheProvider cacheProvider,
 | |
|             ICacheSettings cacheSettings)
 | |
|         {
 | |
|             this.repository = repository;
 | |
|             this.repository.CollectionInitialization();
 | |
|             this.cacheProvider = cacheProvider;
 | |
|             this.cacheSettings = cacheSettings;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Creates a new Role.
 | |
|         /// </summary>
 | |
|         /// <param name="newRole">The Role to be created.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{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 a Role by its identifier.
 | |
|         /// </summary>
 | |
|         /// <param name="_id">The Role Mongo identifier.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{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, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
 | |
| 
 | |
|             return role;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets all Roles.
 | |
|         /// </summary>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{IEnumerable{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, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
 | |
| 
 | |
|             return roles;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Changes the status of a Role.
 | |
|         /// </summary>
 | |
|         /// <param name="_id">The Role Mongo identifier.</param>
 | |
|         /// <param name="newStatus">The new status of the Role.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{RoleAdapter}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<RoleAdapter?> ChangeRoleStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var entity = await repository.FindByIdAsync(_id);
 | |
| 
 | |
|             if (entity is not null)
 | |
|             {
 | |
|                 entity.Status = newStatus;
 | |
| 
 | |
|                 return repository.ReplaceOneAsync(entity).Result;
 | |
|             }
 | |
|             else return null;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Updates a Role.
 | |
|         /// </summary>
 | |
|         /// <param name="entity">The Role to be updated.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{RoleAdapter}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<RoleAdapter?> UpdateRole(RoleAdapter entity, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var updatedEntity = await repository.ReplaceOneAsync(entity);
 | |
|             return updatedEntity;
 | |
|         }
 | |
| 
 | |
|         /// <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>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{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 == 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>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{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 == Blueprint.Mongo.StatusEnum.Active);
 | |
| 
 | |
|             var updatedApplications = role.Applications?
 | |
|                 .Where(c => c != application)
 | |
|                 .ToArray();
 | |
| 
 | |
|             role.Applications = updatedApplications;
 | |
|             await repository.ReplaceOneAsync(role);
 | |
| 
 | |
|             return role;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Deletes a Role by identifier.
 | |
|         /// </summary>
 | |
|         /// <param name="_id">The Role Mongo identifier.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{RoleAdapter}"/> representing the asynchronous deletion result.
 | |
|         /// The deleted Role entity if found; otherwise, null.
 | |
|         /// </returns>
 | |
|         public async ValueTask<RoleAdapter?> DeleteRole(string _id, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var entity = await repository.DeleteOneAsync(doc => doc._Id == _id);
 | |
|             return entity;
 | |
|         }
 | |
|     }
 | |
| }
 |