From 421f6ae69be32fed1cef9a98c0626a893f976465 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 22 May 2024 09:42:42 +0200 Subject: [PATCH] chore: some more api code cleanup --- server/src/api/mod.rs | 35 +++++++---------------------------- server/src/api/pagination.rs | 21 ++++++++++++++------- server/src/cli.rs | 2 +- server/src/db/mod.rs | 1 - server/src/db/query/repo.rs | 8 +++++++- server/src/repo/mod.rs | 2 +- 6 files changed, 30 insertions(+), 39 deletions(-) diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 09b6c95..fd54cd8 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -1,7 +1,5 @@ mod pagination; -use sea_orm::{*}; - use axum::extract::{Path, Query, State}; use axum::routing::get; use axum::Json; @@ -9,7 +7,7 @@ use axum::Router; use pagination::PaginatedResponse; -use crate::db::{self, *}; +use crate::db; pub fn router() -> Router { Router::new() @@ -24,25 +22,10 @@ async fn get_repos( Query(pagination): Query, Query(filter): Query, ) -> crate::Result>> { - let page = pagination.page.unwrap_or(1) - 1; - let per_page = pagination.per_page.unwrap_or(25); + let (total_pages, items) = + db::query::repo::page(&global.db, pagination.per_page, pagination.page - 1, filter).await?; - let paginator = Repo::find() - .filter(filter) - .order_by_asc(package::Column::Id) - .paginate(&global.db, pagination.per_page.unwrap_or(25)); - let items = paginator - .fetch_page(pagination.page.unwrap_or(1) - 1) - .await?; - let total_pages = paginator.num_pages().await?; - - Ok(Json(PaginatedResponse { - page, - per_page, - total_pages, - count: items.len(), - items, - })) + Ok(Json(pagination.res(total_pages, items))) } async fn get_single_repo( @@ -61,13 +44,9 @@ async fn get_packages( Query(pagination): Query, Query(filter): Query, ) -> crate::Result>> { - let (total_pages, pkgs) = db::query::package::page( - &global.db, - pagination.per_page.unwrap_or(25), - pagination.page.unwrap_or(1) - 1, - filter, - ) - .await?; + let (total_pages, pkgs) = + db::query::package::page(&global.db, pagination.per_page, pagination.page - 1, filter) + .await?; Ok(Json(pagination.res(total_pages, pkgs))) } diff --git a/server/src/api/pagination.rs b/server/src/api/pagination.rs index db1ffa0..02e32dc 100644 --- a/server/src/api/pagination.rs +++ b/server/src/api/pagination.rs @@ -1,12 +1,19 @@ use serde::{Deserialize, Serialize}; -pub const DEFAULT_PAGE: u64 = 1; -pub const DEFAULT_PER_PAGE: u64 = 25; - #[derive(Deserialize)] +#[serde(default)] pub struct Query { - pub page: Option, - pub per_page: Option, + pub page: u64, + pub per_page: u64, +} + +impl Default for Query { + fn default() -> Self { + Query { + page: 1, + per_page: 25, + } + } } #[derive(Serialize)] @@ -28,8 +35,8 @@ impl Query { items: Vec, ) -> PaginatedResponse { PaginatedResponse { - page: self.page.unwrap_or(DEFAULT_PAGE), - per_page: self.per_page.unwrap_or(DEFAULT_PER_PAGE), + page: self.page, + per_page: self.per_page, total_pages, count: items.len(), items, diff --git a/server/src/cli.rs b/server/src/cli.rs index 1ae6de4..2976d1c 100644 --- a/server/src/cli.rs +++ b/server/src/cli.rs @@ -4,13 +4,13 @@ use crate::{Config, Global}; use axum::extract::FromRef; use axum::Router; use clap::Parser; +use sea_orm_migration::MigratorTrait; use std::io; use std::path::PathBuf; use std::sync::{Arc, RwLock}; use tower_http::trace::TraceLayer; use tracing::debug; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; -use sea_orm_migration::MigratorTrait; #[derive(Parser)] #[command(author, version, about, long_about = None)] diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index b29f3d3..0194079 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize}; pub use entities::{prelude::*, *}; pub use migrator::Migrator; - type Result = std::result::Result; #[derive(EnumIter, DeriveActiveEnum, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] diff --git a/server/src/db/query/repo.rs b/server/src/db/query/repo.rs index 34fbb81..399d227 100644 --- a/server/src/db/query/repo.rs +++ b/server/src/db/query/repo.rs @@ -13,8 +13,14 @@ impl IntoCondition for Filter { } } -pub async fn page(conn: &DbConn, per_page: u64, page: u64) -> Result<(u64, Vec)> { +pub async fn page( + conn: &DbConn, + per_page: u64, + page: u64, + filter: Filter, +) -> Result<(u64, Vec)> { let paginator = Repo::find() + .filter(filter) .order_by_asc(repo::Column::Id) .paginate(conn, per_page); let repos = paginator.fetch_page(page).await?; diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index 5bf00a6..7d3f7e4 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -5,7 +5,7 @@ pub use manager::RepoGroupManager; use std::path::PathBuf; -use axum::body::{Body}; +use axum::body::Body; use axum::extract::{Path, State}; use axum::http::Request; use axum::http::StatusCode;