Refactoring query extraction from cookie into extender methods

This commit is contained in:
rockstardev 2020-07-28 10:28:19 -05:00
parent 466a0c6049
commit 19ffd031ec
3 changed files with 36 additions and 61 deletions

View file

@ -8,7 +8,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Client; using BTCPayServer.Client;
using BTCPayServer.Client.Models; using BTCPayServer.Client.Models;
using BTCPayServer.Controllers.Logic;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Events; using BTCPayServer.Events;
using BTCPayServer.Filters; using BTCPayServer.Filters;
@ -30,7 +29,6 @@ using Microsoft.EntityFrameworkCore;
using NBitcoin; using NBitcoin;
using NBitpayClient; using NBitpayClient;
using NBXplorer; using NBXplorer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using StoreData = BTCPayServer.Data.StoreData; using StoreData = BTCPayServer.Data.StoreData;
@ -605,7 +603,7 @@ namespace BTCPayServer.Controllers
[BitpayAPIConstraint(false)] [BitpayAPIConstraint(false)]
public async Task<IActionResult> ListInvoices(int skip = 0, int count = 50, string searchTerm = null, int? timezoneOffset = null) public async Task<IActionResult> ListInvoices(int skip = 0, int count = 50, string searchTerm = null, int? timezoneOffset = null)
{ {
ListCookiePreference.Parse(this, UserPrefCookieKeys.InvoicesQuery, ref searchTerm, ref timezoneOffset); this.InvoicesQuery(ref searchTerm, ref 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();

View file

@ -3,7 +3,6 @@ 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;
@ -64,7 +63,7 @@ namespace BTCPayServer.Controllers
[BitpayAPIConstraint(false)] [BitpayAPIConstraint(false)]
public async Task<IActionResult> GetPaymentRequests(int skip = 0, int count = 50, string searchTerm = null, int? timezoneOffset = null) public async Task<IActionResult> GetPaymentRequests(int skip = 0, int count = 50, string searchTerm = null, int? timezoneOffset = null)
{ {
ListCookiePreference.Parse(this, UserPrefCookieKeys.PaymentRequestsQuery, ref searchTerm, ref timezoneOffset); this.PaymentRequestsQuery(ref searchTerm, ref timezoneOffset);
var includeArchived = new SearchString(searchTerm).GetFilterBool("includearchived") == true; var includeArchived = new SearchString(searchTerm).GetFilterBool("includearchived") == true;
var result = await _PaymentRequestRepository.FindPaymentRequests(new PaymentRequestQuery() var result = await _PaymentRequestRepository.FindPaymentRequests(new PaymentRequestQuery()

View file

@ -1,19 +1,28 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace BTCPayServer.Controllers.Logic namespace BTCPayServer
{ {
// Classes here remember users preferences on certain pages and store them in unified blob cookie "UserPreferCookie" public static class ControllerBaseExtensions
public class ListCookiePreference
{ {
public static void Parse(ControllerBase ctrl, UserPrefCookieKeys key, public static void InvoicesQuery(this ControllerBase ctrl, ref string searchTerm, ref int? timezoneOffset)
{
ListCookiePreference.Parse(ctrl, "InvoicesQuery", ref searchTerm, ref timezoneOffset);
}
public static void PaymentRequestsQuery(this ControllerBase ctrl, ref string searchTerm, ref int? timezoneOffset)
{
ListCookiePreference.Parse(ctrl, "PaymentRequestsQuery", ref searchTerm, ref timezoneOffset);
}
}
// Classes here remember users preferences on certain pages and store them in unified blob cookie "UserPreferCookie"
class ListCookiePreference
{
internal static void Parse(ControllerBase ctrl, string propName,
ref string searchTerm, ref int? timezoneOffset) ref string searchTerm, ref int? timezoneOffset)
{ {
var prop = typeof(UserPrefsCookie).GetProperty(propName);
var prefCookie = parsePrefCookie(ctrl); var prefCookie = parsePrefCookie(ctrl);
// If the user enter an empty searchTerm, then the variable will be null and not empty string // If the user enter an empty searchTerm, then the variable will be null and not empty string
@ -24,7 +33,7 @@ namespace BTCPayServer.Controllers.Logic
null; null;
if (searchTerm is null) if (searchTerm is null)
{ {
var section = prefCookie.GetSection(key); var section = prop.GetValue(prefCookie) as ListQueryDataHolder;
if (section != null && !String.IsNullOrEmpty(section.SearchTerm)) if (section != null && !String.IsNullOrEmpty(section.SearchTerm))
{ {
searchTerm = section.SearchTerm; searchTerm = section.SearchTerm;
@ -33,7 +42,7 @@ namespace BTCPayServer.Controllers.Logic
} }
else else
{ {
prefCookie.SetSection(key, new ListQueryDataHolder(searchTerm, timezoneOffset)); prop.SetValue(prefCookie, new ListQueryDataHolder(searchTerm, timezoneOffset));
ctrl.Response.Cookies.Append(nameof(UserPrefsCookie), JsonConvert.SerializeObject(prefCookie)); ctrl.Response.Cookies.Append(nameof(UserPrefsCookie), JsonConvert.SerializeObject(prefCookie));
} }
} }
@ -53,57 +62,26 @@ namespace BTCPayServer.Controllers.Logic
return prefCookie; return prefCookie;
} }
}
public enum UserPrefCookieKeys class UserPrefsCookie
{
InvoicesQuery, PaymentRequestsQuery
}
public class UserPrefsCookie
{
public ListQueryDataHolder InvoicesQuery { get; set; }
public ListQueryDataHolder PaymentRequestsQuery { get; set; }
internal ListQueryDataHolder GetSection(UserPrefCookieKeys key)
{ {
switch (key) public ListQueryDataHolder InvoicesQuery { get; set; }
public ListQueryDataHolder PaymentRequestsQuery { get; set; }
}
class ListQueryDataHolder
{
public ListQueryDataHolder() { }
public ListQueryDataHolder(string searchTerm, int? timezoneOffset)
{ {
case UserPrefCookieKeys.InvoicesQuery: SearchTerm = searchTerm;
return InvoicesQuery; TimezoneOffset = timezoneOffset;
case UserPrefCookieKeys.PaymentRequestsQuery:
return PaymentRequestsQuery;
} }
return null; public int? TimezoneOffset { get; set; }
public string SearchTerm { get; set; }
} }
internal void SetSection(UserPrefCookieKeys key, ListQueryDataHolder query)
{
switch (key)
{
case UserPrefCookieKeys.InvoicesQuery:
InvoicesQuery = query;
break;
case UserPrefCookieKeys.PaymentRequestsQuery:
PaymentRequestsQuery = query;
break;
}
}
}
public class ListQueryDataHolder
{
public ListQueryDataHolder() { }
public ListQueryDataHolder(string searchTerm, int? timezoneOffset)
{
SearchTerm = searchTerm;
TimezoneOffset = timezoneOffset;
}
public int? TimezoneOffset { get; set; }
public string SearchTerm { get; set; }
} }
} }