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); return request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase);
} }
public static RequestBaseUrl GetRequestBaseUrl(this HttpRequest request) => new (request);
public static string GetAbsoluteRoot(this HttpRequest request) public static string GetAbsoluteRoot(this HttpRequest request)
{ => request.GetRequestBaseUrl().ToString();
return string.Concat(
request.Scheme,
"://",
request.Host.ToUriComponent(),
request.PathBase.ToUriComponent());
}
public static Uri GetAbsoluteRootUri(this HttpRequest request) public static Uri GetAbsoluteRootUri(this HttpRequest request)
{ {
@@ -105,7 +101,7 @@ public static class HttpRequestExtensions
} }
/// <summary> /// <summary>
/// Will return an absolute URL. /// Will return an absolute URL.
/// If `relativeOrAbsolute` is absolute, returns it. /// If `relativeOrAbsolute` is absolute, returns it.
/// If `relativeOrAbsolute` is relative, send absolute url based on the HOST of this request (without PathBase) /// If `relativeOrAbsolute` is relative, send absolute url based on the HOST of this request (without PathBase)
/// </summary> /// </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.Contracts;
using BTCPayServer.Abstractions.Models; using BTCPayServer.Abstractions.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure; using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
@@ -22,6 +23,11 @@ namespace BTCPayServer.Data
var builder = new DbContextOptionsBuilder<ApplicationDbContext>(); var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseLoggerFactory(LoggerFactory); builder.UseLoggerFactory(LoggerFactory);
builder.AddInterceptors(MigrationInterceptor.Instance); builder.AddInterceptors(MigrationInterceptor.Instance);
builder.ConfigureWarnings(w =>
{
w.Log((CoreEventId.OptimisticConcurrencyException, LogLevel.Debug));
w.Log((CoreEventId.SaveChangesFailed, LogLevel.Debug));
});
ConfigureBuilder(builder, npgsqlOptionsAction); ConfigureBuilder(builder, npgsqlOptionsAction);
return new ApplicationDbContext(builder.Options); return new ApplicationDbContext(builder.Options);
} }

View File

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

View File

@@ -6,13 +6,13 @@ namespace BTCPayServer.Components.InvoiceStatus
{ {
public class InvoiceStatus : ViewComponent 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 var vm = new InvoiceStatusViewModel
{ {
State = state, State = state,
Payments = payments, Payments = payments ?? new(),
InvoiceId = invoiceId, InvoiceId = invoiceId ?? string.Empty,
IsArchived = isArchived, IsArchived = isArchived,
HasRefund = hasRefund HasRefund = hasRefund
}; };

View File

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

View File

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