Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination for runes and fix size query #3602

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,9 @@ impl Index {
}

let more = entries.len() > page_size;

if more {
entries.pop();
}
Ok((entries, more))
}

Expand Down
5 changes: 4 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,8 @@ impl Server {
AcceptJson(accept_json): AcceptJson,
) -> ServerResult {
task::block_in_place(|| {
let (entries, more) = index.runes_paginated(50, page_index)?;
let (entries, more) = index.runes_paginated(usize::try_from(100).unwrap(), page_index)?;
let page_size = entries.len();

let prev = page_index.checked_sub(1);

Expand All @@ -736,6 +737,7 @@ impl Server {
more,
prev,
next,
page_size,
})
.into_response()
} else {
Expand All @@ -744,6 +746,7 @@ impl Server {
more,
prev,
next,
page_size,
}
.page(server_config)
.into_response()
Expand Down
13 changes: 9 additions & 4 deletions src/templates/runes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct RunesHtml {
pub more: bool,
pub prev: Option<usize>,
pub next: Option<usize>,
pub page_size: usize,
}

impl PageContent for RunesHtml {
Expand Down Expand Up @@ -35,6 +36,7 @@ mod tests {
more: false,
prev: None,
next: None,
page_size: 0,
}
.to_string(),
"<h1>Runes</h1>
Expand All @@ -44,7 +46,8 @@ mod tests {
<div class=center>
prev
next
</div>"
</div>
"
);
}

Expand Down Expand Up @@ -77,6 +80,7 @@ mod tests {
prev: Some(1),
next: Some(2),
more: true,
page_size: 0,
}
.to_string(),
"<h1>Runes</h1>
Expand All @@ -85,9 +89,10 @@ mod tests {
<li><a href=/rune/C>C</a></li>
</ul>
<div class=center>
<a class=prev href=/runes/1>prev</a>
<a class=next href=/runes/2>next</a>
</div>"
<a class=prev href=/runes/1?size=0>prev</a>
<a class=next href=/runes/2?size=0>next</a>
</div>
"
);
}
}
6 changes: 3 additions & 3 deletions templates/runes.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ <h1>Runes</h1>
</ul>
<div class=center>
%% if let Some(prev) = self.prev {
<a class=prev href=/runes/{{prev}}>prev</a>
<a class=prev href=/runes/{{prev}}?size={{self.page_size}}>prev</a>
%% } else {
prev
%% }
%% if let Some(next) = self.next {
<a class=next href=/runes/{{next}}>next</a>
<a class=next href=/runes/{{next}}?size={{self.page_size}}>next</a>
%% } else {
next
%% }
</div>
</div>
62 changes: 51 additions & 11 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ fn get_runes() {

let a = etch(&core, &ord, Rune(RUNE));
let b = etch(&core, &ord, Rune(RUNE + 1));
let c = etch(&core, &ord, Rune(RUNE + 2));

core.mine_blocks(1);

Expand Down Expand Up @@ -558,36 +557,76 @@ fn get_runes() {
}
);

let response = ord.json_request("/runes");
let response0 = ord.json_request("/runes/0");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response0.status(), StatusCode::OK);

let runes_json: api::Runes = serde_json::from_str(&response.text().unwrap()).unwrap();
let runes_json0: api::Runes = serde_json::from_str(&response0.text().unwrap()).unwrap();

assert_eq!(runes_json0.entries.len(), 2);

pretty_assert_eq!(
runes_json,
runes_json0,
api::Runes {
entries: vec![
(
RuneId { block: 24, tx: 1 },
RuneId { block: 17, tx: 1 },
RuneEntry {
block: c.id.block,
block: b.id.block,
burned: 0,
terms: None,
divisibility: 0,
etching: c.output.reveal,
etching: b.output.reveal,
mints: 0,
number: 2,
number: 1,
premine: 1000,
spaced_rune: SpacedRune {
rune: Rune(RUNE + 2),
rune: Rune(RUNE + 1),
spacers: 0
},
symbol: Some('¢'),
timestamp: 24,
timestamp: 17,
turbo: false,
}
),
(
RuneId { block: 10, tx: 1 },
RuneEntry {
block: a.id.block,
burned: 0,
terms: None,
divisibility: 0,
etching: a.output.reveal,
mints: 0,
number: 0,
premine: 1000,
spaced_rune: SpacedRune {
rune: Rune(RUNE),
spacers: 0
},
symbol: Some('¢'),
timestamp: 10,
turbo: false,
}
)
],
more: false,
next: None,
prev: None,
page_size: 2,
}
);

let response = ord.json_request("/runes");

assert_eq!(response.status(), StatusCode::OK);

let runes_json: api::Runes = serde_json::from_str(&response.text().unwrap()).unwrap();

pretty_assert_eq!(
runes_json,
api::Runes {
entries: vec![
(
RuneId { block: 17, tx: 1 },
RuneEntry {
Expand Down Expand Up @@ -632,6 +671,7 @@ fn get_runes() {
more: false,
next: None,
prev: None,
page_size: 2,
}
);
}
Expand Down