Refactoring Data entities to fit one standard convention

This commit is contained in:
rockstardev 2020-12-28 14:57:21 -06:00
parent 91cb9129ab
commit c9dc60be7b
28 changed files with 173 additions and 387 deletions

View file

@ -6,15 +6,13 @@ namespace BTCPayServer.Data
public class APIKeyData
{
[MaxLength(50)]
public string Id
{
get;
set;
}
public string Id { get; set; }
[MaxLength(50)] public string StoreId { get; set; }
[MaxLength(50)]
public string StoreId { get; set; }
[MaxLength(50)] public string UserId { get; set; }
[MaxLength(50)]
public string UserId { get; set; }
public APIKeyType Type { get; set; } = APIKeyType.Legacy;
@ -23,6 +21,7 @@ namespace BTCPayServer.Data
public ApplicationUser User { get; set; }
public string Label { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<APIKeyData>()

View file

@ -10,25 +10,11 @@ namespace BTCPayServer.Data
/// For not having exceptions thrown by two address on different network, we suffix by "#CRYPTOCODE"
/// </summary>
[Obsolete("Use GetHash instead")]
public string Address
{
get; set;
}
public string Address { get; set; }
public InvoiceData InvoiceData { get; set; }
public string InvoiceDataId { get; set; }
public DateTimeOffset? CreatedTime { get; set; }
public InvoiceData InvoiceData
{
get; set;
}
public string InvoiceDataId
{
get; set;
}
public DateTimeOffset? CreatedTime
{
get; set;
}
internal static void OnModelCreating(ModelBuilder builder)
{

View file

@ -8,22 +8,24 @@ namespace BTCPayServer.Data
{
public string Id { get; set; }
public string Name { get; set; }
public string StoreDataId
{
get; set;
}
public string StoreDataId { get; set; }
public string AppType { get; set; }
public StoreData StoreData
{
get; set;
}
public DateTimeOffset Created
{
get; set;
}
public StoreData StoreData { get; set; }
public DateTimeOffset Created { get; set; }
public bool TagAllInvoices { get; set; }
public string Settings { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<AppData>()
.HasOne(o => o.StoreData)
.WithMany(i => i.Apps).OnDelete(DeleteBehavior.Cascade);
builder.Entity<AppData>()
.HasOne(a => a.StoreData);
}
// utility methods
public T GetSettings<T>() where T : class, new()
{
if (String.IsNullOrEmpty(Settings))
@ -35,14 +37,5 @@ namespace BTCPayServer.Data
{
Settings = value == null ? null : JsonConvert.SerializeObject(value);
}
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<AppData>()
.HasOne(o => o.StoreData)
.WithMany(i => i.Apps).OnDelete(DeleteBehavior.Cascade);
builder.Entity<AppData>()
.HasOne(a => a.StoreData);
}
}
}

View file

@ -30,15 +30,8 @@ namespace BTCPayServer.Data
_designTime = designTime;
}
public DbSet<InvoiceData> Invoices
{
get; set;
}
public DbSet<RefundData> Refunds
{
get; set;
}
public DbSet<InvoiceData> Invoices { get; set; }
public DbSet<RefundData> Refunds { get; set; }
public DbSet<PlannedTransaction> PlannedTransactions { get; set; }
public DbSet<PayjoinLock> PayjoinLocks { get; set; }
public DbSet<AppData> Apps { get; set; }
@ -84,7 +77,6 @@ namespace BTCPayServer.Data
InvoiceData.OnModelCreating(builder);
InvoiceSearchData.OnModelCreating(builder);
PaymentData.OnModelCreating(builder);
Data.UserStore.OnModelCreating(builder);
APIKeyData.OnModelCreating(builder);
AppData.OnModelCreating(builder);
AddressInvoiceData.OnModelCreating(builder);

View file

@ -7,27 +7,14 @@ namespace BTCPayServer.Data
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
public List<NotificationData> Notifications { get; set; }
public List<UserStore> UserStores
{
get;
set;
}
public bool RequiresEmailConfirmation
{
get; set;
}
public List<StoredFile> StoredFiles
{
get;
set;
}
public bool RequiresEmailConfirmation { get; set; }
public List<StoredFile> StoredFiles { get; set; }
public List<U2FDevice> U2FDevices { get; set; }
public List<APIKeyData> APIKeys { get; set; }
public DateTimeOffset? Created { get; set; }
public string DisabledNotifications { get; set; }
public List<NotificationData> Notifications { get; set; }
public List<UserStore> UserStores { get; set; }
}
}

View file

@ -5,39 +5,23 @@ namespace BTCPayServer.Data
{
public class HistoricalAddressInvoiceData
{
public string InvoiceDataId
{
get; set;
}
public InvoiceData InvoiceData
{
get; set;
}
public string InvoiceDataId { get; set; }
public InvoiceData InvoiceData { get; set; }
/// <summary>
/// Some crypto currencies share same address prefix
/// For not having exceptions thrown by two address on different network, we suffix by "#CRYPTOCODE"
/// </summary>
[Obsolete("Use GetCryptoCode instead")]
public string Address
{
get; set;
}
public string Address { get; set; }
[Obsolete("Use GetCryptoCode instead")]
public string CryptoCode { get; set; }
public DateTimeOffset Assigned
{
get; set;
}
public DateTimeOffset Assigned { get; set; }
public DateTimeOffset? UnAssigned { get; set; }
public DateTimeOffset? UnAssigned
{
get; set;
}
internal static void OnModelCreating(ModelBuilder builder)
{

View file

@ -7,72 +7,24 @@ namespace BTCPayServer.Data
{
public class InvoiceData
{
public string StoreDataId
{
get; set;
}
public StoreData StoreData
{
get; set;
}
public string Id { get; set; }
public string Id
{
get; set;
}
public string StoreDataId { get; set; }
public StoreData StoreData { get; set; }
public DateTimeOffset Created
{
get; set;
}
public List<PaymentData> Payments
{
get; set;
}
public DateTimeOffset Created { get; set; }
public List<PaymentData> Payments { get; set; }
public List<InvoiceEventData> Events { get; set; }
public List<InvoiceEventData> Events
{
get; set;
}
public List<HistoricalAddressInvoiceData> HistoricalAddressInvoices { get; set; }
public List<HistoricalAddressInvoiceData> HistoricalAddressInvoices
{
get; set;
}
public byte[] Blob
{
get; set;
}
public string ItemCode
{
get;
set;
}
public string OrderId
{
get;
set;
}
public string Status
{
get;
set;
}
public string ExceptionStatus
{
get;
set;
}
public string CustomerEmail
{
get;
set;
}
public List<AddressInvoiceData> AddressInvoices
{
get; set;
}
public byte[] Blob { get; set; }
public string ItemCode { get; set; }
public string OrderId { get; set; }
public string Status { get; set; }
public string ExceptionStatus { get; set; }
public string CustomerEmail { get; set; }
public List<AddressInvoiceData> AddressInvoices { get; set; }
public bool Archived { get; set; }
public List<PendingInvoiceData> PendingInvoices { get; set; }
public List<InvoiceSearchData> InvoiceSearchData { get; set; }
@ -80,6 +32,8 @@ namespace BTCPayServer.Data
public string CurrentRefundId { get; set; }
[ForeignKey("Id,CurrentRefundId")]
public RefundData CurrentRefund { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<InvoiceData>()

View file

@ -5,23 +5,14 @@ namespace BTCPayServer.Data
{
public class InvoiceEventData
{
public string InvoiceDataId
{
get; set;
}
public InvoiceData InvoiceData
{
get; set;
}
public string InvoiceDataId { get; set; }
public InvoiceData InvoiceData { get; set; }
public string UniqueId { get; set; }
public DateTimeOffset Timestamp
{
get; set;
}
public DateTimeOffset Timestamp { get; set; }
public string Message { get; set; }
public EventSeverity Severity { get; set; } = EventSeverity.Info;
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<InvoiceEventData>()
@ -36,7 +27,7 @@ namespace BTCPayServer.Data
#pragma warning restore CS0618
});
}
public enum EventSeverity
{
Info,
@ -44,7 +35,7 @@ namespace BTCPayServer.Data
Success,
Warning
}
public string GetCssClass()
{
return Severity switch

View file

@ -1,7 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace BTCPayServer.Data

View file

@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace BTCPayServer.Data
@ -11,6 +8,8 @@ namespace BTCPayServer.Data
public InvoiceData Invoice { get; set; }
public string DeliveryId { get; set; }
public WebhookDeliveryData Delivery { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<InvoiceWebhookDeliveryData>()

View file

@ -19,6 +19,7 @@ namespace BTCPayServer.Data
public bool Seen { get; set; }
public byte[] Blob { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<NotificationData>()

View file

@ -5,32 +5,16 @@ namespace BTCPayServer.Data
{
public class PairedSINData
{
public string Id
{
get; set;
}
public string Id { get; set; }
public string StoreDataId
{
get; set;
}
public string StoreDataId { get; set; }
public StoreData StoreData { get; set; }
public string Label
{
get;
set;
}
public DateTimeOffset PairingTime
{
get;
set;
}
public string SIN
{
get; set;
}
public string Label { get; set; }
public DateTimeOffset PairingTime { get; set; }
public string SIN { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{

View file

@ -5,45 +5,17 @@ namespace BTCPayServer.Data
{
public class PairingCodeData
{
public string Id
{
get; set;
}
public string Id { get; set; }
[Obsolete("Unused")]
public string Facade
{
get; set;
}
public string StoreDataId
{
get; set;
}
public DateTimeOffset Expiration
{
get;
set;
}
public string Facade { get; set; }
public string StoreDataId { get; set; }
public DateTimeOffset Expiration { get; set; }
public string Label { get; set; }
public string SIN { get; set; }
public DateTime DateCreated { get; set; }
public string TokenValue { get; set; }
public string Label
{
get;
set;
}
public string SIN
{
get;
set;
}
public DateTime DateCreated
{
get;
set;
}
public string TokenValue
{
get;
set;
}
internal static void OnModelCreating(ModelBuilder builder)
{

View file

@ -4,28 +4,13 @@ namespace BTCPayServer.Data
{
public class PaymentData
{
public string Id
{
get; set;
}
public string Id { get; set; }
public string InvoiceDataId { get; set; }
public InvoiceData InvoiceData { get; set; }
public string InvoiceDataId
{
get; set;
}
public InvoiceData InvoiceData
{
get; set;
}
public byte[] Blob { get; set; }
public bool Accounted { get; set; }
public byte[] Blob
{
get; set;
}
public bool Accounted
{
get; set;
}
internal static void OnModelCreating(ModelBuilder builder)
{

View file

@ -6,10 +6,7 @@ namespace BTCPayServer.Data
public class PaymentRequestData
{
public string Id { get; set; }
public DateTimeOffset Created
{
get; set;
}
public DateTimeOffset Created { get; set; }
public string StoreDataId { get; set; }
public bool Archived { get; set; }
@ -19,6 +16,7 @@ namespace BTCPayServer.Data
public byte[] Blob { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<PaymentRequestData>()

View file

@ -21,18 +21,7 @@ namespace BTCPayServer.Data
public string Destination { get; set; }
public byte[] Blob { get; set; }
public byte[] Proof { get; set; }
public bool IsInPeriod(PullPaymentData pp, DateTimeOffset now)
{
var period = pp.GetPeriod(now);
if (period is { } p)
{
return p.Start <= Date && (p.End is DateTimeOffset end ? Date < end : true);
}
else
{
return false;
}
}
internal static void OnModelCreating(ModelBuilder builder)
{
@ -49,6 +38,20 @@ namespace BTCPayServer.Data
builder.Entity<PayoutData>()
.HasIndex(o => o.State);
}
// utility methods
public bool IsInPeriod(PullPaymentData pp, DateTimeOffset now)
{
var period = pp.GetPeriod(now);
if (period is { } p)
{
return p.Start <= Date && (p.End is DateTimeOffset end ? Date < end : true);
}
else
{
return false;
}
}
}
public enum PayoutState

View file

@ -4,10 +4,7 @@ namespace BTCPayServer.Data
{
public class PendingInvoiceData
{
public string Id
{
get; set;
}
public string Id { get; set; }
public InvoiceData InvoiceData { get; set; }
internal static void OnModelCreating(ModelBuilder builder)

View file

@ -7,8 +7,7 @@ namespace BTCPayServer.Data
{
[Key]
[MaxLength(100)]
// Id in the format [cryptocode]-[txid]
public string Id { get; set; }
public string Id { get; set; } // Id in the format [cryptocode]-[txid]
public DateTimeOffset BroadcastAt { get; set; }
public byte[] Blob { get; set; }
}

View file

@ -8,53 +8,6 @@ using NBitcoin;
namespace BTCPayServer.Data
{
public static class PayoutExtensions
{
public static IQueryable<PayoutData> GetPayoutInPeriod(this IQueryable<PayoutData> payouts, PullPaymentData pp)
{
return GetPayoutInPeriod(payouts, pp, DateTimeOffset.UtcNow);
}
public static IQueryable<PayoutData> GetPayoutInPeriod(this IQueryable<PayoutData> payouts, PullPaymentData pp, DateTimeOffset now)
{
var request = payouts.Where(p => p.PullPaymentDataId == pp.Id);
var period = pp.GetPeriod(now);
if (period is { } p)
{
var start = p.Start;
if (p.End is DateTimeOffset end)
{
return request.Where(p => p.Date >= start && p.Date < end);
}
else
{
return request.Where(p => p.Date >= start);
}
}
else
{
return request.Where(p => false);
}
}
public static string GetStateString(this PayoutState state)
{
switch (state)
{
case PayoutState.AwaitingApproval:
return "Awaiting Approval";
case PayoutState.AwaitingPayment:
return "Awaiting Payment";
case PayoutState.InProgress:
return "In Progress";
case PayoutState.Completed:
return "Completed";
case PayoutState.Cancelled:
return "Cancelled";
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
}
public class PullPaymentData
{
[Key]
@ -71,6 +24,16 @@ namespace BTCPayServer.Data
public List<PayoutData> Payouts { get; set; }
public byte[] Blob { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<PullPaymentData>()
.HasIndex(o => o.StoreId);
builder.Entity<PullPaymentData>()
.HasOne(o => o.StoreData)
.WithMany(o => o.PullPayments).OnDelete(DeleteBehavior.Cascade);
}
public (DateTimeOffset Start, DateTimeOffset? End)? GetPeriod(DateTimeOffset now)
{
if (now < StartDate)
@ -121,14 +84,54 @@ namespace BTCPayServer.Data
{
return !Archived && !IsExpired(now) && HasStarted(now);
}
}
internal static void OnModelCreating(ModelBuilder builder)
public static class PayoutExtensions
{
public static IQueryable<PayoutData> GetPayoutInPeriod(this IQueryable<PayoutData> payouts, PullPaymentData pp)
{
builder.Entity<PullPaymentData>()
.HasIndex(o => o.StoreId);
builder.Entity<PullPaymentData>()
.HasOne(o => o.StoreData)
.WithMany(o => o.PullPayments).OnDelete(DeleteBehavior.Cascade);
return GetPayoutInPeriod(payouts, pp, DateTimeOffset.UtcNow);
}
public static IQueryable<PayoutData> GetPayoutInPeriod(this IQueryable<PayoutData> payouts, PullPaymentData pp, DateTimeOffset now)
{
var request = payouts.Where(p => p.PullPaymentDataId == pp.Id);
var period = pp.GetPeriod(now);
if (period is { } p)
{
var start = p.Start;
if (p.End is DateTimeOffset end)
{
return request.Where(p => p.Date >= start && p.Date < end);
}
else
{
return request.Where(p => p.Date >= start);
}
}
else
{
return request.Where(p => false);
}
}
public static string GetStateString(this PayoutState state)
{
switch (state)
{
case PayoutState.AwaitingApproval:
return "Awaiting Approval";
case PayoutState.AwaitingPayment:
return "Awaiting Payment";
case PayoutState.InProgress:
return "In Progress";
case PayoutState.Completed:
return "Completed";
case PayoutState.Cancelled:
return "Cancelled";
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
}
}

View file

@ -13,6 +13,7 @@ namespace BTCPayServer.Data
public PullPaymentData PullPaymentData { get; set; }
public InvoiceData InvoiceData { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<RefundData>()

View file

@ -2,14 +2,8 @@ namespace BTCPayServer.Data
{
public class SettingData
{
public string Id
{
get; set;
}
public string Id { get; set; }
public string Value
{
get; set;
}
public string Value { get; set; }
}
}

View file

@ -5,7 +5,6 @@ using BTCPayServer.Client.Models;
namespace BTCPayServer.Data
{
public class StoreData
{
public string Id { get; set; }
@ -43,6 +42,4 @@ namespace BTCPayServer.Data
public List<PairedSINData> PairedSINs { get; set; }
public IEnumerable<APIKeyData> APIKeys { get; set; }
}
}

View file

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace BTCPayServer.Data
{
@ -13,6 +9,7 @@ namespace BTCPayServer.Data
public WebhookData Webhook { get; set; }
public StoreData Store { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<StoreWebhookData>()

View file

@ -12,9 +12,6 @@ namespace BTCPayServer.Data
public string StorageFileName { get; set; }
public DateTime Timestamp { get; set; }
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser
{
get; set;
}
public ApplicationUser ApplicationUser { get; set; }
}
}

View file

@ -4,28 +4,13 @@ namespace BTCPayServer.Data
{
public class UserStore
{
public string ApplicationUserId
{
get; set;
}
public ApplicationUser ApplicationUser
{
get; set;
}
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public string StoreDataId { get; set; }
public StoreData StoreData { get; set; }
public string Role { get; set; }
public string StoreDataId
{
get; set;
}
public StoreData StoreData
{
get; set;
}
public string Role
{
get;
set;
}
internal static void OnModelCreating(ModelBuilder builder)
{

View file

@ -1,6 +1,4 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace BTCPayServer.Data
{
@ -11,6 +9,8 @@ namespace BTCPayServer.Data
public string TransactionId { get; set; }
public string Labels { get; set; }
public byte[] Blob { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<WalletTransactionData>()

View file

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace BTCPayServer.Data
{
@ -10,11 +7,7 @@ namespace BTCPayServer.Data
{
[Key]
[MaxLength(25)]
public string Id
{
get;
set;
}
public string Id { get; set; }
[Required]
public byte[] Blob { get; set; }
public List<WebhookDeliveryData> Deliveries { get; set; }

View file

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace BTCPayServer.Data
@ -18,13 +15,12 @@ namespace BTCPayServer.Data
public WebhookData Webhook { get; set; }
[Required]
public DateTimeOffset Timestamp
{
get; set;
}
public DateTimeOffset Timestamp { get; set; }
[Required]
public byte[] Blob { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<WebhookDeliveryData>()