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 _webCollection; private readonly IMongoCollection _mobileCollection; 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); _webCollection = database.GetCollection("WebSideMenus"); _mobileCollection = database.GetCollection("MobileSideMenus"); } public async Task> GetAllWebMenuSectionsAsync(Guid tenantId) { try { var filter = Builders.Filter.Eq(e => e.TenantId, tenantId); var result = await _webCollection .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 _webCollection .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 _webCollection.InsertManyAsync(newItems); return newItems; } catch (Exception ex) { _logger.LogError(ex, "Error occurred while adding Web Menu Section."); return new List(); } } public async Task> GetAllMobileMenuSectionsAsync(Guid tenantId) { try { var filter = Builders.Filter.Eq(e => e.TenantId, tenantId); var result = await _mobileCollection .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 _mobileCollection .Find(filter) .ToListAsync(); return result; } catch (Exception ex) { _logger.LogError(ex, "Error occurred while fetching Web Menu Sections."); return new List(); } } public async Task> AddMobileMenuItemAsync(List newItems) { try { await _mobileCollection.InsertManyAsync(newItems); return newItems; } catch (Exception ex) { _logger.LogError(ex, "Error occurred while adding Mobile Menu Section."); return new List(); } } } }