Add tenant services
This commit is contained in:
		
							
								
								
									
										140
									
								
								Core.Thalos.Provider/Providers/Onboarding/TenantProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								Core.Thalos.Provider/Providers/Onboarding/TenantProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,140 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="TenantService.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; | ||||
|  | ||||
| namespace Core.Thalos.Provider.Providers.Onboarding | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all services and business rules related to <see cref="TenantAdapter"/>. | ||||
|     /// </summary> | ||||
|     public class TenantProvider : ITenantProvider | ||||
|     { | ||||
|         private readonly CollectionRepository<TenantAdapter> repository; | ||||
|         private readonly ICacheSettings cacheSettings; | ||||
|         private readonly IRedisCacheProvider cacheProvider; | ||||
|  | ||||
|         public TenantProvider( | ||||
|             CollectionRepository<TenantAdapter> repository, | ||||
|             IRedisCacheProvider cacheProvider, | ||||
|             ICacheSettings cacheSettings) | ||||
|         { | ||||
|             this.repository = repository; | ||||
|             this.repository.CollectionInitialization(); | ||||
|             this.cacheSettings = cacheSettings; | ||||
|             this.cacheProvider = cacheProvider; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Tenant. | ||||
|         /// </summary> | ||||
|         /// <param name="newTenant">The Tenant to be created.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{TenantAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<TenantAdapter> CreateTenant(TenantRequest newTenant, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var tenant = newTenant.Adapt<TenantAdapter>(); | ||||
|             await repository.InsertOneAsync(tenant); | ||||
|             return tenant; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a Tenant by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="_id">The Tenant Mongo identifier.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{TenantAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<TenantAdapter> GetTenantById(string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTenantById", _id); | ||||
|             var cachedData = await cacheProvider.GetAsync<TenantAdapter>(cacheKey); | ||||
|  | ||||
|             if (cachedData is not null) return cachedData; | ||||
|  | ||||
|             var tenant = await repository.FindByIdAsync(_id); | ||||
|             await cacheProvider.SetAsync(cacheKey, tenant, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); | ||||
|  | ||||
|             return tenant; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all Tenants. | ||||
|         /// </summary> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{IEnumerable{TenantAdapter}}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<IEnumerable<TenantAdapter>> GetAllTenants(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTenants"); | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<TenantAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             //if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var tenants = await repository.AsQueryable(); | ||||
|             await cacheProvider.SetAsync(cacheKey, tenants, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); | ||||
|  | ||||
|             return tenants; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a Tenant. | ||||
|         /// </summary> | ||||
|         /// <param name="_id">The Tenant Mongo identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the Tenant.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{TenantAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<TenantAdapter> ChangeTenantStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await repository.FindByIdAsync(_id); | ||||
|             entity.Status = newStatus; | ||||
|  | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a Tenant. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Tenant to be updated.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{TenantAdapter}"/> representing the asynchronous execution of the service. | ||||
|         /// </returns> | ||||
|         public async ValueTask<TenantAdapter> UpdateTenant(TenantAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Deletes a Tenant by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="_id">The Tenant Mongo identifier.</param> | ||||
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> | ||||
|         /// <returns> | ||||
|         /// A <see cref="ValueTask{TenantAdapter}"/> representing the asynchronous deletion result. | ||||
|         /// The deleted Tenant entity if found; otherwise, null. | ||||
|         /// </returns> | ||||
|         public async ValueTask<TenantAdapter?> DeleteTenant(string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await repository.DeleteOneAsync(doc => doc._Id == _id); | ||||
|             return entity; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user