-
Notifications
You must be signed in to change notification settings - Fork 312
Added pagination and search to History #2702
Conversation
Linked to stratisproject/StratisCore#100 and stratisproject/StratisCore#96 |
not sure if @codingupastorm and @rowandh can benefit from this? |
Thanks, not immediately, we had to make some changes to our history. Useful skeleton for future though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some improvements on the readability of those Linq statements.
@@ -375,7 +375,8 @@ public IActionResult GetHistory([FromQuery] WalletHistoryRequest request) | |||
{ | |||
var transactionItems = new List<TransactionItemModel>(); | |||
|
|||
List<FlatHistory> items = accountHistory.History.OrderByDescending(o => o.Transaction.CreationTime).Take(200).ToList(); | |||
List<FlatHistory> items = accountHistory.History.OrderByDescending(o => o.Transaction.CreationTime).ToList(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bokobza a bit more readable:
IEnumerable<FlatHistory> items = accountHistory.History.OrderByDescending(o => o.Transaction.CreationTime);
if (string.IsNullOrEmpty(request.SearchQuery))
items = items.Take(200);
Also, no reason to ToList()
unless the intention is to take a copy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
items = string.IsNullOrEmpty(request.SearchQuery) ? items.Take(200).ToList() : items;`
is as readable as
if (string.IsNullOrEmpty(request.SearchQuery))
items = items.Take(200);
I need ToList, the type I get from OrderByDescending
is IOrderedEnumerable<FlatHistory>
.
@@ -107,6 +107,13 @@ public class WalletHistoryRequest : RequestModel | |||
public string WalletName { get; set; } | |||
|
|||
public string AccountName { get; set; } | |||
|
|||
public int? Skip { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cant you just default this to 0 then you don't have to do a coalesce?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Skip that wouldn't be a problem, but then how do I interpret Take = 0
?
If Take is 0 I should normally return no items, or write some code that says that if it's 0 then it's means it's off.
To avoid all confusion, a nullable is the easiest way.
|
||
public int? Take { get; set; } | ||
|
||
[JsonProperty(PropertyName = "q")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call this query
rather?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q is kind of an API convention for search queries. I think it's ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the value included in the URL?
@@ -497,9 +498,16 @@ public IActionResult GetHistory([FromQuery] WalletHistoryRequest request) | |||
} | |||
} | |||
|
|||
// Sort and filter the history items. | |||
List<TransactionItemModel> itemsToInclude = transactionItems.OrderByDescending(t => t.Timestamp) | |||
.Where(x => string.IsNullOrEmpty(request.SearchQuery) || (x.Id.ToString() == request.SearchQuery || x.ToAddress == request.SearchQuery || x.Payments.Any(p => p.DestinationAddress == request.SearchQuery))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if SearchQuery is null or empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's the beauty of this statement!
The goal is to avoid evaluating x.Id.ToString() == request.SearchQuery
(for example) if SearchQuery is empty.
So:
if SearchQuery is null or empty then the evaluation stops as the statement returns true
.
if SearchQuery is not null or empty, then the evaluation moves on to the rest of the statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so you want to take all items if IsNullOrEmpty(request.SearchQuery)
abut if its not null then apply the query?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yeah thats what want so you apply the rest of the linq qeruey
skip
andtake
parametersExample query:
q
parameter, that can search by transaction id, receiving address or send addressExample query: