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

Remove old pagination system of editoast #8662

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions editoast/src/views/light_rolling_stocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ mod tests {
use crate::error::InternalError;
use crate::modelsv2::fixtures::create_fast_rolling_stock;
use crate::modelsv2::fixtures::create_rolling_stock_livery_fixture;
use crate::views::pagination::PaginatedResponse;
use crate::views::light_rolling_stocks::LightRollingStockWithLiveriesCountList;
use crate::views::test_app::TestAppBuilder;

fn is_sorted(data: &[i64]) -> bool {
Expand Down Expand Up @@ -313,12 +313,12 @@ mod tests {
.collect::<HashSet<_>>();

let request = app.get("/light_rolling_stock/");
let response: PaginatedResponse<LightRollingStockWithLiveries> =
let response: LightRollingStockWithLiveriesCountList =
app.fetch(request).assert_status(StatusCode::OK).json_into();
let count = response.count;
let count = response.stats.count;
let uri = format!("/light_rolling_stock/?page_size={count}");
let request = app.get(&uri);
let response: PaginatedResponse<LightRollingStockWithLiveries> =
let response: LightRollingStockWithLiveriesCountList =
app.fetch(request).assert_status(StatusCode::OK).json_into();

// Ensure that AT LEAST all the rolling stocks create above are returned, in order
Expand Down
70 changes: 0 additions & 70 deletions editoast/src/views/pagination.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use diesel::pg::Pg;
use diesel::query_builder::*;
use diesel::sql_types::BigInt;
use diesel::sql_types::Untyped;
use diesel::QueryResult;
use editoast_derive::EditoastError;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -127,45 +122,6 @@ pub trait PaginatedList: ListAndCount + 'static {

impl<T> PaginatedList for T where T: ListAndCount + 'static {}

/// Generates a specialized [PaginatedResponse], commented, annotated with `ToSchema`
///
/// We need to specialize manually PaginatedResponse with each
/// type we intend to use it with, otherwise utoipa will generate a $ref to T...
#[macro_export]
macro_rules! decl_paginated_response {
($name:ident, $item:ty) => {
$crate::decl_paginated_response! {pub(self) $name, $item}
};
($vis:vis $name:ident, $item:ty) => {
/// A paginated response
#[allow(unused)]
#[allow(clippy::needless_pub_self)]
#[derive(utoipa::ToSchema)]
$vis struct $name {
/// The total number of items
pub count: i64,
/// The previous page number
#[schema(required)]
pub previous: Option<i64>,
/// The next page number
#[schema(required)]
pub next: Option<i64>,
/// The list of results
#[schema(required)]
pub results: Vec<$item>,
}
};
}

/// A paginated response
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct PaginatedResponse<T> {
pub count: i64,
pub previous: Option<i64>,
pub next: Option<i64>,
pub results: Vec<T>,
}

#[derive(Debug, Clone, Copy, Deserialize, IntoParams)]
#[into_params(parameter_in = Query)]
pub struct PaginationQueryParam {
Expand Down Expand Up @@ -235,32 +191,6 @@ pub enum PaginationError {
},
}

#[derive(Debug, Clone, Copy, QueryId)]
pub struct Paginated<T> {
query: T,
page_size: i64,
offset: i64,
}

impl<T> QueryFragment<Pg> for Paginated<T>
where
T: QueryFragment<Pg>,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
out.push_sql("SELECT *, COUNT(*) OVER () FROM (");
self.query.walk_ast(out.reborrow())?;
out.push_sql(") as paged_query_with LIMIT ");
out.push_bind_param::<BigInt, _>(&self.page_size)?;
out.push_sql(" OFFSET ");
out.push_bind_param::<BigInt, _>(&self.offset)?;
Ok(())
}
}

impl<T: Query> Query for Paginated<T> {
type SqlType = Untyped;
}

#[cfg(test)]
mod pagination_stats_tests {
use super::PaginationStats;
Expand Down
Loading