From 6bacddc159bac3aaa6043d5019e3d2b042ddca40 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 19 Jul 2020 16:20:28 -0500 Subject: [PATCH 01/13] Refactoring ListInvoicePreferences --- .../Controllers/InvoiceController.UI.cs | 24 +++++++------------ .../Logic/ListInvoicesPreference.cs | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 572cc248a..b438405ab 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client; using BTCPayServer.Client.Models; +using BTCPayServer.Controllers.Logic; using BTCPayServer.Data; using BTCPayServer.Events; using BTCPayServer.Filters; @@ -598,17 +599,11 @@ namespace BTCPayServer.Controllers return Ok("{}"); } - public class InvoicePreference - { - public int? TimezoneOffset { get; set; } - public string SearchTerm { get; set; } - } - [HttpGet] [Route("invoices")] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] [BitpayAPIConstraint(false)] - public async Task ListInvoices(string searchTerm = null, int skip = 0, int count = 50, int? timezoneOffset = null) + public async Task ListInvoices(string searchTerm = null, int skip = 0, int count = 50, int timezoneOffset = 0) { // 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 @@ -618,20 +613,19 @@ namespace BTCPayServer.Controllers null; if (searchTerm is null) { - if (this.Request.Cookies.TryGetValue("ListInvoicePreferences", out var str)) + if (this.Request.Cookies.TryGetValue(ListInvoicesPreference.KEY, out var str)) { - var preferences = JsonConvert.DeserializeObject(str); + var preferences = JsonConvert.DeserializeObject(str); searchTerm = preferences.SearchTerm; - timezoneOffset = timezoneOffset is int v ? v : preferences.TimezoneOffset; + timezoneOffset = preferences.TimezoneOffset ?? 0; } } else { - var preferences = new InvoicePreference(); - preferences.SearchTerm = searchTerm; - preferences.TimezoneOffset = timezoneOffset; - this.Response.Cookies.Append("ListInvoicePreferences", JsonConvert.SerializeObject(preferences)); + this.Response.Cookies.Append(ListInvoicesPreference.KEY, + JsonConvert.SerializeObject(new ListInvoicesPreference(searchTerm, timezoneOffset))); } + var fs = new SearchString(searchTerm); var storeIds = fs.GetFilterArray("storeid") != null ? fs.GetFilterArray("storeid") : new List().ToArray(); @@ -643,7 +637,7 @@ namespace BTCPayServer.Controllers StoreIds = storeIds, TimezoneOffset = timezoneOffset }; - InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset ?? 0); + InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset); var counting = _InvoiceRepository.GetInvoicesTotal(invoiceQuery); invoiceQuery.Count = count; invoiceQuery.Skip = skip; diff --git a/BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs b/BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs new file mode 100644 index 000000000..620cc1182 --- /dev/null +++ b/BTCPayServer/Controllers/Logic/ListInvoicesPreference.cs @@ -0,0 +1,24 @@ +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; } + } +} From 9d9d0461ad21e05325c48f6c1c4a9ec62832d458 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 19 Jul 2020 16:40:57 -0500 Subject: [PATCH 02/13] 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) { From e3a8e1a187ab081c6378fc082c1b8a630c04d5d2 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 19 Jul 2020 16:49:13 -0500 Subject: [PATCH 03/13] Applying conventions from ListInvoice on PaymentRequests --- .../Controllers/PaymentRequestController.cs | 3 +- .../ListPaymentRequestsViewModel.cs | 6 +- .../PaymentRequest/GetPaymentRequests.cshtml | 103 ++++++++++-------- 3 files changed, 61 insertions(+), 51 deletions(-) diff --git a/BTCPayServer/Controllers/PaymentRequestController.cs b/BTCPayServer/Controllers/PaymentRequestController.cs index 4488285ec..ab887f7eb 100644 --- a/BTCPayServer/Controllers/PaymentRequestController.cs +++ b/BTCPayServer/Controllers/PaymentRequestController.cs @@ -76,10 +76,11 @@ namespace BTCPayServer.Controllers }); return View(new ListPaymentRequestsViewModel() { - IncludeArchived = includeArchived, Skip = skip, Count = count, Total = result.Total, + SearchTerm = searchTerm, + TimezoneOffset = timezoneOffset, Items = result.Items.Select(data => new ViewPaymentRequestViewModel(data)).ToList() }); } diff --git a/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs b/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs index 0abe7c75c..8f9cedc70 100644 --- a/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs +++ b/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs @@ -12,11 +12,11 @@ namespace BTCPayServer.Models.PaymentRequestViewModels { public int Skip { get; set; } public int Count { get; set; } - + public int Total { get; set; } + public string SearchTerm { get; set; } + public int? TimezoneOffset { get; set; } public List Items { get; set; } - public int Total { get; set; } - public bool IncludeArchived { get; set; } } public class UpdatePaymentRequestViewModel diff --git a/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml b/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml index 3c172550d..b14e96419 100644 --- a/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml +++ b/BTCPayServer/Views/PaymentRequest/GetPaymentRequests.cshtml @@ -10,7 +10,7 @@ {
- +
} @@ -18,10 +18,35 @@

Payment Requests


+

Create, search or pay an payment request.

+ +
+ +
+ + +
+ + + + +
+
+ +
+