make invoice repository able to query more extensively

This commit is contained in:
Kukks 2019-09-21 17:07:48 +02:00
parent 59839a3332
commit f3aa67e0f1
4 changed files with 28 additions and 24 deletions

View File

@ -41,7 +41,7 @@ namespace BTCPayServer.Controllers
{ {
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
{ {
InvoiceId = id, InvoiceId = new[] {id},
StoreId = new[] { HttpContext.GetStoreData().Id } StoreId = new[] { HttpContext.GetStoreData().Id }
})).FirstOrDefault(); })).FirstOrDefault();
if (invoice == null) if (invoice == null)

View File

@ -37,7 +37,7 @@ namespace BTCPayServer.Controllers
{ {
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
{ {
InvoiceId = invoiceId, InvoiceId = new[] {invoiceId},
UserId = GetUserId(), UserId = GetUserId(),
IncludeAddresses = true, IncludeAddresses = true,
IncludeEvents = true IncludeEvents = true
@ -582,7 +582,7 @@ namespace BTCPayServer.Controllers
{ {
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
{ {
InvoiceId = invoiceId, InvoiceId = new[] {invoiceId},
UserId = GetUserId() UserId = GetUserId()
})).FirstOrDefault(); })).FirstOrDefault();

View File

@ -149,7 +149,8 @@ namespace BTCPayServer.Payments.Bitcoin
foreach (var txCoin in evt.TransactionData.Transaction.Outputs.AsCoins() foreach (var txCoin in evt.TransactionData.Transaction.Outputs.AsCoins()
.Where(o => o.ScriptPubKey == output.ScriptPubKey)) .Where(o => o.ScriptPubKey == output.ScriptPubKey))
{ {
var invoice = await _InvoiceRepository.GetInvoiceFromScriptPubKey(output.ScriptPubKey, network.CryptoCode); var key = output.ScriptPubKey.Hash + "#" + network.CryptoCode;
var invoice = (await _InvoiceRepository.GetInvoicesFromAddresses(new [] {key})).FirstOrDefault();
if (invoice != null) if (invoice != null)
{ {
var paymentData = new BitcoinLikePaymentData(txCoin, evt.TransactionData.Transaction.RBF); var paymentData = new BitcoinLikePaymentData(txCoin, evt.TransactionData.Transaction.RBF);

View File

@ -1,6 +1,7 @@
using DBriize; using DBriize;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using NBitpayClient; using NBitpayClient;
@ -80,30 +81,31 @@ retry:
} }
} }
public async Task<InvoiceEntity> GetInvoiceFromScriptPubKey(Script scriptPubKey, string cryptoCode) public async Task<IEnumerable<InvoiceEntity>> GetInvoicesFromAddresses(string[] addresses)
{ {
using (var db = _ContextFactory.CreateContext()) using (var db = _ContextFactory.CreateContext())
{ {
var key = scriptPubKey.Hash.ToString() + "#" + cryptoCode; return (await db.AddressInvoices
var result = (await db.AddressInvoices
#pragma warning disable CS0618 #pragma warning disable CS0618
.Where(a => a.Address == key) .Where(a => addresses.Contains(a.Address))
#pragma warning restore CS0618 #pragma warning restore CS0618
.Select(a => a.InvoiceData) .Select(a => a.InvoiceData)
.Include(a => a.Payments) .Include(a => a.Payments)
.Include(a => a.RefundAddresses) .Include(a => a.RefundAddresses)
.ToListAsync()).FirstOrDefault(); .ToListAsync()).Select(ToEntity);
if (result == null)
return null;
return ToEntity(result);
} }
} }
public async Task<string[]> GetPendingInvoices() public async Task<string[]> GetPendingInvoices(Func<IQueryable<PendingInvoiceData>, IQueryable<PendingInvoiceData>> filter = null )
{ {
using (var ctx = _ContextFactory.CreateContext()) using (var ctx = _ContextFactory.CreateContext())
{ {
return await ctx.PendingInvoices.Select(p => p.Id).ToArrayAsync(); var queryable = ctx.PendingInvoices.AsQueryable();
if (filter != null)
{
queryable = filter.Invoke(queryable);
}
return await queryable.Select(p => p.Id).ToArrayAsync();
} }
} }
@ -239,7 +241,7 @@ retry:
return paymentMethod.GetPaymentMethodDetails().GetPaymentDestination(); return paymentMethod.GetPaymentMethodDetails().GetPaymentDestination();
} }
public async Task<bool> NewAddress(string invoiceId, Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod paymentMethod, BTCPayNetworkBase network) public async Task<bool> NewAddress(string invoiceId, IPaymentMethodDetails paymentMethod, BTCPayNetworkBase network)
{ {
using (var context = _ContextFactory.CreateContext()) using (var context = _ContextFactory.CreateContext())
{ {
@ -252,7 +254,7 @@ retry:
if (currencyData == null) if (currencyData == null)
return false; return false;
var existingPaymentMethod = (Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)currencyData.GetPaymentMethodDetails(); var existingPaymentMethod = currencyData.GetPaymentMethodDetails();
if (existingPaymentMethod.GetPaymentDestination() != null) if (existingPaymentMethod.GetPaymentDestination() != null)
{ {
MarkUnassigned(invoiceId, invoiceEntity, context, currencyData.GetId()); MarkUnassigned(invoiceId, invoiceEntity, context, currencyData.GetId());
@ -487,11 +489,12 @@ retry:
{ {
IQueryable<Data.InvoiceData> query = context.Invoices; IQueryable<Data.InvoiceData> query = context.Invoices;
if (!string.IsNullOrEmpty(queryObject.InvoiceId)) if (queryObject.InvoiceId != null && queryObject.InvoiceId.Length > 0)
{ {
query = query.Where(i => i.Id == queryObject.InvoiceId); var statusSet = queryObject.InvoiceId.ToHashSet();
query = query.Where(i => statusSet.Contains(i.Id));
} }
if (queryObject.StoreId != null && queryObject.StoreId.Length > 0) if (queryObject.StoreId != null && queryObject.StoreId.Length > 0)
{ {
var stores = queryObject.StoreId.ToHashSet(); var stores = queryObject.StoreId.ToHashSet();
@ -666,7 +669,7 @@ retry:
Network = network Network = network
}; };
entity.SetCryptoPaymentData(paymentData); entity.SetCryptoPaymentData(paymentData);
//TODO: abstract
if (paymentMethodDetails is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod && if (paymentMethodDetails is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod &&
bitcoinPaymentMethod.NetworkFeeMode == NetworkFeeMode.MultiplePaymentsOnly && bitcoinPaymentMethod.NetworkFeeMode == NetworkFeeMode.MultiplePaymentsOnly &&
bitcoinPaymentMethod.NextNetworkFee == Money.Zero) bitcoinPaymentMethod.NextNetworkFee == Money.Zero)
@ -811,7 +814,7 @@ retry:
get; set; get; set;
} }
public string InvoiceId public string[] InvoiceId
{ {
get; get;
set; set;