2020-08-27 10:31:33 +02:00
|
|
|
using System.Linq;
|
2018-03-23 08:24:57 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2019-08-29 17:24:42 +02:00
|
|
|
using BTCPayServer.Data;
|
2018-03-23 08:24:57 +01:00
|
|
|
using BTCPayServer.Models;
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2018-04-29 19:33:42 +02:00
|
|
|
using BTCPayServer.Security;
|
2018-03-23 08:24:57 +01:00
|
|
|
using BTCPayServer.Services.Stores;
|
2020-10-16 07:35:53 +02:00
|
|
|
using ExchangeSharp;
|
2018-03-23 08:24:57 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
[Route("stores")]
|
2019-10-12 13:35:30 +02:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
|
2018-03-23 08:24:57 +01:00
|
|
|
[AutoValidateAntiforgeryToken]
|
|
|
|
public partial class UserStoresController : Controller
|
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
private readonly StoreRepository _Repo;
|
|
|
|
private readonly BTCPayNetworkProvider _NetworkProvider;
|
|
|
|
private readonly UserManager<ApplicationUser> _UserManager;
|
2018-03-23 08:24:57 +01:00
|
|
|
|
|
|
|
public UserStoresController(
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
BTCPayNetworkProvider networkProvider,
|
|
|
|
StoreRepository storeRepository)
|
|
|
|
{
|
|
|
|
_Repo = storeRepository;
|
|
|
|
_NetworkProvider = networkProvider;
|
|
|
|
_UserManager = userManager;
|
2020-06-28 10:55:27 +02:00
|
|
|
}
|
2018-07-19 15:23:14 +02:00
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
[Route("create")]
|
|
|
|
public IActionResult CreateStore()
|
|
|
|
{
|
|
|
|
return View();
|
2018-03-23 08:24:57 +01:00
|
|
|
}
|
2018-07-19 15:23:14 +02:00
|
|
|
|
2020-10-16 06:30:46 +02:00
|
|
|
[HttpPost]
|
|
|
|
[Route("create")]
|
|
|
|
public async Task<IActionResult> CreateStore(CreateStoreViewModel vm)
|
|
|
|
{
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(vm);
|
|
|
|
}
|
|
|
|
var store = await _Repo.CreateStore(GetUserId(), vm.Name);
|
|
|
|
CreatedStoreId = store.Id;
|
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "Store successfully created";
|
|
|
|
return RedirectToAction(nameof(StoresController.UpdateStore), "Stores", new
|
|
|
|
{
|
|
|
|
storeId = store.Id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:23:14 +02:00
|
|
|
public string CreatedStoreId
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
|
2018-03-23 08:24:57 +01:00
|
|
|
[HttpGet]
|
2018-07-19 15:23:14 +02:00
|
|
|
[Route("{storeId}/me/delete")]
|
2018-04-29 19:33:42 +02:00
|
|
|
public IActionResult DeleteStore(string storeId)
|
2018-03-23 08:24:57 +01:00
|
|
|
{
|
2018-04-29 19:33:42 +02:00
|
|
|
var store = HttpContext.GetStoreData();
|
2018-03-23 08:24:57 +01:00
|
|
|
if (store == null)
|
|
|
|
return NotFound();
|
|
|
|
return View("Confirm", new ConfirmModel()
|
|
|
|
{
|
|
|
|
Title = "Delete store " + store.StoreName,
|
|
|
|
Description = "This store will still be accessible to users sharing it",
|
|
|
|
Action = "Delete"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
2018-07-19 15:23:14 +02:00
|
|
|
[Route("{storeId}/me/delete")]
|
2018-03-23 08:24:57 +01:00
|
|
|
public async Task<IActionResult> DeleteStorePost(string storeId)
|
|
|
|
{
|
|
|
|
var userId = GetUserId();
|
2018-04-29 19:33:42 +02:00
|
|
|
var store = HttpContext.GetStoreData();
|
2018-03-23 08:24:57 +01:00
|
|
|
if (store == null)
|
|
|
|
return NotFound();
|
|
|
|
await _Repo.RemoveStore(storeId, userId);
|
2019-10-31 04:29:59 +01:00
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "Store removed successfully";
|
2018-03-23 08:24:57 +01:00
|
|
|
return RedirectToAction(nameof(ListStores));
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2020-08-27 10:31:33 +02:00
|
|
|
public async Task<IActionResult> ListStores(
|
|
|
|
string sortOrder = null,
|
|
|
|
string sortOrderColumn = null
|
|
|
|
)
|
2018-03-23 08:24:57 +01:00
|
|
|
{
|
|
|
|
StoresViewModel result = new StoresViewModel();
|
|
|
|
var stores = await _Repo.GetStoresByUserId(GetUserId());
|
2020-08-27 10:31:33 +02:00
|
|
|
if (sortOrder != null && sortOrderColumn != null)
|
|
|
|
{
|
|
|
|
stores = stores.OrderByDescending(store =>
|
|
|
|
{
|
|
|
|
switch (sortOrderColumn)
|
|
|
|
{
|
|
|
|
case nameof(store.StoreName):
|
|
|
|
return store.StoreName;
|
|
|
|
case nameof(store.StoreWebsite):
|
|
|
|
return store.StoreWebsite;
|
|
|
|
default:
|
|
|
|
return store.Id;
|
|
|
|
}
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
switch (sortOrder)
|
|
|
|
{
|
|
|
|
case "desc":
|
|
|
|
ViewData[$"{sortOrderColumn}SortOrder"] = "asc";
|
|
|
|
break;
|
|
|
|
case "asc":
|
|
|
|
stores = stores.Reverse().ToArray();
|
|
|
|
ViewData[$"{sortOrderColumn}SortOrder"] = "desc";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 08:24:57 +01:00
|
|
|
for (int i = 0; i < stores.Length; i++)
|
|
|
|
{
|
|
|
|
var store = stores[i];
|
2020-10-16 07:35:53 +02:00
|
|
|
var blob = store.GetStoreBlob();
|
2018-03-23 08:24:57 +01:00
|
|
|
result.Stores.Add(new StoresViewModel.StoreViewModel()
|
|
|
|
{
|
|
|
|
Id = store.Id,
|
2020-10-16 07:35:53 +02:00
|
|
|
|
2018-03-23 08:24:57 +01:00
|
|
|
Name = store.StoreName,
|
|
|
|
WebSite = store.StoreWebsite,
|
2020-10-16 07:35:53 +02:00
|
|
|
IsOwner = store.Role == StoreRoles.Owner,
|
|
|
|
HintWalletWarning = blob.Hints.Wallet
|
2018-03-23 08:24:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return View(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetUserId()
|
|
|
|
{
|
|
|
|
return _UserManager.GetUserId(User);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|