mirror of
https://github.com/YGGverse/btracker.git
synced 2026-03-31 17:15:31 +00:00
implement pagination totals info
This commit is contained in:
parent
3b95561c08
commit
a4ce8e29d5
5 changed files with 38 additions and 20 deletions
38
src/main.rs
38
src/main.rs
|
|
@ -36,6 +36,7 @@ fn index(
|
|||
storage: &State<Storage>,
|
||||
meta: &State<Meta>,
|
||||
) -> Result<Template, Custom<String>> {
|
||||
use plurify::Plurify;
|
||||
#[derive(Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
struct Row {
|
||||
|
|
@ -45,32 +46,38 @@ fn index(
|
|||
size: String,
|
||||
torrent: Torrent,
|
||||
}
|
||||
let rows = storage
|
||||
let (total, torrents) = storage
|
||||
.torrents(
|
||||
Some((Sort::Modified, Order::Asc)),
|
||||
page.map(|p| if p > 0 { p - 1 } else { p } * storage.default_limit),
|
||||
Some(storage.default_limit),
|
||||
)
|
||||
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
|
||||
.into_iter()
|
||||
.map(|torrent| Row {
|
||||
created: torrent
|
||||
.creation_date
|
||||
.map(|t| t.format(&meta.format_time).to_string()),
|
||||
indexed: torrent.time.format(&meta.format_time).to_string(),
|
||||
magnet: format::magnet(&torrent.info_hash, meta.trackers.as_ref()),
|
||||
size: format::bytes(torrent.size),
|
||||
torrent,
|
||||
})
|
||||
.collect::<Vec<Row>>();
|
||||
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?;
|
||||
Ok(Template::render(
|
||||
"index",
|
||||
context! {
|
||||
meta: meta.inner(),
|
||||
back: page.map(|p| uri!(index(if p > 2 { Some(p - 1) } else { None }))),
|
||||
next: if rows.len() < storage.default_limit { None }
|
||||
next: if page.unwrap_or(1) * storage.default_limit >= total { None }
|
||||
else { Some(uri!(index(Some(page.map_or(2, |p| p + 1))))) },
|
||||
rows
|
||||
rows: torrents
|
||||
.into_iter()
|
||||
.map(|torrent| Row {
|
||||
created: torrent
|
||||
.creation_date
|
||||
.map(|t| t.format(&meta.format_time).to_string()),
|
||||
indexed: torrent.time.format(&meta.format_time).to_string(),
|
||||
magnet: format::magnet(&torrent.info_hash, meta.trackers.as_ref()),
|
||||
size: format::bytes(torrent.size),
|
||||
torrent,
|
||||
})
|
||||
.collect::<Vec<Row>>(),
|
||||
pagination_totals: format!(
|
||||
"Page {} / {} ({total} {} total)",
|
||||
page.unwrap_or(1),
|
||||
(total as f64 / storage.default_limit as f64).ceil(),
|
||||
total.plurify(&["torrent", "torrents", "torrents"])
|
||||
)
|
||||
},
|
||||
))
|
||||
}
|
||||
|
|
@ -85,6 +92,7 @@ fn rss(feed: &State<Feed>, storage: &State<Storage>) -> Result<RawXml<String>, C
|
|||
Some(storage.default_limit),
|
||||
)
|
||||
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
|
||||
.1
|
||||
{
|
||||
feed.push(&mut b, torrent)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,9 +76,10 @@ impl Storage {
|
|||
sort_order: Option<(Sort, Order)>,
|
||||
start: Option<usize>,
|
||||
limit: Option<usize>,
|
||||
) -> Result<Vec<Torrent>, String> {
|
||||
) -> Result<(usize, Vec<Torrent>), String> {
|
||||
let f = self.files(sort_order)?;
|
||||
let l = limit.unwrap_or(f.len());
|
||||
let t = f.len();
|
||||
let l = limit.unwrap_or(t);
|
||||
let mut b = Vec::with_capacity(l);
|
||||
for file in f.into_iter().skip(start.unwrap_or_default()).take(l) {
|
||||
if file
|
||||
|
|
@ -163,7 +164,7 @@ impl Storage {
|
|||
.into(),
|
||||
})
|
||||
}
|
||||
Ok(b)
|
||||
Ok((t, b))
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue