diff --git a/BTCPayServer/Controllers/ServerController.cs b/BTCPayServer/Controllers/ServerController.cs index 6ca54f006..b092defd8 100644 --- a/BTCPayServer/Controllers/ServerController.cs +++ b/BTCPayServer/Controllers/ServerController.cs @@ -44,12 +44,11 @@ namespace BTCPayServer.Controllers private UserManager _UserManager; SettingsRepository _SettingsRepository; private readonly NBXplorerDashboard _dashBoard; - private RateFetcher _RateProviderFactory; private StoreRepository _StoreRepository; LightningConfigurationProvider _LnConfigProvider; private readonly TorServices _torServices; - BTCPayServerOptions _Options; - ApplicationDbContextFactory _ContextFactory; + private BTCPayServerOptions _Options; + private readonly AppService _AppService; private readonly StoredFileRepository _StoredFileRepository; private readonly FileService _FileService; private readonly IEnumerable _StorageProviderServices; @@ -59,14 +58,13 @@ namespace BTCPayServer.Controllers FileService fileService, IEnumerable storageProviderServices, BTCPayServerOptions options, - RateFetcher rateProviderFactory, SettingsRepository settingsRepository, NBXplorerDashboard dashBoard, IHttpClientFactory httpClientFactory, LightningConfigurationProvider lnConfigProvider, TorServices torServices, StoreRepository storeRepository, - ApplicationDbContextFactory contextFactory) + AppService appService) { _Options = options; _StoredFileRepository = storedFileRepository; @@ -76,11 +74,10 @@ namespace BTCPayServer.Controllers _SettingsRepository = settingsRepository; _dashBoard = dashBoard; HttpClientFactory = httpClientFactory; - _RateProviderFactory = rateProviderFactory; _StoreRepository = storeRepository; _LnConfigProvider = lnConfigProvider; _torServices = torServices; - _ContextFactory = contextFactory; + _AppService = appService; } [Route("server/rates")] @@ -503,19 +500,16 @@ namespace BTCPayServer.Controllers if (appIdsToFetch.Any()) { - using (var ctx = _ContextFactory.CreateContext()) + var apps = (await _AppService.GetApps(appIdsToFetch.ToArray())) + .ToDictionary(data => data.Id, data => Enum.Parse(data.AppType));; + if (!string.IsNullOrEmpty(settings.RootAppId)) { - var apps = await ctx.Apps.Where(data => appIdsToFetch.Contains(data.Id)) - .ToDictionaryAsync(data => data.Id, data => Enum.Parse(data.AppType)); - if (!string.IsNullOrEmpty(settings.RootAppId)) - { - settings.RootAppType = apps[settings.RootAppId]; - } + settings.RootAppType = apps[settings.RootAppId]; + } - foreach (var domainToAppMappingItem in settings.DomainToAppMapping) - { - domainToAppMappingItem.AppType = apps[domainToAppMappingItem.AppId]; - } + foreach (var domainToAppMappingItem in settings.DomainToAppMapping) + { + domainToAppMappingItem.AppType = apps[domainToAppMappingItem.AppId]; } } @@ -585,18 +579,10 @@ namespace BTCPayServer.Controllers private async Task> GetAppSelectList() { - // load display app dropdown - using (var ctx = _ContextFactory.CreateContext()) - { - var userId = _UserManager.GetUserId(base.User); - var selectList = await ctx.Users.Where(user => user.Id == userId) - .SelectMany(s => s.UserStores) - .Select(s => s.StoreData) - .SelectMany(s => s.Apps) - .Select(a => new SelectListItem($"{a.AppType} - {a.Name}", a.Id)).ToListAsync(); - selectList.Insert(0, new SelectListItem("(None)", null)); - return selectList; - } + var apps = (await _AppService.GetAllApps(null, true)) + .Select(a => new SelectListItem($"{a.AppType} - {a.AppName} - {a.StoreName}", a.Id)).ToList(); + apps.Insert(0, new SelectListItem("(None)", null)); + return apps; } private static bool TryParseAsExternalService(TorService torService, out ExternalService externalService) diff --git a/BTCPayServer/Services/Apps/AppService.cs b/BTCPayServer/Services/Apps/AppService.cs index 689f284ca..f41a2366d 100644 --- a/BTCPayServer/Services/Apps/AppService.cs +++ b/BTCPayServer/Services/Apps/AppService.cs @@ -231,7 +231,21 @@ namespace BTCPayServer.Services.Apps .ToArrayAsync(); } } + + public async Task> GetApps(string[] appIds, bool includeStore = false) + { + using (var ctx = _ContextFactory.CreateContext()) + { + var query = ctx.Apps + .Where(us => appIds.Contains(us.Id)); + if (includeStore) + { + query = query.Include(data => data.StoreData); + } + return await query.ToListAsync(); + } + } public async Task GetApp(string appId, AppType appType, bool includeStore = false) {