From 6bacddc159bac3aaa6043d5019e3d2b042ddca40 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 19 Jul 2020 16:20:28 -0500 Subject: [PATCH] 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; } + } +}