chore: some more api code cleanup
parent
45f1abade3
commit
421f6ae69b
|
@ -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<crate::Global> {
|
||||
Router::new()
|
||||
|
@ -24,25 +22,10 @@ async fn get_repos(
|
|||
Query(pagination): Query<pagination::Query>,
|
||||
Query(filter): Query<db::query::repo::Filter>,
|
||||
) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> {
|
||||
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,12 +44,8 @@ async fn get_packages(
|
|||
Query(pagination): Query<pagination::Query>,
|
||||
Query(filter): Query<db::query::package::Filter>,
|
||||
) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> {
|
||||
let (total_pages, pkgs) = db::query::package::page(
|
||||
&global.db,
|
||||
pagination.per_page.unwrap_or(25),
|
||||
pagination.page.unwrap_or(1) - 1,
|
||||
filter,
|
||||
)
|
||||
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)))
|
||||
|
|
|
@ -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<u64>,
|
||||
pub per_page: Option<u64>,
|
||||
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<T>,
|
||||
) -> PaginatedResponse<T> {
|
||||
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,
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize};
|
|||
pub use entities::{prelude::*, *};
|
||||
pub use migrator::Migrator;
|
||||
|
||||
|
||||
type Result<T> = std::result::Result<T, sea_orm::DbErr>;
|
||||
|
||||
#[derive(EnumIter, DeriveActiveEnum, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
|
||||
|
|
|
@ -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()
|
||||
.filter(filter)
|
||||
.order_by_asc(repo::Column::Id)
|
||||
.paginate(conn, per_page);
|
||||
let repos = paginator.fetch_page(page).await?;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue