// *********************************************************************** // // AgileWebs // // *********************************************************************** using Core.Thalos.Adapters; 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 Core.Thalos.Domain.Contexts.Onboarding.Request; namespace Core.Thalos.Provider.Providers.Onboarding { /// /// Handles all services and business rules related to . /// public class ModuleProvider : IModuleProvider { private readonly CollectionRepository repository; private readonly CacheSettings cacheSettings; private readonly IRedisCacheProvider cacheProvider; public ModuleProvider(CollectionRepository repository, IRedisCacheProvider cacheProvider, IOptions cacheSettings) { this.repository = repository; this.repository.CollectionInitialization(); this.cacheSettings = cacheSettings.Value; this.cacheProvider = cacheProvider; } /// /// Creates a new Module. /// /// The Module to be created. /// A representing /// the asynchronous execution of the service. public async ValueTask CreateModule(ModuleRequest newModule, CancellationToken cancellationToken) { var moduleCollection = newModule.Adapt(); await repository.InsertOneAsync(moduleCollection); return moduleCollection; } /// /// Gets an Module by identifier. /// /// The Module identifier. /// A representing /// the asynchronous execution of the service.0 public async ValueTask GetModuleById(string _id, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleById", _id); var cachedData = await cacheProvider.GetAsync(cacheKey); if (cachedData is not null) { return cachedData; } var module = await repository.FindByIdAsync(_id); await cacheProvider.SetAsync(cacheKey, module); return module; } /// /// Gets all the modules. /// /// A representing /// the asynchronous execution of the service. public async ValueTask> GetAllModules(CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModules"); var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; if (cachedData.Any()) return cachedData; var modules = await repository.AsQueryable(); await cacheProvider.SetAsync(cacheKey, modules); return modules; } /// /// Gets all the modules by modules identifier list. /// /// The list of modules identifiers. /// A representing /// the asynchronous execution of the service. public async ValueTask> GetAllModulesByList(string[] modules, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByList", modules); var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; if (cachedData.Any()) return cachedData; var builder = Builders.Filter; var filters = new List>(); if (modules == null || !modules.Any()) { 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); return modulesList; } /// /// Changes the status of the module. /// /// The module identifier. /// The new status of the module. /// A representing /// the asynchronous execution of the service. public async ValueTask ChangeModuleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) { var entity = await repository.FindByIdAsync(id); entity.Status = newStatus; await repository.ReplaceOneAsync(entity); return entity; } /// /// Updates a Module by id. /// /// The Module to be updated. /// The Module identifier. /// A representing /// the asynchronous execution of the service. public async ValueTask UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken) { await repository.ReplaceOneAsync(entity); return entity; } } }