btcpayserver/BTCPayServer/Controllers/AppsController.Crowdsale.cs

116 lines
4.1 KiB
C#
Raw Normal View History

2018-12-11 16:36:25 +01:00
using System;
using System.Threading.Tasks;
using BTCPayServer.Models.AppViewModels;
using BTCPayServer.Services.Apps;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
public partial class AppsController
{
2018-12-28 17:38:20 +01:00
public class CrowdfundAppUpdated
{
public string AppId { get; set; }
}
2018-12-11 16:36:25 +01:00
public class CrowdfundSettings
{
public string Title { get; set; }
public string Description { get; set; }
public bool Enabled { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string TargetCurrency { get; set; }
public decimal? TargetAmount { get; set; }
public bool EnforceTargetAmount { get; set; }
public string CustomCSSLink { get; set; }
2018-12-22 15:02:16 +01:00
public string MainImageUrl { get; set; }
public string NotificationUrl { get; set; }
public string Tagline { get; set; }
2018-12-22 15:43:40 +01:00
public string EmbeddedCSS { get; set; }
2018-12-29 11:52:07 +01:00
public string PerksTemplate { get; set; }
2018-12-11 16:36:25 +01:00
}
[HttpGet]
[Route("{appId}/settings/crowdfund")]
public async Task<IActionResult> UpdateCrowdfund(string appId)
{
var app = await GetOwnedApp(appId, AppType.Crowdfund);
if (app == null)
return NotFound();
var settings = app.GetSettings<CrowdfundSettings>();
var vm = new UpdateCrowdfundViewModel()
{
Title = settings.Title,
Enabled = settings.Enabled,
EnforceTargetAmount = settings.EnforceTargetAmount,
StartDate = settings.StartDate,
TargetCurrency = settings.TargetCurrency,
Description = settings.Description,
2018-12-22 15:02:16 +01:00
MainImageUrl = settings.MainImageUrl,
2018-12-22 15:43:40 +01:00
EmbeddedCSS = settings.EmbeddedCSS,
2018-12-11 16:36:25 +01:00
EndDate = settings.EndDate,
TargetAmount = settings.TargetAmount,
2018-12-22 15:02:16 +01:00
CustomCSSLink = settings.CustomCSSLink,
NotificationUrl = settings.NotificationUrl,
2018-12-28 17:38:20 +01:00
Tagline = settings.Tagline,
2018-12-29 11:52:07 +01:00
PerksTemplate = settings.PerksTemplate
2018-12-11 16:36:25 +01:00
};
return View(vm);
}
[HttpPost]
[Route("{appId}/settings/crowdfund")]
2018-12-29 11:52:07 +01:00
public async Task<IActionResult> UpdateCrowdfund(string appId, UpdateCrowdfundViewModel vm)
2018-12-11 16:36:25 +01:00
{
if (_AppsHelper.GetCurrencyData(vm.TargetCurrency, false) == null)
ModelState.AddModelError(nameof(vm.TargetCurrency), "Invalid currency");
2018-12-29 11:52:07 +01:00
try
{
_AppsHelper.Parse(vm.PerksTemplate, vm.TargetCurrency);
}
catch
{
ModelState.AddModelError(nameof(vm.PerksTemplate), "Invalid template");
}
if (!ModelState.IsValid)
{
return View(vm);
}
2018-12-11 16:36:25 +01:00
var app = await GetOwnedApp(appId, AppType.Crowdfund);
if (app == null)
return NotFound();
app.SetSettings(new CrowdfundSettings()
{
Title = vm.Title,
Enabled = vm.Enabled,
EnforceTargetAmount = vm.EnforceTargetAmount,
2018-12-28 17:38:20 +01:00
StartDate = vm.StartDate,
2018-12-11 16:36:25 +01:00
TargetCurrency = vm.TargetCurrency,
Description = vm.Description,
2018-12-28 17:38:20 +01:00
EndDate = vm.EndDate,
2018-12-11 16:36:25 +01:00
TargetAmount = vm.TargetAmount,
2018-12-22 15:02:16 +01:00
CustomCSSLink = vm.CustomCSSLink,
MainImageUrl = vm.MainImageUrl,
2018-12-22 15:43:40 +01:00
EmbeddedCSS = vm.EmbeddedCSS,
2018-12-22 15:02:16 +01:00
NotificationUrl = vm.NotificationUrl,
2018-12-29 11:52:07 +01:00
Tagline = vm.Tagline,
PerksTemplate = vm.PerksTemplate
2018-12-11 16:36:25 +01:00
});
await UpdateAppSettings(app);
2018-12-28 17:38:20 +01:00
_EventAggregator.Publish(new CrowdfundAppUpdated()
{
AppId = appId
});
2018-12-11 16:36:25 +01:00
StatusMessage = "App updated";
return RedirectToAction(nameof(ListApps));
}
}
}