Generalizing saving of search, applying it to PaymentRequests grid

This commit is contained in:
rockstardev 2020-07-19 16:40:57 -05:00
parent 6bacddc159
commit 9d9d0461ad
6 changed files with 60 additions and 49 deletions

View file

@ -1354,7 +1354,7 @@ namespace BTCPayServer.Tests
{ {
var result = var result =
(Models.InvoicingModels.InvoicesModel)((ViewResult)acc.GetController<InvoiceController>() (Models.InvoicingModels.InvoicesModel)((ViewResult)acc.GetController<InvoiceController>()
.ListInvoices(filter).Result).Model; .ListInvoices(searchTerm:filter).Result).Model;
Assert.Equal(expected, result.Invoices.Any(i => i.InvoiceId == invoiceId)); Assert.Equal(expected, result.Invoices.Any(i => i.InvoiceId == invoiceId));
} }

View file

@ -603,28 +603,9 @@ namespace BTCPayServer.Controllers
[Route("invoices")] [Route("invoices")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
[BitpayAPIConstraint(false)] [BitpayAPIConstraint(false)]
public async Task<IActionResult> ListInvoices(string searchTerm = null, int skip = 0, int count = 50, int timezoneOffset = 0) public async Task<IActionResult> ListInvoices(int skip = 0, int count = 50, string searchTerm = null, int? timezoneOffset = null)
{ {
// If the user enter an empty searchTerm, then the variable will be null and not empty string ListCookiePreference.Parse(this, "ListInvoicesPreference", ref searchTerm, ref timezoneOffset);
// but we want searchTerm to be null only if the user is browsing the page via some link
// NOT if the user entered some empty search
searchTerm = searchTerm is string ? searchTerm :
this.Request.Query.ContainsKey(nameof(searchTerm)) ? string.Empty :
null;
if (searchTerm is null)
{
if (this.Request.Cookies.TryGetValue(ListInvoicesPreference.KEY, out var str))
{
var preferences = JsonConvert.DeserializeObject<ListInvoicesPreference>(str);
searchTerm = preferences.SearchTerm;
timezoneOffset = preferences.TimezoneOffset ?? 0;
}
}
else
{
this.Response.Cookies.Append(ListInvoicesPreference.KEY,
JsonConvert.SerializeObject(new ListInvoicesPreference(searchTerm, timezoneOffset)));
}
var fs = new SearchString(searchTerm); var fs = new SearchString(searchTerm);
var storeIds = fs.GetFilterArray("storeid") != null ? fs.GetFilterArray("storeid") : new List<string>().ToArray(); var storeIds = fs.GetFilterArray("storeid") != null ? fs.GetFilterArray("storeid") : new List<string>().ToArray();
@ -637,7 +618,7 @@ namespace BTCPayServer.Controllers
StoreIds = storeIds, StoreIds = storeIds,
TimezoneOffset = timezoneOffset TimezoneOffset = timezoneOffset
}; };
InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset); InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset ?? 0);
var counting = _InvoiceRepository.GetInvoicesTotal(invoiceQuery); var counting = _InvoiceRepository.GetInvoicesTotal(invoiceQuery);
invoiceQuery.Count = count; invoiceQuery.Count = count;
invoiceQuery.Skip = skip; invoiceQuery.Skip = skip;

View file

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace BTCPayServer.Controllers.Logic
{
public class ListCookiePreference
{
public ListCookiePreference() { }
public ListCookiePreference(string searchTerm, int? timezoneOffset)
{
SearchTerm = searchTerm;
TimezoneOffset = timezoneOffset;
}
public int? TimezoneOffset { get; set; }
public string SearchTerm { get; set; }
public static void Parse(ControllerBase ctrl, string key,
ref string searchTerm, ref int? timezoneOffset)
{
// If the user enter an empty searchTerm, then the variable will be null and not empty string
// but we want searchTerm to be null only if the user is browsing the page via some link
// NOT if the user entered some empty search
searchTerm = searchTerm is string ? searchTerm :
ctrl.Request.Query.ContainsKey(nameof(searchTerm)) ? string.Empty :
null;
if (searchTerm is null)
{
if (ctrl.Request.Cookies.TryGetValue(key, out var str))
{
var preferences = JsonConvert.DeserializeObject<ListCookiePreference>(str);
searchTerm = preferences.SearchTerm;
timezoneOffset = preferences.TimezoneOffset ?? 0;
}
}
else
{
ctrl.Response.Cookies.Append(key,
JsonConvert.SerializeObject(new ListCookiePreference(searchTerm, timezoneOffset)));
}
}
}
}

View file

@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BTCPayServer.Controllers.Logic
{
public class ListInvoicesPreference
{
public const string KEY = "ListInvoicePreferences";
public ListInvoicesPreference() { }
public ListInvoicesPreference(string searchTerm, int timezoneOffset)
{
SearchTerm = searchTerm;
if (timezoneOffset != 0)
TimezoneOffset = timezoneOffset;
}
public int? TimezoneOffset { get; set; }
public string SearchTerm { get; set; }
}
}

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Controllers.Logic;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Events; using BTCPayServer.Events;
using BTCPayServer.Filters; using BTCPayServer.Filters;
@ -61,8 +62,11 @@ namespace BTCPayServer.Controllers
[HttpGet] [HttpGet]
[Route("")] [Route("")]
[BitpayAPIConstraint(false)] [BitpayAPIConstraint(false)]
public async Task<IActionResult> GetPaymentRequests(int skip = 0, int count = 50, bool includeArchived = false) public async Task<IActionResult> GetPaymentRequests(int skip = 0, int count = 50, string searchTerm = null, int? timezoneOffset = null)
{ {
ListCookiePreference.Parse(this, "ListPaymentRequestsPreference", ref searchTerm, ref timezoneOffset);
var includeArchived = new SearchString(searchTerm).GetFilterBool("includearchived") == true;
var result = await _PaymentRequestRepository.FindPaymentRequests(new PaymentRequestQuery() var result = await _PaymentRequestRepository.FindPaymentRequests(new PaymentRequestQuery()
{ {
UserId = GetUserId(), UserId = GetUserId(),

View file

@ -99,7 +99,7 @@
{ {
skip = Model.Skip, skip = Model.Skip,
count = Model.Count, count = Model.Count,
includeArchived = !Model.IncludeArchived searchTerm = "includearchived:"+ !Model.IncludeArchived
})"> })">
@if (Model.IncludeArchived) @if (Model.IncludeArchived)
{ {