mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 05:54:26 +01:00
Minor refactoring (#6939)
This commit is contained in:
@@ -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>
|
||||
|
||||
39
BTCPayServer.Abstractions/RequestBaseUrl.cs
Normal file
39
BTCPayServer.Abstractions/RequestBaseUrl.cs
Normal 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());
|
||||
}
|
||||
21
BTCPayServer.Data/ApplicationDbContextExtensions.cs
Normal file
21
BTCPayServer.Data/ApplicationDbContextExtensions.cs
Normal 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();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user