fix: page counter increment
This commit is contained in:
parent
7e1903f807
commit
aba71115a7
1 changed files with 27 additions and 41 deletions
|
@ -81,6 +81,20 @@ pub struct Page {
|
||||||
pub page: u32,
|
pub page: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Page {
|
||||||
|
pub fn next(&self) -> u32 {
|
||||||
|
self.page + 2
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prev(&self) -> u32 {
|
||||||
|
if self.page == 0 {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
self.page
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct OptionalPage {
|
pub struct OptionalPage {
|
||||||
pub page: Option<u32>,
|
pub page: Option<u32>,
|
||||||
|
@ -89,8 +103,8 @@ pub struct OptionalPage {
|
||||||
impl From<OptionalPage> for Page {
|
impl From<OptionalPage> for Page {
|
||||||
fn from(o: OptionalPage) -> Self {
|
fn from(o: OptionalPage) -> Self {
|
||||||
match o.page {
|
match o.page {
|
||||||
Some(page) => Self { page: page + 1 },
|
Some(page) => Self { page: page - 1 },
|
||||||
None => Page { page: 2 },
|
None => Page { page: 0 },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,7 +118,8 @@ pub async fn home(
|
||||||
let q = q.into_inner();
|
let q = q.into_inner();
|
||||||
async fn _home(_ctx: &ArcCtx, db: &BoxDB, p: &Page) -> ServiceResult<Vec<db_core::Repository>> {
|
async fn _home(_ctx: &ArcCtx, db: &BoxDB, p: &Page) -> ServiceResult<Vec<db_core::Repository>> {
|
||||||
const LIMIT: u32 = 10;
|
const LIMIT: u32 = 10;
|
||||||
let responses = db.get_all_repositories(p.page, LIMIT).await?;
|
let offset = p.page * LIMIT;
|
||||||
|
let responses = db.get_all_repositories(offset, LIMIT).await?;
|
||||||
Ok(responses)
|
Ok(responses)
|
||||||
}
|
}
|
||||||
let q: Page = q.into();
|
let q: Page = q.into();
|
||||||
|
@ -114,12 +129,10 @@ pub async fn home(
|
||||||
PageError::new(HomePage::new(&ctx.settings, &x), e)
|
PageError::new(HomePage::new(&ctx.settings, &x), e)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let prev = if q.page == 2 { 1 } else { q.page - 1 };
|
|
||||||
|
|
||||||
let payload = HomePagePayload {
|
let payload = HomePagePayload {
|
||||||
repos,
|
repos,
|
||||||
next_page: PAGES.home_next(q.page),
|
next_page: PAGES.home_next(q.next()),
|
||||||
prev_page: PAGES.home_next(prev),
|
prev_page: PAGES.home_next(q.prev()),
|
||||||
};
|
};
|
||||||
let page = HomePage::page(&ctx.settings, &payload);
|
let page = HomePage::page(&ctx.settings, &payload);
|
||||||
|
|
||||||
|
@ -134,44 +147,17 @@ mod tests {
|
||||||
fn page_counter_increases() {
|
fn page_counter_increases() {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
let mut page = Page { page: 0 };
|
||||||
struct TestPage {
|
|
||||||
// input
|
|
||||||
current: u32,
|
|
||||||
expected_next: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestPage {
|
assert_eq!(page.next(), 2);
|
||||||
fn new(current: u32) -> Self {
|
assert_eq!(page.prev(), 1);
|
||||||
Self {
|
|
||||||
current,
|
|
||||||
expected_next: current + 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&TestPage> for OptionalPage {
|
page.page = 1;
|
||||||
fn from(p: &TestPage) -> Self {
|
assert_eq!(page.next(), 3);
|
||||||
Self {
|
assert_eq!(page.prev(), 1);
|
||||||
page: Some(p.current),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut res = Vec::with_capacity(100);
|
|
||||||
for i in 0..100 {
|
|
||||||
res.push(TestPage::new(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
let op = OptionalPage { page: None };
|
let op = OptionalPage { page: None };
|
||||||
let p: Page = op.into();
|
let p: Page = op.into();
|
||||||
assert_eq!(p.page, 2);
|
assert_eq!(p.page, 0);
|
||||||
|
|
||||||
for i in res.iter() {
|
|
||||||
let op: OptionalPage = i.into();
|
|
||||||
let p: Page = op.into();
|
|
||||||
println!("Checking test case {:?}", i);
|
|
||||||
assert_eq!(p.page, i.expected_next);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue