mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-21 14:04:12 +01:00
Create store: Add default currency and rate provider fields
This commit is contained in:
parent
723a38da68
commit
c2fc099439
8 changed files with 80 additions and 47 deletions
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
|
||||
namespace BTCPayServer.Rating
|
||||
{
|
||||
public enum RateSource
|
||||
|
@ -25,5 +27,13 @@ namespace BTCPayServer.Rating
|
|||
Url = url;
|
||||
Source = source;
|
||||
}
|
||||
|
||||
public string DisplayName =>
|
||||
Source switch
|
||||
{
|
||||
RateSource.Direct => Name,
|
||||
RateSource.Coingecko => $"{Name} (via CoinGecko)",
|
||||
_ => throw new NotSupportedException(Source.ToString())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,7 +104,6 @@ namespace BTCPayServer.Controllers
|
|||
private readonly EventAggregator _EventAggregator;
|
||||
private readonly NBXplorerDashboard _Dashboard;
|
||||
private readonly IOptions<ExternalServicesOptions> _externalServiceOptions;
|
||||
public string CreatedStoreId { get; set; }
|
||||
|
||||
[TempData]
|
||||
public bool StoreNotConfigured
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Abstractions.Constants;
|
||||
|
@ -5,12 +7,13 @@ using BTCPayServer.Client;
|
|||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Models;
|
||||
using BTCPayServer.Models.StoreViewModels;
|
||||
using BTCPayServer.Security;
|
||||
using BTCPayServer.Rating;
|
||||
using BTCPayServer.Services.Rates;
|
||||
using BTCPayServer.Services.Stores;
|
||||
using ExchangeSharp;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
namespace BTCPayServer.Controllers
|
||||
{
|
||||
|
@ -20,20 +23,30 @@ namespace BTCPayServer.Controllers
|
|||
{
|
||||
private readonly StoreRepository _repo;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly RateFetcher _rateFactory;
|
||||
public string CreatedStoreId { get; set; }
|
||||
|
||||
public UIUserStoresController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
StoreRepository storeRepository)
|
||||
StoreRepository storeRepository,
|
||||
RateFetcher rateFactory)
|
||||
{
|
||||
_repo = storeRepository;
|
||||
_userManager = userManager;
|
||||
_rateFactory = rateFactory;
|
||||
}
|
||||
|
||||
[HttpGet("create")]
|
||||
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
|
||||
public IActionResult CreateStore()
|
||||
{
|
||||
return View();
|
||||
var vm = new CreateStoreViewModel
|
||||
{
|
||||
DefaultCurrency = StoreBlob.StandardDefaultCurrency,
|
||||
Exchanges = GetExchangesSelectList(CoinGeckoRateProvider.CoinGeckoName)
|
||||
};
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
[HttpPost("create")]
|
||||
|
@ -42,10 +55,13 @@ namespace BTCPayServer.Controllers
|
|||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
vm.Exchanges = GetExchangesSelectList(vm.PreferredExchange);
|
||||
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;
|
||||
|
||||
TempData[WellKnownTempData.SuccessMessage] = "Store successfully created";
|
||||
return RedirectToAction(nameof(UIStoresController.Dashboard), "UIStores", new
|
||||
{
|
||||
|
@ -53,11 +69,6 @@ namespace BTCPayServer.Controllers
|
|||
});
|
||||
}
|
||||
|
||||
public string CreatedStoreId
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
[HttpGet("{storeId}/me/delete")]
|
||||
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
|
||||
public IActionResult DeleteStore(string storeId)
|
||||
|
@ -82,5 +93,14 @@ namespace BTCPayServer.Controllers
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ namespace BTCPayServer.Data
|
|||
{
|
||||
public class StoreBlob
|
||||
{
|
||||
public static string StandardDefaultCurrency = "USD";
|
||||
|
||||
public StoreBlob()
|
||||
{
|
||||
InvoiceExpiration = TimeSpan.FromMinutes(15);
|
||||
|
@ -27,8 +29,7 @@ namespace BTCPayServer.Data
|
|||
RecommendedFeeBlockTarget = 1;
|
||||
PaymentMethodCriteria = new List<PaymentMethodCriteria>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
|
||||
public NetworkFeeMode NetworkFeeMode { get; set; }
|
||||
|
||||
|
@ -45,7 +46,7 @@ namespace BTCPayServer.Data
|
|||
{
|
||||
get
|
||||
{
|
||||
return string.IsNullOrEmpty(_DefaultCurrency) ? "USD" : _DefaultCurrency;
|
||||
return string.IsNullOrEmpty(_DefaultCurrency) ? StandardDefaultCurrency : _DefaultCurrency;
|
||||
}
|
||||
set
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
namespace BTCPayServer.Models.StoreViewModels
|
||||
{
|
||||
|
@ -7,9 +8,17 @@ namespace BTCPayServer.Models.StoreViewModels
|
|||
[Required]
|
||||
[MaxLength(50)]
|
||||
[MinLength(1)]
|
||||
public string Name
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
public string Name { 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; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,27 +16,15 @@ namespace BTCPayServer.Models.StoreViewModels
|
|||
public string Rule { get; set; }
|
||||
public bool Error { get; set; }
|
||||
}
|
||||
|
||||
public void SetExchangeRates(IEnumerable<AvailableRateProvider> supportedList, string preferredExchange)
|
||||
{
|
||||
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();
|
||||
Exchanges = new SelectList(supportedList, nameof(chosen.Id), nameof(chosen.Name), chosen);
|
||||
PreferredExchange = chosen.Id;
|
||||
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());
|
||||
}
|
||||
PreferredExchange = chosen?.Id;
|
||||
RateSource = chosen?.Url;
|
||||
}
|
||||
|
||||
public List<TestResultViewModel> TestRateRules { get; set; }
|
||||
|
@ -56,19 +44,11 @@ namespace BTCPayServer.Models.StoreViewModels
|
|||
|
||||
[Display(Name = "Add Exchange Rate Spread")]
|
||||
[Range(0.0, 100.0)]
|
||||
public double Spread
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public double Spread { get; set; }
|
||||
|
||||
[Display(Name = "Preferred Price Source")]
|
||||
public string PreferredExchange { get; set; }
|
||||
|
||||
public string RateSource
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public string RateSource { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,10 +186,14 @@ namespace BTCPayServer.Services.Stores
|
|||
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 };
|
||||
SetNewStoreHints(ref store);
|
||||
var store = new StoreData { StoreName = name };
|
||||
var blob = store.GetStoreBlob();
|
||||
blob.DefaultCurrency = defaultCurrency;
|
||||
blob.PreferredExchange = preferredExchange;
|
||||
store.SetStoreBlob(blob);
|
||||
|
||||
await CreateStore(ownerId, store);
|
||||
return store;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,16 @@
|
|||
<input asp-for="Name" class="form-control" required />
|
||||
<span asp-validation-for="Name" class="text-danger"></span>
|
||||
</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">
|
||||
<input type="submit" value="Create" class="btn btn-primary" id="Create" />
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue