45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
mod pagination;
|
|
|
|
use axum::extract::{Path, Query, State};
|
|
use axum::routing::get;
|
|
use axum::Json;
|
|
use axum::Router;
|
|
use sea_orm::entity::EntityTrait;
|
|
use sea_orm::query::QueryOrder;
|
|
use sea_orm::PaginatorTrait;
|
|
|
|
use pagination::PaginatedResponse;
|
|
|
|
use crate::db::entities::repo;
|
|
|
|
pub fn router() -> Router<crate::Global> {
|
|
Router::new()
|
|
.route("/repos", get(get_repos))
|
|
.route("/repos/:id", get(get_single_repo))
|
|
}
|
|
|
|
async fn get_repos(
|
|
State(global): State<crate::Global>,
|
|
Query(pagination): Query<pagination::Query>,
|
|
) -> crate::Result<Json<PaginatedResponse<repo::Model>>> {
|
|
let repos = repo::Entity::find()
|
|
.order_by_asc(repo::Column::Id)
|
|
.paginate(&global.db, pagination.per_page.unwrap_or(25))
|
|
.fetch_page(pagination.page.unwrap_or(1) - 1)
|
|
.await?;
|
|
|
|
Ok(Json(pagination.res(repos)))
|
|
}
|
|
|
|
async fn get_single_repo(
|
|
State(global): State<crate::Global>,
|
|
Path(id): Path<i32>,
|
|
) -> crate::Result<Json<repo::Model>> {
|
|
let repo = repo::Entity::find_by_id(id)
|
|
.one(&global.db)
|
|
.await?
|
|
.ok_or(axum::http::StatusCode::NOT_FOUND)?;
|
|
|
|
Ok(Json(repo))
|
|
}
|