btcpayserver/BTCPayServer/Controllers/AppsController.Crowdfund.cs

171 lines
7.0 KiB
C#
Raw Normal View History

2018-12-11 16:36:25 +01:00
using System;
using System.Linq;
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 BTCPayServer.Services.Mails;
2018-12-11 16:36:25 +01:00
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
public partial class AppsController
{
public class AppUpdated
2018-12-28 17:38:20 +01:00
{
public string AppId { get; set; }
public object Settings { get; set; }
2019-01-05 19:47:39 +01:00
public string StoreId { get; set; }
public override string ToString()
{
return String.Empty;
}
2018-12-28 17:38:20 +01:00
}
2020-01-24 03:19:24 +01:00
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,
StoreId = app.StoreDataId,
2018-12-11 16:36:25 +01:00
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,
ResetEveryAmount = settings.ResetEveryAmount,
ResetEvery = Enum.GetName(typeof(CrowdfundResetEvery), settings.ResetEvery),
UseAllStoreInvoices = app.TagAllInvoices,
2019-01-09 12:22:36 +01:00
AppId = appId,
2019-02-19 05:04:58 +01:00
SearchTerm = app.TagAllInvoices ? $"storeid:{app.StoreDataId}" : $"orderid:{AppService.GetCrowdfundOrderId(appId)}",
2019-01-09 12:22:36 +01:00
DisplayPerksRanking = settings.DisplayPerksRanking,
SortPerksByPopularity = settings.SortPerksByPopularity,
2020-01-24 03:19:24 +01:00
Sounds = string.Join(Environment.NewLine, settings.Sounds),
AnimationColors = string.Join(Environment.NewLine, settings.AnimationColors)
2018-12-11 16:36:25 +01:00
};
return View(vm);
}
[HttpPost]
[Route("{appId}/settings/crowdfund")]
public async Task<IActionResult> UpdateCrowdfund(string appId, UpdateCrowdfundViewModel vm, string command)
2018-12-11 16:36:25 +01:00
{
2020-01-24 03:19:24 +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");
2020-01-24 03:19:24 +01:00
2018-12-29 11:52:07 +01:00
try
{
2019-02-19 05:04:58 +01:00
_AppService.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.");
}
var parsedSounds = vm.Sounds.Split(
2020-01-24 03:19:24 +01:00
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
).Select(s => s.Trim()).ToArray();
if (vm.SoundsEnabled && !parsedSounds.Any())
{
ModelState.AddModelError(nameof(vm.Sounds), "You must have at least one sound if you enable sounds");
}
2020-01-24 03:19:24 +01:00
var parsedAnimationColors = vm.AnimationColors.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
).Select(s => s.Trim()).ToArray();
if (vm.AnimationsEnabled && !parsedAnimationColors.Any())
{
ModelState.AddModelError(nameof(vm.AnimationColors), "You must have at least one animation color if you enable animations");
}
2020-01-24 03:19:24 +01:00
2018-12-29 11:52:07 +01:00
if (!ModelState.IsValid)
{
return View(vm);
}
2020-01-24 03:19:24 +01:00
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,
StartDate = vm.StartDate?.ToUniversalTime(),
2018-12-11 16:36:25 +01:00
TargetCurrency = vm.TargetCurrency,
Description = vm.Description,
EndDate = vm.EndDate?.ToUniversalTime(),
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-09 12:22:36 +01:00
DisplayPerksRanking = vm.DisplayPerksRanking,
SortPerksByPopularity = vm.SortPerksByPopularity,
Sounds = parsedSounds,
AnimationColors = parsedAnimationColors
2019-01-05 19:47:39 +01:00
};
app.TagAllInvoices = vm.UseAllStoreInvoices;
2019-01-05 19:47:39 +01:00
app.SetSettings(newSettings);
await _AppService.UpdateOrCreateApp(app);
_EventAggregator.Publish(new AppUpdated()
2019-11-14 13:13:41 +01:00
{
AppId = appId,
StoreId = app.StoreDataId,
Settings = newSettings
});
TempData[WellKnownTempData.SuccessMessage] = "App updated";
return RedirectToAction(nameof(UpdateCrowdfund), new { appId });
2018-12-11 16:36:25 +01:00
}
}
}