2022-01-20 17:35:25 +01:00
using System ;
2020-08-27 01:31:33 -07:00
using System.Linq ;
2018-03-23 16:24:57 +09:00
using System.Threading.Tasks ;
2020-11-17 13:46:23 +01:00
using BTCPayServer.Abstractions.Constants ;
2024-10-14 14:11:00 +09:00
using BTCPayServer.Abstractions.Extensions ;
2022-07-15 05:38:33 +02:00
using BTCPayServer.Abstractions.Models ;
2021-12-31 08:36:38 +01:00
using BTCPayServer.Client ;
2019-08-30 00:24:42 +09:00
using BTCPayServer.Data ;
2018-03-23 16:24:57 +09:00
using BTCPayServer.Models.StoreViewModels ;
2024-05-10 07:26:04 +01:00
using BTCPayServer.Services ;
2022-01-20 17:35:25 +01:00
using BTCPayServer.Services.Rates ;
2018-03-23 16:24:57 +09:00
using BTCPayServer.Services.Stores ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.Mvc ;
2022-01-20 17:35:25 +01:00
using Microsoft.AspNetCore.Mvc.Rendering ;
2024-10-14 14:11:00 +09:00
using Microsoft.Extensions.Localization ;
2018-03-23 16:24:57 +09:00
namespace BTCPayServer.Controllers
{
[Route("stores")]
[AutoValidateAntiforgeryToken]
2022-01-07 12:32:00 +09:00
public class UIUserStoresController : Controller
2018-03-23 16:24:57 +09:00
{
2021-12-31 08:36:38 +01:00
private readonly StoreRepository _repo ;
2024-10-14 14:11:00 +09:00
private readonly IStringLocalizer StringLocalizer ;
2024-05-10 07:26:04 +01:00
private readonly SettingsRepository _settingsRepository ;
2021-12-31 08:36:38 +01:00
private readonly UserManager < ApplicationUser > _userManager ;
2024-05-13 22:29:42 +09:00
private readonly DefaultRulesCollection _defaultRules ;
2022-01-20 17:35:25 +01:00
private readonly RateFetcher _rateFactory ;
public string CreatedStoreId { get ; set ; }
2018-03-23 16:24:57 +09:00
2022-01-07 12:32:00 +09:00
public UIUserStoresController (
2018-03-23 16:24:57 +09:00
UserManager < ApplicationUser > userManager ,
2024-05-13 22:29:42 +09:00
DefaultRulesCollection defaultRules ,
2022-01-20 17:35:25 +01:00
StoreRepository storeRepository ,
2024-10-14 14:11:00 +09:00
IStringLocalizer stringLocalizer ,
2024-05-10 07:26:04 +01:00
RateFetcher rateFactory ,
SettingsRepository settingsRepository )
2018-03-23 16:24:57 +09:00
{
2021-12-31 08:36:38 +01:00
_repo = storeRepository ;
2024-10-14 14:11:00 +09:00
StringLocalizer = stringLocalizer ;
2021-12-31 08:36:38 +01:00
_userManager = userManager ;
2024-05-13 22:29:42 +09:00
_defaultRules = defaultRules ;
2022-01-20 17:35:25 +01:00
_rateFactory = rateFactory ;
2024-05-10 07:26:04 +01:00
_settingsRepository = settingsRepository ;
2020-06-28 17:55:27 +09:00
}
2018-07-19 22:23:14 +09:00
2024-02-23 09:51:41 +01:00
[HttpGet]
2023-09-11 02:59:17 +02:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
public async Task < IActionResult > ListStores ( bool archived = false )
{
var stores = await _repo . GetStoresByUserId ( GetUserId ( ) ) ;
var vm = new ListStoresViewModel
{
Stores = stores
. Where ( s = > s . Archived = = archived )
. Select ( s = > new ListStoresViewModel . StoreViewModel
{
StoreId = s . Id ,
StoreName = s . StoreName ,
Archived = s . Archived
} ) . ToList ( ) ,
Archived = archived
} ;
return View ( vm ) ;
}
2021-12-31 08:36:38 +01:00
[HttpGet("create")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
2023-07-19 15:21:16 +02:00
public async Task < IActionResult > CreateStore ( bool skipWizard )
2018-07-19 22:23:14 +09:00
{
2023-05-10 11:18:29 +02:00
var stores = await _repo . GetStoresByUserId ( GetUserId ( ) ) ;
2022-01-20 17:35:25 +01:00
var vm = new CreateStoreViewModel
{
2023-07-19 15:21:16 +02:00
IsFirstStore = ! ( stores . Any ( ) | | skipWizard ) ,
2024-05-10 07:26:04 +01:00
DefaultCurrency = ( await _settingsRepository . GetSettingAsync < PoliciesSettings > ( ) ) ? . DefaultCurrency ? ? StoreBlob . StandardDefaultCurrency ,
2022-12-14 13:33:27 +09:00
Exchanges = GetExchangesSelectList ( null )
2022-01-20 17:35:25 +01:00
} ;
return View ( vm ) ;
2018-03-23 16:24:57 +09:00
}
2018-07-19 22:23:14 +09:00
2021-12-31 08:36:38 +01:00
[HttpPost("create")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
2020-10-15 23:30:46 -05:00
public async Task < IActionResult > CreateStore ( CreateStoreViewModel vm )
{
if ( ! ModelState . IsValid )
{
2023-05-10 11:18:29 +02:00
var stores = await _repo . GetStoresByUserId ( GetUserId ( ) ) ;
vm . IsFirstStore = ! stores . Any ( ) ;
2024-05-13 22:29:42 +09:00
vm . Exchanges = GetExchangesSelectList ( null ) ;
2020-10-15 23:30:46 -05:00
return View ( vm ) ;
}
2023-01-06 14:18:07 +01:00
2023-04-04 10:45:40 +09:00
var store = new StoreData { StoreName = vm . Name } ;
var blob = store . GetStoreBlob ( ) ;
blob . DefaultCurrency = vm . DefaultCurrency ;
blob . PreferredExchange = vm . PreferredExchange ;
store . SetStoreBlob ( blob ) ;
await _repo . CreateStore ( GetUserId ( ) , store ) ;
2020-10-15 23:30:46 -05:00
CreatedStoreId = store . Id ;
2024-10-14 14:11:00 +09:00
TempData . SetStatusSuccess ( StringLocalizer [ "Store successfully created" ] ) ;
2024-01-24 11:34:16 +01:00
return RedirectToAction ( nameof ( UIStoresController . Index ) , "UIStores" , new
2020-10-15 23:30:46 -05:00
{
storeId = store . Id
} ) ;
}
2021-09-07 04:55:53 +02:00
[HttpGet("{storeId}/me/delete")]
2021-12-31 08:36:38 +01:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
2018-04-30 02:33:42 +09:00
public IActionResult DeleteStore ( string storeId )
2018-03-23 16:24:57 +09:00
{
2018-04-30 02:33:42 +09:00
var store = HttpContext . GetStoreData ( ) ;
2018-03-23 16:24:57 +09:00
if ( store = = null )
return NotFound ( ) ;
2024-10-14 14:11:00 +09:00
return View ( "Confirm" , new ConfirmModel ( StringLocalizer [ "Delete store {0}" , store . StoreName ] , StringLocalizer [ "This store will still be accessible to users sharing it" ] , "Delete" ) ) ;
2018-03-23 16:24:57 +09:00
}
2021-09-07 04:55:53 +02:00
[HttpPost("{storeId}/me/delete")]
2021-12-31 08:36:38 +01:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
2018-03-23 16:24:57 +09:00
public async Task < IActionResult > DeleteStorePost ( string storeId )
{
var userId = GetUserId ( ) ;
2018-04-30 02:33:42 +09:00
var store = HttpContext . GetStoreData ( ) ;
2018-03-23 16:24:57 +09:00
if ( store = = null )
return NotFound ( ) ;
2021-12-31 08:36:38 +01:00
await _repo . RemoveStore ( storeId , userId ) ;
2024-10-14 14:11:00 +09:00
TempData . SetStatusSuccess ( StringLocalizer [ "Store removed successfully" ] ) ;
2022-01-07 12:32:00 +09:00
return RedirectToAction ( nameof ( UIHomeController . Index ) , "UIHome" ) ;
2018-03-23 16:24:57 +09:00
}
2021-12-31 08:36:38 +01:00
private string GetUserId ( ) = > _userManager . GetUserId ( User ) ;
2023-01-06 14:18:07 +01:00
2024-10-14 14:11:00 +09:00
internal SelectList GetExchangesSelectList ( StoreBlob storeBlob )
2024-05-13 22:29:42 +09:00
{
if ( storeBlob is null )
storeBlob = new StoreBlob ( ) ;
2024-10-14 14:11:00 +09:00
var defaultExchange = _defaultRules . GetRecommendedExchange ( storeBlob . DefaultCurrency ) ;
var exchanges = _rateFactory . RateProviderFactory
2024-05-13 22:29:42 +09:00
. AvailableRateProviders
. OrderBy ( s = > s . Id , StringComparer . OrdinalIgnoreCase )
. ToList ( ) ;
var exchange = exchanges . First ( e = > e . Id = = defaultExchange ) ;
2024-10-14 14:11:00 +09:00
exchanges . Insert ( 0 , new ( null , StringLocalizer [ "Recommendation ({0})" , exchange . DisplayName ] , "" ) ) ;
2024-05-13 22:29:42 +09:00
var chosen = exchanges . FirstOrDefault ( f = > f . Id = = storeBlob . PreferredExchange ) ? ? exchanges . First ( ) ;
return new SelectList ( exchanges , nameof ( chosen . Id ) , nameof ( chosen . DisplayName ) , chosen . Id ) ;
}
}
2018-03-23 16:24:57 +09:00
}