2020-06-29 04:44:35 +02:00
using System ;
2018-04-03 04:50:41 +02:00
using System.Linq ;
using System.Threading.Tasks ;
2020-11-17 13:46:23 +01:00
using BTCPayServer.Abstractions.Constants ;
2021-12-11 04:32:23 +01:00
using BTCPayServer.Client ;
2021-12-31 08:59:02 +01:00
using BTCPayServer.Data ;
2018-04-03 04:50:41 +02:00
using BTCPayServer.Models ;
using BTCPayServer.Models.AppViewModels ;
2018-08-22 10:26:49 +02:00
using BTCPayServer.Services.Apps ;
2019-02-17 08:53:41 +01:00
using BTCPayServer.Services.Rates ;
2021-10-25 09:54:36 +02:00
using BTCPayServer.Services.Stores ;
2018-08-30 20:16:24 +02:00
using Microsoft.AspNetCore.Authorization ;
2018-04-03 04:50:41 +02:00
using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.Mvc ;
namespace BTCPayServer.Controllers
{
2021-12-11 04:32:23 +01:00
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
2018-04-03 04:50:41 +02:00
[AutoValidateAntiforgeryToken]
[Route("apps")]
2022-01-07 04:32:00 +01:00
public partial class UIAppsController : Controller
2018-04-03 04:50:41 +02:00
{
2022-01-07 04:32:00 +01:00
public UIAppsController (
2018-04-03 04:50:41 +02:00
UserManager < ApplicationUser > userManager ,
2018-12-28 17:38:20 +01:00
EventAggregator eventAggregator ,
2019-02-17 08:53:41 +01:00
CurrencyNameTable currencies ,
2021-12-11 04:32:23 +01:00
StoreRepository storeRepository ,
AppService appService )
2018-04-03 04:50:41 +02:00
{
2021-12-11 04:32:23 +01:00
_userManager = userManager ;
_eventAggregator = eventAggregator ;
2019-02-17 08:53:41 +01:00
_currencies = currencies ;
2021-10-25 09:54:36 +02:00
_storeRepository = storeRepository ;
2021-12-11 04:32:23 +01:00
_appService = appService ;
2018-04-03 04:50:41 +02:00
}
2018-08-30 20:16:24 +02:00
2021-12-11 04:32:23 +01:00
private readonly UserManager < ApplicationUser > _userManager ;
private readonly EventAggregator _eventAggregator ;
2019-02-17 08:53:41 +01:00
private readonly CurrencyNameTable _currencies ;
2021-10-25 09:54:36 +02:00
private readonly StoreRepository _storeRepository ;
2021-12-11 04:32:23 +01:00
private readonly AppService _appService ;
2018-08-30 20:16:24 +02:00
2018-10-09 16:38:56 +02:00
public string CreatedAppId { get ; set ; }
2021-12-31 08:59:02 +01:00
2021-12-11 04:32:23 +01:00
[HttpGet("/stores/{storeId}/apps")]
2020-07-19 23:22:26 +02:00
public async Task < IActionResult > ListApps (
2021-12-31 08:59:02 +01:00
string storeId ,
2020-07-19 23:22:26 +02:00
string sortOrder = null ,
string sortOrderColumn = null
)
2018-04-03 04:50:41 +02:00
{
2021-12-20 15:15:32 +01:00
var store = GetCurrentStore ( ) ;
var apps = await _appService . GetAllApps ( GetUserId ( ) , false , store . Id ) ;
2020-07-19 23:22:26 +02:00
2021-12-31 08:59:02 +01:00
if ( sortOrder ! = null & & sortOrderColumn ! = null )
2020-07-19 23:22:26 +02:00
{
2021-12-31 08:59:02 +01:00
apps = apps . OrderByDescending ( app = >
2021-12-11 04:32:23 +01:00
{
switch ( sortOrderColumn )
2020-07-19 23:22:26 +02:00
{
2021-12-11 04:32:23 +01:00
case nameof ( app . AppName ) :
return app . AppName ;
case nameof ( app . StoreName ) :
return app . StoreName ;
case nameof ( app . AppType ) :
return app . AppType ;
default :
return app . Id ;
}
} ) . ToArray ( ) ;
2020-07-19 23:22:26 +02:00
switch ( sortOrder )
{
case "desc" :
ViewData [ $"{sortOrderColumn}SortOrder" ] = "asc" ;
break ;
case "asc" :
apps = apps . Reverse ( ) . ToArray ( ) ;
ViewData [ $"{sortOrderColumn}SortOrder" ] = "desc" ;
break ;
}
}
2021-12-31 08:59:02 +01:00
2021-12-11 04:32:23 +01:00
return View ( new ListAppsViewModel
2018-04-03 04:50:41 +02:00
{
Apps = apps
} ) ;
}
2021-12-11 04:32:23 +01:00
[HttpGet("/stores/{storeId}/apps/create")]
public IActionResult CreateApp ( string storeId )
2018-04-03 04:50:41 +02:00
{
2021-12-11 04:32:23 +01:00
return View ( new CreateAppViewModel
2018-04-03 04:50:41 +02:00
{
2021-12-20 15:15:32 +01:00
StoreId = GetCurrentStore ( ) . Id
2021-12-11 04:32:23 +01:00
} ) ;
2018-04-03 04:50:41 +02:00
}
2021-12-11 04:32:23 +01:00
[HttpPost("/stores/{storeId}/apps/create")]
public async Task < IActionResult > CreateApp ( string storeId , CreateAppViewModel vm )
2018-04-03 04:50:41 +02:00
{
2021-12-20 15:15:32 +01:00
var store = GetCurrentStore ( ) ;
vm . StoreId = store . Id ;
2018-04-03 04:50:41 +02:00
2021-12-11 04:32:23 +01:00
if ( ! Enum . TryParse ( vm . SelectedAppType , out AppType appType ) )
2018-04-03 04:50:41 +02:00
ModelState . AddModelError ( nameof ( vm . SelectedAppType ) , "Invalid App Type" ) ;
if ( ! ModelState . IsValid )
{
return View ( vm ) ;
}
2019-09-02 15:37:52 +02:00
var appData = new AppData
2018-04-03 04:50:41 +02:00
{
2021-12-20 15:15:32 +01:00
StoreDataId = store . Id ,
2021-10-29 12:29:02 +02:00
Name = vm . AppName ,
2019-09-02 15:37:52 +02:00
AppType = appType . ToString ( )
} ;
2021-10-25 09:54:36 +02:00
var defaultCurrency = await GetStoreDefaultCurrentIfEmpty ( appData . StoreDataId , null ) ;
switch ( appType )
{
case AppType . Crowdfund :
2021-12-11 04:32:23 +01:00
var emptyCrowdfund = new CrowdfundSettings { TargetCurrency = defaultCurrency } ;
2021-10-25 09:54:36 +02:00
appData . SetSettings ( emptyCrowdfund ) ;
break ;
case AppType . PointOfSale :
2021-12-11 04:32:23 +01:00
var empty = new PointOfSaleSettings { Currency = defaultCurrency } ;
2021-10-25 09:54:36 +02:00
appData . SetSettings ( empty ) ;
break ;
2022-06-28 05:03:13 +02:00
default :
throw new ArgumentOutOfRangeException ( ) ;
2021-10-25 09:54:36 +02:00
}
2021-12-11 04:32:23 +01:00
await _appService . UpdateOrCreateApp ( appData ) ;
2019-10-31 04:29:59 +01:00
TempData [ WellKnownTempData . SuccessMessage ] = "App successfully created" ;
2019-09-02 15:37:52 +02:00
CreatedAppId = appData . Id ;
2018-12-29 11:52:07 +01:00
2022-06-28 05:03:13 +02:00
return appType switch
2018-12-29 11:52:07 +01:00
{
2022-06-28 05:03:13 +02:00
AppType . PointOfSale = > RedirectToAction ( nameof ( UpdatePointOfSale ) , new { appId = appData . Id } ) ,
AppType . Crowdfund = > RedirectToAction ( nameof ( UpdateCrowdfund ) , new { appId = appData . Id } ) ,
_ = > throw new ArgumentOutOfRangeException ( )
} ;
2018-04-03 04:50:41 +02:00
}
2021-09-07 04:55:53 +02:00
[HttpGet("{appId}/delete")]
2021-12-16 17:37:19 +01:00
public IActionResult DeleteApp ( string appId )
2018-04-03 04:50:41 +02:00
{
2021-12-20 15:15:32 +01:00
var app = GetCurrentApp ( ) ;
if ( app = = null )
2018-04-03 04:50:41 +02:00
return NotFound ( ) ;
2021-12-31 08:59:02 +01:00
2021-12-20 15:15:32 +01:00
return View ( "Confirm" , new ConfirmModel ( "Delete app" , $"The app <strong>{app.Name}</strong> and its settings will be permanently deleted. Are you sure?" , "Delete" ) ) ;
2018-04-03 04:50:41 +02:00
}
2021-12-11 04:32:23 +01:00
[HttpPost("{appId}/delete")]
public async Task < IActionResult > DeleteAppPost ( string appId )
{
2021-12-20 15:15:32 +01:00
var app = GetCurrentApp ( ) ;
if ( app = = null )
2021-12-11 04:32:23 +01:00
return NotFound ( ) ;
2021-12-31 08:59:02 +01:00
2021-12-20 15:15:32 +01:00
if ( await _appService . DeleteApp ( app ) )
2021-12-11 04:32:23 +01:00
TempData [ WellKnownTempData . SuccessMessage ] = "App deleted successfully." ;
2021-12-31 08:59:02 +01:00
2022-07-12 08:18:08 +02:00
return RedirectToAction ( nameof ( UIStoresController . Dashboard ) , "UIStores" , new { storeId = app . StoreDataId } ) ;
2021-12-11 04:32:23 +01:00
}
async Task < string > GetStoreDefaultCurrentIfEmpty ( string storeId , string currency )
{
if ( string . IsNullOrWhiteSpace ( currency ) )
{
currency = ( await _storeRepository . FindStore ( storeId ) ) . GetStoreBlob ( ) . DefaultCurrency ;
}
return currency . Trim ( ) . ToUpperInvariant ( ) ;
}
2021-12-20 15:15:32 +01:00
private string GetUserId ( ) = > _userManager . GetUserId ( User ) ;
2021-12-11 04:32:23 +01:00
2021-12-20 15:15:32 +01:00
private StoreData GetCurrentStore ( ) = > HttpContext . GetStoreData ( ) ;
2021-12-31 08:59:02 +01:00
2021-12-20 15:15:32 +01:00
private AppData GetCurrentApp ( ) = > HttpContext . GetAppData ( ) ;
2018-04-03 04:50:41 +02:00
}
}