From 9d9d0461ad21e05325c48f6c1c4a9ec62832d458 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 19 Jul 2020 16:40:57 -0500 Subject: [PATCH] Generalizing saving of search, applying it to PaymentRequests grid --- BTCPayServer.Tests/UnitTest1.cs | 2 +- .../Controllers/InvoiceController.UI.cs | 25 ++-------- .../Controllers/Logic/ListCookiePreference.cs | 50 +++++++++++++++++++ .../Logic/ListInvoicesPreference.cs | 24 --------- .../Controllers/PaymentRequestController.cs | 6 ++- .../PaymentRequest/GetPaymentRequests.cshtml | 2 +- 6 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 BTCPayServer/Controllers/Logic/ListCookiePreference.cs delete mode 100644 BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 6be6bdc52..7ff50622a 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -1354,7 +1354,7 @@ namespace BTCPayServer.Tests { var result = (Models.InvoicingModels.InvoicesModel)((ViewResult)acc.GetController() - .ListInvoices(filter).Result).Model; + .ListInvoices(searchTerm:filter).Result).Model; Assert.Equal(expected, result.Invoices.Any(i => i.InvoiceId == invoiceId)); } diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index b438405ab..69db96a19 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -603,28 +603,9 @@ namespace BTCPayServer.Controllers [Route("invoices")] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] [BitpayAPIConstraint(false)] - public async Task ListInvoices(string searchTerm = null, int skip = 0, int count = 50, int timezoneOffset = 0) + public async Task 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 - // 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(str); - searchTerm = preferences.SearchTerm; - timezoneOffset = preferences.TimezoneOffset ?? 0; - } - } - else - { - this.Response.Cookies.Append(ListInvoicesPreference.KEY, - JsonConvert.SerializeObject(new ListInvoicesPreference(searchTerm, timezoneOffset))); - } + ListCookiePreference.Parse(this, "ListInvoicesPreference", ref searchTerm, ref timezoneOffset); var fs = new SearchString(searchTerm); var storeIds = fs.GetFilterArray("storeid") != null ? fs.GetFilterArray("storeid") : new List().ToArray(); @@ -637,7 +618,7 @@ namespace BTCPayServer.Controllers StoreIds = storeIds, TimezoneOffset = timezoneOffset }; - InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset); + InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset ?? 0); var counting = _InvoiceRepository.GetInvoicesTotal(invoiceQuery); invoiceQuery.Count = count; invoiceQuery.Skip = skip; diff --git a/BTCPayServer/Controllers/Logic/ListCookiePreference.cs b/BTCPayServer/Controllers/Logic/ListCookiePreference.cs new file mode 100644 index 000000000..ab3ccac4b --- /dev/null +++ b/BTCPayServer/Controllers/Logic/ListCookiePreference.cs @@ -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(str); + searchTerm = preferences.SearchTerm; + timezoneOffset = preferences.TimezoneOffset ?? 0; + } + } + else + { + ctrl.Response.Cookies.Append(key, + JsonConvert.SerializeObject(new ListCookiePreference(searchTerm, timezoneOffset))); + } + } + } +} diff --git a/BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs b/BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs deleted file mode 100644 index 620cc1182..000000000 --- a/BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs +++ /dev/null @@ -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; } - } -} diff --git a/BTCPayServer/Controllers/PaymentRequestController.cs b/BTCPayServer/Controllers/PaymentRequestController.cs index 077d567c1..4488285ec 100644 --- a/BTCPayServer/Controllers/PaymentRequestController.cs +++ b/BTCPayServer/Controllers/PaymentRequestController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using BTCPayServer.Controllers.Logic; using BTCPayServer.Data; using BTCPayServer.Events; using BTCPayServer.Filters; @@ -61,8 +62,11 @@ namespace BTCPayServer.Controllers [HttpGet] [Route("")] [BitpayAPIConstraint(false)] - public async Task GetPaymentRequests(int skip = 0, int count = 50, bool includeArchived = false) + public async Task 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() { UserId = GetUserId(), diff --git a/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml b/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml index c189d5fee..3c172550d 100644 --- a/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml +++ b/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml @@ -99,7 +99,7 @@ { skip = Model.Skip, count = Model.Count, - includeArchived = !Model.IncludeArchived + searchTerm = "includearchived:"+ !Model.IncludeArchived })"> @if (Model.IncludeArchived) {