using Marco.Pms.Model.AppMenu; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using MongoDB.Driver; namespace Marco.Pms.CacheHelper { public class SidebarMenuHelper { private readonly IMongoCollection _oldCollection; private readonly IMongoCollection _collection; private readonly ILogger _logger; public SidebarMenuHelper(IConfiguration configuration, ILogger logger) { _logger = logger; var connectionString = configuration["MongoDB:ModificationConnectionString"]; var mongoUrl = new MongoUrl(connectionString); var client = new MongoClient(mongoUrl); var database = client.GetDatabase(mongoUrl.DatabaseName); _oldCollection = database.GetCollection("Menus"); _collection = database.GetCollection("WebSideMenus"); } public async Task> GetAllWebMenuSectionsAsync(Guid tenantId) { try { var filter = Builders.Filter.Eq(e => e.TenantId, tenantId); var result = await _collection .Find(filter) .ToListAsync(); if (result.Any()) { return result; } tenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26"); filter = Builders.Filter.Eq(e => e.TenantId, tenantId); result = await _collection .Find(filter) .ToListAsync(); return result; } catch (Exception ex) { _logger.LogError(ex, "Error occurred while fetching Web Menu Sections."); return new List(); } } public async Task> AddWebMenuItemAsync(List newItems) { try { await _collection.InsertManyAsync(newItems); return newItems; } catch (Exception ex) { _logger.LogError(ex, "Error occurred while adding Web Menu Section."); return new List(); } } public async Task> GetAllMenuSectionsAsync(Guid tenantId) { var filter = Builders.Filter.Eq(e => e.TenantId, tenantId); var result = await _oldCollection .Find(filter) .ToListAsync(); if (result.Any()) { return result; } tenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26"); filter = Builders.Filter.Eq(e => e.TenantId, tenantId); result = await _oldCollection .Find(filter) .ToListAsync(); return result; } } }