btcpayserver/BTCPayServer/Controllers/AppsController.cs

200 lines
7.3 KiB
C#
Raw Normal View History

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;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Abstractions.Models;
2018-04-03 04:50:41 +02:00
using BTCPayServer.Data;
using BTCPayServer.Models;
using BTCPayServer.Models.AppViewModels;
using BTCPayServer.Security;
2018-08-22 10:26:49 +02:00
using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Mails;
using BTCPayServer.Services.Rates;
using Microsoft.AspNetCore.Authorization;
2018-04-03 04:50:41 +02:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
2019-10-12 13:35:30 +02:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
2018-04-03 04:50:41 +02:00
[AutoValidateAntiforgeryToken]
[Route("apps")]
public partial class AppsController : Controller
{
public AppsController(
UserManager<ApplicationUser> userManager,
ApplicationDbContextFactory contextFactory,
2018-12-28 17:38:20 +01:00
EventAggregator eventAggregator,
BTCPayNetworkProvider networkProvider,
CurrencyNameTable currencies,
EmailSenderFactory emailSenderFactory,
2019-02-19 05:04:58 +01:00
AppService AppService)
2018-04-03 04:50:41 +02:00
{
_UserManager = userManager;
_ContextFactory = contextFactory;
2018-12-28 17:38:20 +01:00
_EventAggregator = eventAggregator;
_NetworkProvider = networkProvider;
_currencies = currencies;
_emailSenderFactory = emailSenderFactory;
2019-02-19 05:04:58 +01:00
_AppService = AppService;
2018-04-03 04:50:41 +02:00
}
private readonly UserManager<ApplicationUser> _UserManager;
private readonly ApplicationDbContextFactory _ContextFactory;
2018-12-28 17:38:20 +01:00
private readonly EventAggregator _EventAggregator;
private readonly BTCPayNetworkProvider _NetworkProvider;
private readonly CurrencyNameTable _currencies;
private readonly EmailSenderFactory _emailSenderFactory;
private readonly AppService _AppService;
public string CreatedAppId { get; set; }
public async Task<IActionResult> ListApps(
string sortOrder = null,
string sortOrderColumn = null
)
2018-04-03 04:50:41 +02:00
{
2019-02-19 05:04:58 +01:00
var apps = await _AppService.GetAllApps(GetUserId());
if (sortOrder != null && sortOrderColumn != null)
{
apps = apps.OrderByDescending(app =>
{
switch (sortOrderColumn)
{
2020-07-21 05:02:14 +02:00
case nameof(app.AppName):
return app.AppName;
2020-07-21 05:02:14 +02:00
case nameof(app.StoreName):
return app.StoreName;
2020-07-21 05:02:14 +02:00
case nameof(app.AppType):
return app.AppType;
default:
return app.Id;
}
}).ToArray();
switch (sortOrder)
{
case "desc":
ViewData[$"{sortOrderColumn}SortOrder"] = "asc";
break;
case "asc":
apps = apps.Reverse().ToArray();
ViewData[$"{sortOrderColumn}SortOrder"] = "desc";
break;
}
}
2018-04-03 04:50:41 +02:00
return View(new ListAppsViewModel()
{
Apps = apps
});
}
[HttpPost]
[Route("{appId}/delete")]
public async Task<IActionResult> DeleteAppPost(string appId)
{
var appData = await GetOwnedApp(appId);
if (appData == null)
return NotFound();
2019-02-19 05:04:58 +01:00
if (await _AppService.DeleteApp(appData))
TempData[WellKnownTempData.SuccessMessage] = "App deleted successfully.";
2018-04-03 04:50:41 +02:00
return RedirectToAction(nameof(ListApps));
}
[HttpGet]
[Route("create")]
public async Task<IActionResult> CreateApp()
{
2019-02-19 05:04:58 +01:00
var stores = await _AppService.GetOwnedStores(GetUserId());
2018-04-03 04:50:41 +02:00
if (stores.Length == 0)
{
TempData.SetStatusMessageModel(new StatusMessageModel()
{
Html =
2020-04-05 19:17:49 +02:00
$"Error: You need to create at least one store. <a href='{(Url.Action("CreateStore", "UserStores"))}' class='alert-link'>Create store</a>",
Severity = StatusMessageModel.StatusSeverity.Error
});
2018-04-03 04:50:41 +02:00
return RedirectToAction(nameof(ListApps));
}
var vm = new CreateAppViewModel();
vm.SetStores(stores);
return View(vm);
}
[HttpPost]
[Route("create")]
public async Task<IActionResult> CreateApp(CreateAppViewModel vm)
{
2019-02-19 05:04:58 +01:00
var stores = await _AppService.GetOwnedStores(GetUserId());
2018-04-03 04:50:41 +02:00
if (stores.Length == 0)
{
TempData.SetStatusMessageModel(new StatusMessageModel()
{
Html =
2020-04-05 19:17:49 +02:00
$"Error: You need to create at least one store. <a href='{(Url.Action("CreateStore", "UserStores"))}' class='alert-link'>Create store</a>",
Severity = StatusMessageModel.StatusSeverity.Error
});
2018-04-03 04:50:41 +02:00
return RedirectToAction(nameof(ListApps));
}
var selectedStore = vm.SelectedStore;
vm.SetStores(stores);
vm.SelectedStore = selectedStore;
if (!Enum.TryParse<AppType>(vm.SelectedAppType, out AppType appType))
ModelState.AddModelError(nameof(vm.SelectedAppType), "Invalid App Type");
if (!ModelState.IsValid)
{
return View(vm);
}
if (!stores.Any(s => s.Id == selectedStore))
{
TempData[WellKnownTempData.ErrorMessage] = "You are not owner of this store";
2018-04-03 04:50:41 +02:00
return RedirectToAction(nameof(ListApps));
}
var appData = new AppData
2018-04-03 04:50:41 +02:00
{
2020-06-28 10:55:27 +02:00
StoreDataId = selectedStore,
Name = vm.Name,
AppType = appType.ToString()
};
await _AppService.UpdateOrCreateApp(appData);
TempData[WellKnownTempData.SuccessMessage] = "App successfully created";
CreatedAppId = appData.Id;
2018-12-29 11:52:07 +01:00
switch (appType)
{
case AppType.PointOfSale:
return RedirectToAction(nameof(UpdatePointOfSale), new { appId = appData.Id });
2018-12-29 11:52:07 +01:00
case AppType.Crowdfund:
return RedirectToAction(nameof(UpdateCrowdfund), new { appId = appData.Id });
2018-12-29 11:52:07 +01:00
default:
return RedirectToAction(nameof(ListApps));
}
2018-04-03 04:50:41 +02:00
}
[HttpGet("{appId}/delete")]
2018-04-03 04:50:41 +02:00
public async Task<IActionResult> DeleteApp(string appId)
{
var appData = await GetOwnedApp(appId);
if (appData == null)
return NotFound();
return View("Confirm", new ConfirmModel("Delete app", $"The app <strong>{appData.Name}</strong> and its settings will be permanently deleted. Are you sure?", "Delete"));
2018-04-03 04:50:41 +02:00
}
private Task<AppData> GetOwnedApp(string appId, AppType? type = null)
2018-04-03 04:50:41 +02:00
{
2019-02-19 05:04:58 +01:00
return _AppService.GetAppDataIfOwner(GetUserId(), appId, type);
2018-04-03 04:50:41 +02:00
}
private string GetUserId()
{
return _UserManager.GetUserId(User);
}
}
}