Minor refactoring (#6939)

This commit is contained in:
Nicolas Dorier
2025-10-07 17:06:26 +09:00
committed by GitHub
parent 5e9dca19d5
commit 84c2caf2c8
8 changed files with 81 additions and 14 deletions

View File

@@ -12,14 +12,10 @@ public static class HttpRequestExtensions
return request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
}
public static RequestBaseUrl GetRequestBaseUrl(this HttpRequest request) => new (request);
public static string GetAbsoluteRoot(this HttpRequest request)
{
return string.Concat(
request.Scheme,
"://",
request.Host.ToUriComponent(),
request.PathBase.ToUriComponent());
}
=> request.GetRequestBaseUrl().ToString();
public static Uri GetAbsoluteRootUri(this HttpRequest request)
{
@@ -105,7 +101,7 @@ public static class HttpRequestExtensions
}
/// <summary>
/// Will return an absolute URL.
/// Will return an absolute URL.
/// If `relativeOrAbsolute` is absolute, returns it.
/// If `relativeOrAbsolute` is relative, send absolute url based on the HOST of this request (without PathBase)
/// </summary>

View File

@@ -0,0 +1,39 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;
namespace BTCPayServer.Abstractions;
public record RequestBaseUrl(string Scheme, HostString Host, PathString PathBase)
{
public static RequestBaseUrl FromUrl(Uri url)
=> new RequestBaseUrl(url.Scheme, new HostString(url.Authority), new PathString(url.AbsolutePath));
public static RequestBaseUrl FromUrl(string url)
{
if (TryFromUrl(url, out var result))
return result;
throw new FormatException("Invalid RequestBaseUrl");
}
public static bool TryFromUrl(string url, [MaybeNullWhen(false)] out RequestBaseUrl result)
{
ArgumentNullException.ThrowIfNull(url);
result = null;
if (!Uri.TryCreate(url, UriKind.Absolute, out var o))
return false;
result = FromUrl(o);
return true;
}
public RequestBaseUrl(HttpRequest request) : this(request.Scheme, request.Host, request.PathBase)
{
}
public override string ToString()
=> string.Concat(
Scheme,
"://",
Host.ToUriComponent(),
PathBase.ToUriComponent());
}

View File

@@ -0,0 +1,21 @@
#nullable enable
using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace BTCPayServer.Data;
public static partial class ApplicationDbContextExtensions
{
public static DbContext GetDbContext<T>(this DbSet<T> dbSet) where T : class
{
var infrastructure = dbSet as IInfrastructure<IServiceProvider>;
var serviceProvider = infrastructure.Instance;
var currentDbContext = (ICurrentDbContext)serviceProvider.GetService(typeof(ICurrentDbContext))!;
return currentDbContext.Context;
}
public static DbConnection GetDbConnection<T>(this DbSet<T> dbSet) where T : class
=> dbSet.GetDbContext().Database.GetDbConnection();
}

View File

@@ -2,6 +2,7 @@ using System;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
@@ -22,6 +23,11 @@ namespace BTCPayServer.Data
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseLoggerFactory(LoggerFactory);
builder.AddInterceptors(MigrationInterceptor.Instance);
builder.ConfigureWarnings(w =>
{
w.Log((CoreEventId.OptimisticConcurrencyException, LogLevel.Debug));
w.Log((CoreEventId.SaveChangesFailed, LogLevel.Debug));
});
ConfigureBuilder(builder, npgsqlOptionsAction);
return new ApplicationDbContext(builder.Options);
}

View File

@@ -120,6 +120,9 @@ namespace BTCPayServer.Services.Rates
return ni;
return null;
}
public NumberFormatInfo GetNumberFormatInfoOrDefault(string currency)
=> GetNumberFormatInfo(currency) ?? GetNumberFormatInfo("USD")!;
public IFormatProvider GetCurrencyProvider(string currency)
{

View File

@@ -6,13 +6,13 @@ namespace BTCPayServer.Components.InvoiceStatus
{
public class InvoiceStatus : ViewComponent
{
public IViewComponentResult Invoke(InvoiceState state, List<PaymentEntity> payments, string invoiceId, bool isArchived = false, bool hasRefund = false)
public IViewComponentResult Invoke(InvoiceState state, List<PaymentEntity> payments = null, string invoiceId = null, bool isArchived = false, bool hasRefund = false)
{
var vm = new InvoiceStatusViewModel
{
State = state,
Payments = payments,
InvoiceId = invoiceId,
Payments = payments ?? new(),
InvoiceId = invoiceId ?? string.Empty,
IsArchived = isArchived,
HasRefund = hasRefund
};

View File

@@ -104,7 +104,7 @@ namespace BTCPayServer.Components.MainNav
catch when (cts.IsCancellationRequested) { }
}
}
vm.DerivationSchemes = derivationSchemes;
vm.LightningNodes = lightningNodes;
@@ -116,12 +116,13 @@ namespace BTCPayServer.Components.MainNav
{
Id = a.Id,
AppName = a.AppName,
AppType = a.AppType
AppType = a.AppType,
Data = a.App
}).ToList();
vm.ArchivedAppsCount = apps.Count(a => a.Archived);
}
var user = await _userManager.GetUserAsync(HttpContext.User);
if (user != null)
{

View File

@@ -21,5 +21,6 @@ namespace BTCPayServer.Components.MainNav
public string Id { get; set; }
public string AppName { get; set; }
public string AppType { get; set; }
public AppData Data { get; set; }
}
}