btcpayserver/BTCPayServer/Controllers/AppsController.Crowdfund.cs

173 lines
7.3 KiB
C#
Raw Normal View History

2018-12-11 16:36:25 +01:00
using System;
2019-01-06 14:28:53 +01:00
using System.Text;
using System.Text.Encodings.Web;
2018-12-11 16:36:25 +01:00
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; }
2019-01-05 19:47:39 +01:00
public CrowdfundSettings Settings { get; set; }
public string StoreId { get; set; }
2018-12-28 17:38:20 +01:00
}
2018-12-11 16:36:25 +01:00
public class CrowdfundSettings
{
public string Title { get; set; }
public string Description { get; set; }
2019-01-04 16:42:35 +01:00
public bool Enabled { get; set; } = false;
2018-12-11 16:36:25 +01:00
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; }
2019-01-04 16:42:35 +01:00
public bool DisqusEnabled { get; set; }= false;
public bool SoundsEnabled { get; set; }= true;
2018-12-31 11:38:05 +01:00
public string DisqusShortname { get; set; }
2019-01-04 16:42:35 +01:00
public bool AnimationsEnabled { get; set; } = true;
public bool UseInvoiceAmount { get; set; } = true;
public int ResetEveryAmount { get; set; } = 1;
public CrowdfundResetEvery ResetEvery { get; set; } = CrowdfundResetEvery.Never;
2019-01-09 12:22:36 +01:00
public bool UseAllStoreInvoices { get; set; }
public bool DisplayPerksRanking { get; set; }
public bool SortPerksByPopularity { 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-31 11:38:05 +01:00
PerksTemplate = settings.PerksTemplate,
DisqusEnabled = settings.DisqusEnabled,
SoundsEnabled = settings.SoundsEnabled,
DisqusShortname = settings.DisqusShortname,
AnimationsEnabled = settings.AnimationsEnabled,
UseInvoiceAmount = settings.UseInvoiceAmount,
ResetEveryAmount = settings.ResetEveryAmount,
ResetEvery = Enum.GetName(typeof(CrowdfundResetEvery), settings.ResetEvery),
2019-01-05 10:17:52 +01:00
UseAllStoreInvoices = settings.UseAllStoreInvoices,
2019-01-09 12:22:36 +01:00
AppId = appId,
SearchTerm = settings.UseAllStoreInvoices ? $"storeid:{app.StoreDataId}" : $"orderid:{AppsHelper.GetCrowdfundOrderId(appId)}",
2019-01-09 12:22:36 +01:00
DisplayPerksRanking = settings.DisplayPerksRanking,
SortPerksByPopularity = settings.SortPerksByPopularity
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 (!string.IsNullOrEmpty( vm.TargetCurrency) && _currencies.GetCurrencyData(vm.TargetCurrency, false) == null)
2018-12-11 16:36:25 +01:00
ModelState.AddModelError(nameof(vm.TargetCurrency), "Invalid currency");
2018-12-29 11:52:07 +01:00
try
{
2019-01-07 14:25:35 +01:00
_AppsHelper.Parse(vm.PerksTemplate, vm.TargetCurrency).ToString();
2018-12-29 11:52:07 +01:00
}
catch
{
ModelState.AddModelError(nameof(vm.PerksTemplate), "Invalid template");
}
2019-01-04 13:46:44 +01:00
if (Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery) != CrowdfundResetEvery.Never && !vm.StartDate.HasValue)
{
ModelState.AddModelError(nameof(vm.StartDate), "A start date is needed when the goal resets every X amount of time.");
}
2019-01-06 14:28:53 +01:00
if (Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery) != CrowdfundResetEvery.Never && vm.ResetEveryAmount <= 0)
{
ModelState.AddModelError(nameof(vm.ResetEveryAmount), "You must reset the goal at a minimum of 1 ");
}
2019-01-10 14:43:47 +01:00
if (vm.DisplayPerksRanking && !vm.SortPerksByPopularity)
{
ModelState.AddModelError(nameof(vm.DisplayPerksRanking), "You must sort by popularity in order to display ranking.");
}
2019-01-04 13:46:44 +01:00
2018-12-29 11:52:07 +01:00
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();
2019-01-05 19:47:39 +01:00
var newSettings = new CrowdfundSettings()
2018-12-11 16:36:25 +01:00
{
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 = _htmlSanitizer.Sanitize( 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,
2018-12-31 11:38:05 +01:00
PerksTemplate = vm.PerksTemplate,
DisqusEnabled = vm.DisqusEnabled,
SoundsEnabled = vm.SoundsEnabled,
DisqusShortname = vm.DisqusShortname,
AnimationsEnabled = vm.AnimationsEnabled,
ResetEveryAmount = vm.ResetEveryAmount,
ResetEvery = Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery),
2019-01-04 12:58:29 +01:00
UseInvoiceAmount = vm.UseInvoiceAmount,
2019-01-09 12:22:36 +01:00
UseAllStoreInvoices = vm.UseAllStoreInvoices,
DisplayPerksRanking = vm.DisplayPerksRanking,
SortPerksByPopularity = vm.SortPerksByPopularity
2019-01-05 19:47:39 +01:00
};
app.SetSettings(newSettings);
2018-12-11 16:36:25 +01:00
await UpdateAppSettings(app);
2018-12-28 17:38:20 +01:00
_EventAggregator.Publish(new CrowdfundAppUpdated()
{
2019-01-05 19:47:39 +01:00
AppId = appId,
StoreId = app.StoreDataId,
Settings = newSettings
2018-12-28 17:38:20 +01:00
});
2018-12-11 16:36:25 +01:00
StatusMessage = "App updated";
2019-01-07 14:25:35 +01:00
return RedirectToAction(nameof(UpdateCrowdfund), new {appId});
2018-12-11 16:36:25 +01:00
}
}
}