Create store: Add default currency and rate provider fields

This commit is contained in:
Dennis Reimann 2022-01-20 17:35:25 +01:00
parent 723a38da68
commit c2fc099439
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
8 changed files with 80 additions and 47 deletions

View file

@ -1,3 +1,5 @@
using System;
namespace BTCPayServer.Rating namespace BTCPayServer.Rating
{ {
public enum RateSource public enum RateSource
@ -25,5 +27,13 @@ namespace BTCPayServer.Rating
Url = url; Url = url;
Source = source; Source = source;
} }
public string DisplayName =>
Source switch
{
RateSource.Direct => Name,
RateSource.Coingecko => $"{Name} (via CoinGecko)",
_ => throw new NotSupportedException(Source.ToString())
};
} }
} }

View file

@ -104,7 +104,6 @@ namespace BTCPayServer.Controllers
private readonly EventAggregator _EventAggregator; private readonly EventAggregator _EventAggregator;
private readonly NBXplorerDashboard _Dashboard; private readonly NBXplorerDashboard _Dashboard;
private readonly IOptions<ExternalServicesOptions> _externalServiceOptions; private readonly IOptions<ExternalServicesOptions> _externalServiceOptions;
public string CreatedStoreId { get; set; }
[TempData] [TempData]
public bool StoreNotConfigured public bool StoreNotConfigured

View file

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Constants;
@ -5,12 +7,13 @@ using BTCPayServer.Client;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Models; using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels; using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Security; using BTCPayServer.Rating;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using ExchangeSharp;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace BTCPayServer.Controllers namespace BTCPayServer.Controllers
{ {
@ -20,20 +23,30 @@ namespace BTCPayServer.Controllers
{ {
private readonly StoreRepository _repo; private readonly StoreRepository _repo;
private readonly UserManager<ApplicationUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly RateFetcher _rateFactory;
public string CreatedStoreId { get; set; }
public UIUserStoresController( public UIUserStoresController(
UserManager<ApplicationUser> userManager, UserManager<ApplicationUser> userManager,
StoreRepository storeRepository) StoreRepository storeRepository,
RateFetcher rateFactory)
{ {
_repo = storeRepository; _repo = storeRepository;
_userManager = userManager; _userManager = userManager;
_rateFactory = rateFactory;
} }
[HttpGet("create")] [HttpGet("create")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
public IActionResult CreateStore() public IActionResult CreateStore()
{ {
return View(); var vm = new CreateStoreViewModel
{
DefaultCurrency = StoreBlob.StandardDefaultCurrency,
Exchanges = GetExchangesSelectList(CoinGeckoRateProvider.CoinGeckoName)
};
return View(vm);
} }
[HttpPost("create")] [HttpPost("create")]
@ -42,10 +55,13 @@ namespace BTCPayServer.Controllers
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
{ {
vm.Exchanges = GetExchangesSelectList(vm.PreferredExchange);
return View(vm); return View(vm);
} }
var store = await _repo.CreateStore(GetUserId(), vm.Name);
var store = await _repo.CreateStore(GetUserId(), vm.Name, vm.DefaultCurrency, vm.PreferredExchange);
CreatedStoreId = store.Id; CreatedStoreId = store.Id;
TempData[WellKnownTempData.SuccessMessage] = "Store successfully created"; TempData[WellKnownTempData.SuccessMessage] = "Store successfully created";
return RedirectToAction(nameof(UIStoresController.Dashboard), "UIStores", new return RedirectToAction(nameof(UIStoresController.Dashboard), "UIStores", new
{ {
@ -53,11 +69,6 @@ namespace BTCPayServer.Controllers
}); });
} }
public string CreatedStoreId
{
get; set;
}
[HttpGet("{storeId}/me/delete")] [HttpGet("{storeId}/me/delete")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
public IActionResult DeleteStore(string storeId) public IActionResult DeleteStore(string storeId)
@ -82,5 +93,14 @@ namespace BTCPayServer.Controllers
} }
private string GetUserId() => _userManager.GetUserId(User); private string GetUserId() => _userManager.GetUserId(User);
private SelectList GetExchangesSelectList(string selected) {
var exchanges = _rateFactory.RateProviderFactory
.GetSupportedExchanges()
.Where(r => !string.IsNullOrWhiteSpace(r.Name))
.OrderBy(s => s.Id, StringComparer.OrdinalIgnoreCase);
var chosen = exchanges.FirstOrDefault(f => f.Id == selected) ?? exchanges.First();
return new SelectList(exchanges, nameof(chosen.Id), nameof(chosen.Name), chosen.Id);
}
} }
} }

View file

@ -18,6 +18,8 @@ namespace BTCPayServer.Data
{ {
public class StoreBlob public class StoreBlob
{ {
public static string StandardDefaultCurrency = "USD";
public StoreBlob() public StoreBlob()
{ {
InvoiceExpiration = TimeSpan.FromMinutes(15); InvoiceExpiration = TimeSpan.FromMinutes(15);
@ -27,8 +29,7 @@ namespace BTCPayServer.Data
RecommendedFeeBlockTarget = 1; RecommendedFeeBlockTarget = 1;
PaymentMethodCriteria = new List<PaymentMethodCriteria>(); PaymentMethodCriteria = new List<PaymentMethodCriteria>();
} }
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public NetworkFeeMode NetworkFeeMode { get; set; } public NetworkFeeMode NetworkFeeMode { get; set; }
@ -45,7 +46,7 @@ namespace BTCPayServer.Data
{ {
get get
{ {
return string.IsNullOrEmpty(_DefaultCurrency) ? "USD" : _DefaultCurrency; return string.IsNullOrEmpty(_DefaultCurrency) ? StandardDefaultCurrency : _DefaultCurrency;
} }
set set
{ {

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace BTCPayServer.Models.StoreViewModels namespace BTCPayServer.Models.StoreViewModels
{ {
@ -7,9 +8,17 @@ namespace BTCPayServer.Models.StoreViewModels
[Required] [Required]
[MaxLength(50)] [MaxLength(50)]
[MinLength(1)] [MinLength(1)]
public string Name public string Name { get; set; }
{
get; set; [Required]
} [MaxLength(10)]
[Display(Name = "Default currency")]
public string DefaultCurrency { get; set; }
[Required]
[Display(Name = "Preferred Price Source")]
public string PreferredExchange { get; set; }
public SelectList Exchanges { get; set; }
} }
} }

View file

@ -16,27 +16,15 @@ namespace BTCPayServer.Models.StoreViewModels
public string Rule { get; set; } public string Rule { get; set; }
public bool Error { get; set; } public bool Error { get; set; }
} }
public void SetExchangeRates(IEnumerable<AvailableRateProvider> supportedList, string preferredExchange) public void SetExchangeRates(IEnumerable<AvailableRateProvider> supportedList, string preferredExchange)
{ {
var defaultStore = preferredExchange ?? CoinGeckoRateProvider.CoinGeckoName; var defaultStore = preferredExchange ?? CoinGeckoRateProvider.CoinGeckoName;
supportedList = supportedList.Select(a => new AvailableRateProvider(a.Id, a.SourceId, GetName(a), a.Url, a.Source)).ToArray(); supportedList = supportedList.Select(a => new AvailableRateProvider(a.Id, a.SourceId, a.DisplayName, a.Url, a.Source)).ToArray();
var chosen = supportedList.FirstOrDefault(f => f.Id == defaultStore) ?? supportedList.FirstOrDefault(); var chosen = supportedList.FirstOrDefault(f => f.Id == defaultStore) ?? supportedList.FirstOrDefault();
Exchanges = new SelectList(supportedList, nameof(chosen.Id), nameof(chosen.Name), chosen); Exchanges = new SelectList(supportedList, nameof(chosen.Id), nameof(chosen.Name), chosen);
PreferredExchange = chosen.Id; PreferredExchange = chosen?.Id;
RateSource = chosen.Url; RateSource = chosen?.Url;
}
private string GetName(AvailableRateProvider a)
{
switch (a.Source)
{
case Rating.RateSource.Direct:
return a.Name;
case Rating.RateSource.Coingecko:
return $"{a.Name} (via CoinGecko)";
default:
throw new NotSupportedException(a.Source.ToString());
}
} }
public List<TestResultViewModel> TestRateRules { get; set; } public List<TestResultViewModel> TestRateRules { get; set; }
@ -56,19 +44,11 @@ namespace BTCPayServer.Models.StoreViewModels
[Display(Name = "Add Exchange Rate Spread")] [Display(Name = "Add Exchange Rate Spread")]
[Range(0.0, 100.0)] [Range(0.0, 100.0)]
public double Spread public double Spread { get; set; }
{
get;
set;
}
[Display(Name = "Preferred Price Source")] [Display(Name = "Preferred Price Source")]
public string PreferredExchange { get; set; } public string PreferredExchange { get; set; }
public string RateSource public string RateSource { get; set; }
{
get;
set;
}
} }
} }

View file

@ -186,10 +186,14 @@ namespace BTCPayServer.Services.Stores
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
} }
public async Task<StoreData> CreateStore(string ownerId, string name) public async Task<StoreData> CreateStore(string ownerId, string name, string defaultCurrency, string preferredExchange)
{ {
var store = new StoreData() { StoreName = name }; var store = new StoreData { StoreName = name };
SetNewStoreHints(ref store); var blob = store.GetStoreBlob();
blob.DefaultCurrency = defaultCurrency;
blob.PreferredExchange = preferredExchange;
store.SetStoreBlob(blob);
await CreateStore(ownerId, store); await CreateStore(ownerId, store);
return store; return store;
} }

View file

@ -19,6 +19,16 @@
<input asp-for="Name" class="form-control" required /> <input asp-for="Name" class="form-control" required />
<span asp-validation-for="Name" class="text-danger"></span> <span asp-validation-for="Name" class="text-danger"></span>
</div> </div>
<div class="form-group">
<label asp-for="DefaultCurrency" class="form-label" data-required></label>
<input asp-for="DefaultCurrency" class="form-control" style="max-width:10ch;" />
<span asp-validation-for="DefaultCurrency" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PreferredExchange" class="form-label" data-required></label>
<select asp-for="PreferredExchange" asp-items="Model.Exchanges" class="form-select w-auto"></select>
<span asp-validation-for="PreferredExchange" class="text-danger"></span>
</div>
<div class="form-group mt-4"> <div class="form-group mt-4">
<input type="submit" value="Create" class="btn btn-primary" id="Create" /> <input type="submit" value="Create" class="btn btn-primary" id="Create" />
</div> </div>