2021-12-11 04:32:23 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2024-05-09 02:18:02 +02:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2023-05-30 10:59:03 +02:00
|
|
|
using BTCPayServer.Client;
|
2021-12-11 04:32:23 +01:00
|
|
|
using BTCPayServer.Data;
|
2024-04-04 09:31:04 +02:00
|
|
|
using BTCPayServer.Payments.Bitcoin;
|
2024-05-09 02:18:02 +02:00
|
|
|
using BTCPayServer.Services;
|
2024-04-04 09:31:04 +02:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2021-12-11 04:32:23 +01:00
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Components.StoreSelector
|
|
|
|
{
|
|
|
|
public class StoreSelector : ViewComponent
|
|
|
|
{
|
|
|
|
private readonly StoreRepository _storeRepo;
|
2024-05-09 02:18:02 +02:00
|
|
|
private readonly UriResolver _uriResolver;
|
2021-12-11 04:32:23 +01:00
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
|
2021-12-31 08:36:38 +01:00
|
|
|
public StoreSelector(
|
2021-12-31 08:59:02 +01:00
|
|
|
StoreRepository storeRepo,
|
2024-05-09 02:18:02 +02:00
|
|
|
UriResolver uriResolver,
|
2021-12-31 08:36:38 +01:00
|
|
|
UserManager<ApplicationUser> userManager)
|
2021-12-11 04:32:23 +01:00
|
|
|
{
|
|
|
|
_storeRepo = storeRepo;
|
2024-05-09 02:18:02 +02:00
|
|
|
_uriResolver = uriResolver;
|
2021-12-11 04:32:23 +01:00
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IViewComponentResult> InvokeAsync()
|
|
|
|
{
|
|
|
|
var userId = _userManager.GetUserId(UserClaimsPrincipal);
|
|
|
|
var stores = await _storeRepo.GetStoresByUserId(userId);
|
|
|
|
var currentStore = ViewContext.HttpContext.GetStoreData();
|
2023-09-11 02:59:17 +02:00
|
|
|
var archivedCount = stores.Count(s => s.Archived);
|
2021-12-11 04:32:23 +01:00
|
|
|
var options = stores
|
2023-09-11 02:59:17 +02:00
|
|
|
.Where(store => !store.Archived)
|
2021-12-31 08:36:38 +01:00
|
|
|
.Select(store =>
|
2024-04-04 09:31:04 +02:00
|
|
|
new StoreSelectorOption
|
2021-12-11 04:32:23 +01:00
|
|
|
{
|
2024-04-04 09:31:04 +02:00
|
|
|
Text = store.StoreName,
|
|
|
|
Value = store.Id,
|
|
|
|
Selected = store.Id == currentStore?.Id,
|
|
|
|
Store = store
|
2021-12-11 04:32:23 +01:00
|
|
|
})
|
2022-04-05 13:34:14 +02:00
|
|
|
.OrderBy(s => s.Text)
|
2021-12-11 04:32:23 +01:00
|
|
|
.ToList();
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2022-10-17 12:16:29 +02:00
|
|
|
var blob = currentStore?.GetStoreBlob();
|
|
|
|
|
2021-12-11 04:32:23 +01:00
|
|
|
var vm = new StoreSelectorViewModel
|
|
|
|
{
|
|
|
|
Options = options,
|
|
|
|
CurrentStoreId = currentStore?.Id,
|
2022-01-18 02:20:59 +01:00
|
|
|
CurrentDisplayName = currentStore?.StoreName,
|
2024-05-09 02:18:02 +02:00
|
|
|
CurrentStoreLogoUrl = await _uriResolver.Resolve(Request.GetAbsoluteRootUri(), blob?.LogoUrl),
|
2023-09-11 02:59:17 +02:00
|
|
|
ArchivedCount = archivedCount
|
2021-12-11 04:32:23 +01:00
|
|
|
};
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-12-11 04:32:23 +01:00
|
|
|
return View(vm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|