78 lines
1.8 KiB
Rust
78 lines
1.8 KiB
Rust
mod pagination;
|
|
|
|
use axum::extract::{Path, Query, State};
|
|
use axum::routing::get;
|
|
use axum::Json;
|
|
use axum::Router;
|
|
|
|
use pagination::PaginatedResponse;
|
|
|
|
use crate::db;
|
|
|
|
pub fn router() -> Router<crate::Global> {
|
|
Router::new()
|
|
.route("/repos", get(get_repos))
|
|
.route("/repos/:id", get(get_single_repo))
|
|
.route("/packages", get(get_packages))
|
|
.route("/packages/:id", get(get_single_package))
|
|
}
|
|
|
|
async fn get_repos(
|
|
State(global): State<crate::Global>,
|
|
Query(pagination): Query<pagination::Query>,
|
|
) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> {
|
|
let (total_pages, repos) = global
|
|
.db
|
|
.repo
|
|
.page(
|
|
pagination.per_page.unwrap_or(25),
|
|
pagination.page.unwrap_or(1) - 1,
|
|
)
|
|
.await?;
|
|
Ok(Json(pagination.res(total_pages, repos)))
|
|
}
|
|
|
|
async fn get_single_repo(
|
|
State(global): State<crate::Global>,
|
|
Path(id): Path<i32>,
|
|
) -> crate::Result<Json<db::repo::Model>> {
|
|
let repo = global
|
|
.db
|
|
.repo
|
|
.by_id(id)
|
|
.await?
|
|
.ok_or(axum::http::StatusCode::NOT_FOUND)?;
|
|
|
|
Ok(Json(repo))
|
|
}
|
|
|
|
async fn get_packages(
|
|
State(global): State<crate::Global>,
|
|
Query(pagination): Query<pagination::Query>,
|
|
) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> {
|
|
let (total_pages, pkgs) = global
|
|
.db
|
|
.pkg
|
|
.page(
|
|
pagination.per_page.unwrap_or(25),
|
|
pagination.page.unwrap_or(1) - 1,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(pagination.res(total_pages, pkgs)))
|
|
}
|
|
|
|
async fn get_single_package(
|
|
State(global): State<crate::Global>,
|
|
Path(id): Path<i32>,
|
|
) -> crate::Result<Json<crate::db::FullPackage>> {
|
|
let entry = global
|
|
.db
|
|
.pkg
|
|
.full(id)
|
|
.await?
|
|
.ok_or(axum::http::StatusCode::NOT_FOUND)?;
|
|
|
|
Ok(Json(entry))
|
|
}
|