chore: some more api code cleanup

concurrent-repos
Jef Roosens 2024-05-22 09:42:42 +02:00
parent 45f1abade3
commit 421f6ae69b
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 30 additions and 39 deletions

View File

@ -1,7 +1,5 @@
mod pagination; mod pagination;
use sea_orm::{*};
use axum::extract::{Path, Query, State}; use axum::extract::{Path, Query, State};
use axum::routing::get; use axum::routing::get;
use axum::Json; use axum::Json;
@ -9,7 +7,7 @@ use axum::Router;
use pagination::PaginatedResponse; use pagination::PaginatedResponse;
use crate::db::{self, *}; use crate::db;
pub fn router() -> Router<crate::Global> { pub fn router() -> Router<crate::Global> {
Router::new() Router::new()
@ -24,25 +22,10 @@ async fn get_repos(
Query(pagination): Query<pagination::Query>, Query(pagination): Query<pagination::Query>,
Query(filter): Query<db::query::repo::Filter>, Query(filter): Query<db::query::repo::Filter>,
) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> { ) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> {
let page = pagination.page.unwrap_or(1) - 1; let (total_pages, items) =
let per_page = pagination.per_page.unwrap_or(25); db::query::repo::page(&global.db, pagination.per_page, pagination.page - 1, filter).await?;
let paginator = Repo::find() Ok(Json(pagination.res(total_pages, items)))
.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,
}))
} }
async fn get_single_repo( async fn get_single_repo(
@ -61,12 +44,8 @@ async fn get_packages(
Query(pagination): Query<pagination::Query>, Query(pagination): Query<pagination::Query>,
Query(filter): Query<db::query::package::Filter>, Query(filter): Query<db::query::package::Filter>,
) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> { ) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> {
let (total_pages, pkgs) = db::query::package::page( let (total_pages, pkgs) =
&global.db, db::query::package::page(&global.db, pagination.per_page, pagination.page - 1, filter)
pagination.per_page.unwrap_or(25),
pagination.page.unwrap_or(1) - 1,
filter,
)
.await?; .await?;
Ok(Json(pagination.res(total_pages, pkgs))) Ok(Json(pagination.res(total_pages, pkgs)))

View File

@ -1,12 +1,19 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub const DEFAULT_PAGE: u64 = 1;
pub const DEFAULT_PER_PAGE: u64 = 25;
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)]
pub struct Query { pub struct Query {
pub page: Option<u64>, pub page: u64,
pub per_page: Option<u64>, pub per_page: u64,
}
impl Default for Query {
fn default() -> Self {
Query {
page: 1,
per_page: 25,
}
}
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -28,8 +35,8 @@ impl Query {
items: Vec<T>, items: Vec<T>,
) -> PaginatedResponse<T> { ) -> PaginatedResponse<T> {
PaginatedResponse { PaginatedResponse {
page: self.page.unwrap_or(DEFAULT_PAGE), page: self.page,
per_page: self.per_page.unwrap_or(DEFAULT_PER_PAGE), per_page: self.per_page,
total_pages, total_pages,
count: items.len(), count: items.len(),
items, items,

View File

@ -4,13 +4,13 @@ use crate::{Config, Global};
use axum::extract::FromRef; use axum::extract::FromRef;
use axum::Router; use axum::Router;
use clap::Parser; use clap::Parser;
use sea_orm_migration::MigratorTrait;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::debug; use tracing::debug;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use sea_orm_migration::MigratorTrait;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]

View File

@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize};
pub use entities::{prelude::*, *}; pub use entities::{prelude::*, *};
pub use migrator::Migrator; pub use migrator::Migrator;
type Result<T> = std::result::Result<T, sea_orm::DbErr>; type Result<T> = std::result::Result<T, sea_orm::DbErr>;
#[derive(EnumIter, DeriveActiveEnum, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] #[derive(EnumIter, DeriveActiveEnum, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]

View File

@ -13,8 +13,14 @@ impl IntoCondition for Filter {
} }
} }
pub async fn page(conn: &DbConn, per_page: u64, page: u64) -> Result<(u64, Vec<repo::Model>)> { pub async fn page(
conn: &DbConn,
per_page: u64,
page: u64,
filter: Filter,
) -> Result<(u64, Vec<repo::Model>)> {
let paginator = Repo::find() let paginator = Repo::find()
.filter(filter)
.order_by_asc(repo::Column::Id) .order_by_asc(repo::Column::Id)
.paginate(conn, per_page); .paginate(conn, per_page);
let repos = paginator.fetch_page(page).await?; let repos = paginator.fetch_page(page).await?;

View File

@ -5,7 +5,7 @@ pub use manager::RepoGroupManager;
use std::path::PathBuf; use std::path::PathBuf;
use axum::body::{Body}; use axum::body::Body;
use axum::extract::{Path, State}; use axum::extract::{Path, State};
use axum::http::Request; use axum::http::Request;
use axum::http::StatusCode; use axum::http::StatusCode;