177 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			177 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| // ***********************************************************************
 | |
| // <copyright file="ModuleService.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;
 | |
| using StatusEnum = Core.Blueprint.Mongo.StatusEnum;
 | |
| 
 | |
| namespace Core.Thalos.Provider.Providers.Onboarding
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Handles all services and business rules related to <see cref="ModuleAdapter"/>.
 | |
|     /// </summary>
 | |
|     public class ModuleProvider : IModuleProvider
 | |
|     {
 | |
|         private readonly CollectionRepository<ModuleAdapter> repository;
 | |
|         private readonly ICacheSettings cacheSettings;
 | |
|         private readonly IRedisCacheProvider cacheProvider;
 | |
| 
 | |
|         public ModuleProvider(
 | |
|             CollectionRepository<ModuleAdapter> repository,
 | |
|             IRedisCacheProvider cacheProvider,
 | |
|             ICacheSettings cacheSettings)
 | |
|         {
 | |
|             this.repository = repository;
 | |
|             this.repository.CollectionInitialization();
 | |
|             this.cacheProvider = cacheProvider;
 | |
|             this.cacheSettings = cacheSettings;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Creates a new Module.
 | |
|         /// </summary>
 | |
|         /// <param name="newModule">The Module to be created.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<ModuleAdapter> CreateModule(ModuleRequest newModule, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var moduleCollection = newModule.Adapt<ModuleAdapter>();
 | |
|             await repository.InsertOneAsync(moduleCollection);
 | |
|             return moduleCollection;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets a Module by identifier.
 | |
|         /// </summary>
 | |
|         /// <param name="_id">The Module Mongo identifier.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<ModuleAdapter> GetModuleById(string _id, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleById", _id);
 | |
|             var cachedData = await cacheProvider.GetAsync<ModuleAdapter>(cacheKey);
 | |
| 
 | |
|             if (cachedData is not null) return cachedData;
 | |
| 
 | |
|             var module = await repository.FindByIdAsync(_id);
 | |
|             await cacheProvider.SetAsync(cacheKey, module, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
 | |
| 
 | |
|             return module;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets all the Modules.
 | |
|         /// </summary>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{IEnumerable{ModuleAdapter}}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<IEnumerable<ModuleAdapter>> GetAllModules(CancellationToken cancellationToken)
 | |
|         {
 | |
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModules");
 | |
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey) ?? [];
 | |
| 
 | |
|             if (cachedData.Any()) return cachedData;
 | |
| 
 | |
|             var modules = await repository.AsQueryable();
 | |
|             await cacheProvider.SetAsync(cacheKey, modules);
 | |
| 
 | |
|             return modules;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets all the Modules by a list of identifiers.
 | |
|         /// </summary>
 | |
|         /// <param name="modules">The list of Module identifiers.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{IEnumerable{ModuleAdapter}}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<IEnumerable<ModuleAdapter>> GetAllModulesByList(string[] modules, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByList", modules);
 | |
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<ModuleAdapter>>(cacheKey) ?? [];
 | |
| 
 | |
|             if (cachedData.Any()) return cachedData;
 | |
| 
 | |
|             var builder = Builders<ModuleAdapter>.Filter;
 | |
|             var filters = new List<FilterDefinition<ModuleAdapter>>();
 | |
| 
 | |
|             if (modules is { Length: > 0 })
 | |
|             {
 | |
|                 filters.Add(builder.In(x => x._Id, modules));
 | |
|             }
 | |
| 
 | |
|             var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
 | |
|             var modulesList = await repository.FilterByMongoFilterAsync(finalFilter);
 | |
| 
 | |
|             await cacheProvider.SetAsync(cacheKey, modulesList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
 | |
|             return modulesList;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Changes the status of the Module.
 | |
|         /// </summary>
 | |
|         /// <param name="_id">The Module Mongo identifier.</param>
 | |
|         /// <param name="newStatus">The new status of the Module.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<ModuleAdapter?> ChangeModuleStatus(string _id, 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 Module.
 | |
|         /// </summary>
 | |
|         /// <param name="entity">The Module to be updated.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous execution of the service.
 | |
|         /// </returns>
 | |
|         public async ValueTask<ModuleAdapter?> UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var updatedEntity = await repository.ReplaceOneAsync(entity);
 | |
|             return updatedEntity;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Deletes a Module by identifier.
 | |
|         /// </summary>
 | |
|         /// <param name="_id">The Module Mongo identifier.</param>
 | |
|         /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 | |
|         /// <returns>
 | |
|         /// A <see cref="ValueTask{ModuleAdapter}"/> representing the asynchronous deletion result.
 | |
|         /// The deleted Module entity if found; otherwise, null.
 | |
|         /// </returns>
 | |
|         public async ValueTask<ModuleAdapter?> DeleteModule(string _id, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
 | |
|             return entity;
 | |
|         }
 | |
|     }
 | |
| }
 |