feat(server): example of pagination
parent
37218536c5
commit
25627e166e
|
@ -1,19 +1,33 @@
|
|||
use axum::Router;
|
||||
use axum::extract::State;
|
||||
use axum::extract::{Query, State};
|
||||
use axum::routing::get;
|
||||
use axum::Json;
|
||||
use axum::Router;
|
||||
use sea_orm::entity::EntityTrait;
|
||||
use sea_orm::query::QueryOrder;
|
||||
use axum::Json;
|
||||
use sea_orm::PaginatorTrait;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::db::entities::repo;
|
||||
|
||||
pub fn router() -> Router<crate::Global> {
|
||||
Router::new()
|
||||
.route("/repos", get(get_repos))
|
||||
#[derive(Deserialize)]
|
||||
pub struct Pagination {
|
||||
page: Option<u64>,
|
||||
per_page: Option<u64>,
|
||||
}
|
||||
|
||||
async fn get_repos(State(global): State<crate::Global>) -> crate::Result<Json<Vec<repo::Model>>> {
|
||||
let repos = repo::Entity::find().order_by_asc(repo::Column::Id).all(&global.db).await?;
|
||||
pub fn router() -> Router<crate::Global> {
|
||||
Router::new().route("/repos", get(get_repos))
|
||||
}
|
||||
|
||||
async fn get_repos(
|
||||
State(global): State<crate::Global>,
|
||||
Query(params): Query<Pagination>,
|
||||
) -> crate::Result<Json<Vec<repo::Model>>> {
|
||||
let repos = repo::Entity::find()
|
||||
.order_by_asc(repo::Column::Id)
|
||||
.paginate(&global.db, params.per_page.unwrap_or(25))
|
||||
.fetch_page(params.page.unwrap_or(0))
|
||||
.await?;
|
||||
|
||||
Ok(Json(repos))
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ impl Cli {
|
|||
pub async fn run(&self) {
|
||||
self.init_tracing();
|
||||
|
||||
let db = crate::db::init("sqlite://test.db?mode=rwc").await.unwrap();
|
||||
let db = crate::db::init("sqlite://test.db").await.unwrap();
|
||||
|
||||
let config = Config {
|
||||
repo_dir: self.repo_dir.clone(),
|
||||
|
@ -56,7 +56,7 @@ impl Cli {
|
|||
let global = Global {
|
||||
config,
|
||||
repo_manager: Arc::new(RwLock::new(repo_manager)),
|
||||
db
|
||||
db,
|
||||
};
|
||||
|
||||
// build our application with a single route
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
mod migrator;
|
||||
pub mod entities;
|
||||
mod migrator;
|
||||
|
||||
use migrator::Migrator;
|
||||
use sea_orm::ConnectOptions;
|
||||
|
@ -11,7 +11,7 @@ pub async fn init<C: Into<ConnectOptions>>(
|
|||
) -> Result<sea_orm::DatabaseConnection, sea_orm::DbErr> {
|
||||
let db = Database::connect(opt).await?;
|
||||
|
||||
Migrator::refresh(&db).await?;
|
||||
Migrator::up(&db, None).await?;
|
||||
|
||||
Ok(db)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,9 @@ impl IntoResponse for ServerError {
|
|||
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
ServerError::Status(status) => status.into_response(),
|
||||
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => StatusCode::NOT_FOUND.into_response(),
|
||||
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => {
|
||||
StatusCode::NOT_FOUND.into_response()
|
||||
}
|
||||
ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
mod api;
|
||||
mod cli;
|
||||
mod db;
|
||||
mod error;
|
||||
mod repo;
|
||||
mod api;
|
||||
|
||||
use clap::Parser;
|
||||
pub use error::{Result, ServerError};
|
||||
use repo::RepoGroupManager;
|
||||
use sea_orm::DatabaseConnection;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use sea_orm::DatabaseConnection;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Config {
|
||||
|
@ -21,7 +21,7 @@ pub struct Config {
|
|||
pub struct Global {
|
||||
config: Config,
|
||||
repo_manager: Arc<RwLock<RepoGroupManager>>,
|
||||
db: DatabaseConnection
|
||||
db: DatabaseConnection,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
|
Loading…
Reference in New Issue