Add tenant services
This commit is contained in:
		| @@ -3,6 +3,7 @@ | ||||
| //     AgileWebs | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
|  | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Blueprint.Redis; | ||||
| using Core.Blueprint.Redis.Helpers; | ||||
| @@ -10,7 +11,6 @@ using Core.Thalos.BuildingBlocks; | ||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||
| using Core.Thalos.Provider.Contracts; | ||||
| using Mapster; | ||||
| using Microsoft.Extensions.Options; | ||||
| using MongoDB.Driver; | ||||
|  | ||||
| namespace Core.Thalos.Provider.Providers.Onboarding | ||||
| @@ -21,84 +21,87 @@ namespace Core.Thalos.Provider.Providers.Onboarding | ||||
|     public class PermissionProvider : IPermissionProvider | ||||
|     { | ||||
|         private readonly CollectionRepository<PermissionAdapter> repository; | ||||
|         private readonly CacheSettings cacheSettings; | ||||
|         private readonly ICacheSettings cacheSettings; | ||||
|         private readonly IRedisCacheProvider cacheProvider; | ||||
|  | ||||
|         public PermissionProvider(CollectionRepository<PermissionAdapter> repository, | ||||
|         public PermissionProvider( | ||||
|             CollectionRepository<PermissionAdapter> repository, | ||||
|             IRedisCacheProvider cacheProvider, | ||||
|             IOptions<CacheSettings> cacheSettings | ||||
|             ) | ||||
|             ICacheSettings cacheSettings) | ||||
|         { | ||||
|             this.repository = repository; | ||||
|             this.repository.CollectionInitialization(); | ||||
|             this.cacheSettings = cacheSettings.Value; | ||||
|             this.cacheProvider = cacheProvider; | ||||
|             this.cacheSettings = cacheSettings; | ||||
|         } | ||||
|  | ||||
|         /// <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> | ||||
|         /// <param name="newPermission">The Permission to be created.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<PermissionAdapter> CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var permissionCollection = newPermission.Adapt<PermissionAdapter>(); | ||||
|  | ||||
|             await repository.InsertOneAsync(permissionCollection); | ||||
|  | ||||
|             return permissionCollection; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets an Permission by identifier. | ||||
|         /// Gets a 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 | ||||
|         /// <param name="_id">The Permission Mongo identifier.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<PermissionAdapter> GetPermissionById(string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionById", _id); | ||||
|             var cachedData = await cacheProvider.GetAsync<PermissionAdapter>(cacheKey); | ||||
|  | ||||
|             //if (cachedData is not null) { return cachedData; } | ||||
|             if (cachedData is not null) return cachedData; | ||||
|  | ||||
|             var permission = await repository.FindByIdAsync(_id); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, permission); | ||||
|  | ||||
|             return permission; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the permissions. | ||||
|         /// Gets all Permissions. | ||||
|         /// </summary> | ||||
|         /// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{IEnumerable{PermissionAdapter}}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissions(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissions"); | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|             //if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var permissions = await repository.AsQueryable(); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, permissions); | ||||
|             await cacheProvider.SetAsync(cacheKey, permissions, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); | ||||
|  | ||||
|             return permissions; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the permissions by permissions identifier list. | ||||
|         /// Gets all Permissions by a list of identifiers. | ||||
|         /// </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> | ||||
|         /// <param name="permissions">The list of Permission identifiers.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{IEnumerable{PermissionAdapter}}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByList", permissions); | ||||
|  | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
| @@ -106,49 +109,61 @@ namespace Core.Thalos.Provider.Providers.Onboarding | ||||
|             var builder = Builders<PermissionAdapter>.Filter; | ||||
|             var filters = new List<FilterDefinition<PermissionAdapter>>(); | ||||
|  | ||||
|             if (permissions != null || !permissions.Any()) | ||||
|             if (permissions is { Length: > 0 }) | ||||
|             { | ||||
|                 filters.Add(builder.In(x => x._Id, permissions)); | ||||
|             } | ||||
|  | ||||
|             var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty; | ||||
|  | ||||
|             var permissionsList = await repository.FilterByMongoFilterAsync(finalFilter); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, permissionsList); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, permissionsList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); | ||||
|             return permissionsList; | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the permission. | ||||
|         /// Changes the status of a 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 ValueTask<PermissionAdapter> ChangePermissionStatus(string id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         /// <param name="_id">The Permission Mongo identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the Permission.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<PermissionAdapter> ChangePermissionStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await repository.FindByIdAsync(id); | ||||
|             var entity = await repository.FindByIdAsync(_id); | ||||
|             entity.Status = newStatus; | ||||
|  | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|  | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a Permission by id. | ||||
|         /// Updates a Permission. | ||||
|         /// </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> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<PermissionAdapter> UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Deletes a Permission by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="_id">The Permission Mongo identifier.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous deletion result. | ||||
|         /// The deleted Permission entity if found; otherwise, null. | ||||
|         /// </returns> | ||||
|         public async ValueTask<PermissionAdapter?> DeletePermission(string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id); | ||||
|             return entity; | ||||
|         } | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user