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()
{
InvoiceId = id,
InvoiceId = new[] {id},
StoreId = new[] { HttpContext.GetStoreData().Id }
})).FirstOrDefault();
if (invoice == null)

View File

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

View File

@ -149,7 +149,8 @@ namespace BTCPayServer.Payments.Bitcoin
foreach (var txCoin in evt.TransactionData.Transaction.Outputs.AsCoins()
.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)
{
var paymentData = new BitcoinLikePaymentData(txCoin, evt.TransactionData.Transaction.RBF);

View File

@ -1,6 +1,7 @@
using DBriize;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
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())
{
var key = scriptPubKey.Hash.ToString() + "#" + cryptoCode;
var result = (await db.AddressInvoices
return (await db.AddressInvoices
#pragma warning disable CS0618
.Where(a => a.Address == key)
.Where(a => addresses.Contains(a.Address))
#pragma warning restore CS0618
.Select(a => a.InvoiceData)
.Include(a => a.Payments)
.Include(a => a.RefundAddresses)
.ToListAsync()).FirstOrDefault();
if (result == null)
return null;
return ToEntity(result);
.Select(a => a.InvoiceData)
.Include(a => a.Payments)
.Include(a => a.RefundAddresses)
.ToListAsync()).Select(ToEntity);
}
}
public async Task<string[]> GetPendingInvoices()
public async Task<string[]> GetPendingInvoices(Func<IQueryable<PendingInvoiceData>, IQueryable<PendingInvoiceData>> filter = null )
{
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();
}
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())
{
@ -252,7 +254,7 @@ retry:
if (currencyData == null)
return false;
var existingPaymentMethod = (Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)currencyData.GetPaymentMethodDetails();
var existingPaymentMethod = currencyData.GetPaymentMethodDetails();
if (existingPaymentMethod.GetPaymentDestination() != null)
{
MarkUnassigned(invoiceId, invoiceEntity, context, currencyData.GetId());
@ -487,11 +489,12 @@ retry:
{
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)
{
var stores = queryObject.StoreId.ToHashSet();
@ -666,7 +669,7 @@ retry:
Network = network
};
entity.SetCryptoPaymentData(paymentData);
//TODO: abstract
if (paymentMethodDetails is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod &&
bitcoinPaymentMethod.NetworkFeeMode == NetworkFeeMode.MultiplePaymentsOnly &&
bitcoinPaymentMethod.NextNetworkFee == Money.Zero)
@ -811,7 +814,7 @@ retry:
get; set;
}
public string InvoiceId
public string[] InvoiceId
{
get;
set;