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);
|
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)
|
||||||
{
|
{
|
||||||
|
|||||||
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.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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -116,7 +116,8 @@ 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);
|
||||||
|
|||||||
@@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user