Skip to content

Commit

Permalink
feat: add total item count for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed May 30, 2024
1 parent 0c23924 commit 583ab83
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/db/shop_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ pub async fn get_paginated_items(
.filter(|x| Item::is_passing_filters(x, filters))
.collect();

let total_item_count = filtered_list.len();

let curr_slice: Vec<Item> = filtered_list
.iter()
.skip(pagination.cursor as usize)
.take(pagination.page_size as usize)
.cloned()
.collect();

Ok((curr_slice.len() as u32, curr_slice))
Ok((total_item_count as u32, curr_slice))
}

/// Gets all the items from the DB.
Expand Down
11 changes: 10 additions & 1 deletion src/services/shop_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use utoipa::ToSchema;
pub struct ShopListingResponse {
results: Option<Vec<ResponseItem>>,
count: usize,
total: usize,
next: Option<String>,
}

Expand Down Expand Up @@ -68,12 +69,14 @@ pub async fn generate_random_shop_listing(
results: Some(result.into_iter().map(ResponseItem::from).collect()),
count: n_of_items,
next: None,
total: n_of_items,
}
}
Err(_) => ShopListingResponse {
results: None,
count: 0,
next: None,
total: 0,
},
}
}
Expand All @@ -95,16 +98,22 @@ fn convert_result_to_shop_response(
results: Some(item.into_iter().map(ResponseItem::from).collect()),
count: n_of_items,
next: if n_of_items >= pagination.page_size as usize {
Some(shop_next_url_calculator(field_filters, pagination, res.0))
Some(shop_next_url_calculator(
field_filters,
pagination,
n_of_items as u32,
))
} else {
None
},
total: res.0 as usize,
}
}
Err(_) => ShopListingResponse {
results: None,
count: 0,
next: None,
total: 0,
},
}
}

0 comments on commit 583ab83

Please sign in to comment.