64 lines
1.7 KiB
Rust
64 lines
1.7 KiB
Rust
mod pagination;
|
|
|
|
use crate::db;
|
|
use pagination::PaginatedResponse;
|
|
|
|
use axum::{
|
|
extract::{Path, Query, State},
|
|
routing::get,
|
|
Json, Router,
|
|
};
|
|
|
|
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>,
|
|
Query(filter): Query<db::query::repo::Filter>,
|
|
) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> {
|
|
let items =
|
|
db::query::repo::page(&global.db, pagination.per_page, pagination.page - 1, filter).await?;
|
|
|
|
Ok(Json(pagination.res(items)))
|
|
}
|
|
|
|
async fn get_single_repo(
|
|
State(global): State<crate::Global>,
|
|
Path(id): Path<i32>,
|
|
) -> crate::Result<Json<db::repo::Model>> {
|
|
let repo = db::query::repo::by_id(&global.db, 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>,
|
|
Query(filter): Query<db::query::package::Filter>,
|
|
) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> {
|
|
let items =
|
|
db::query::package::page(&global.db, pagination.per_page, pagination.page - 1, filter)
|
|
.await?;
|
|
|
|
Ok(Json(pagination.res(items)))
|
|
}
|
|
|
|
async fn get_single_package(
|
|
State(global): State<crate::Global>,
|
|
Path(id): Path<i32>,
|
|
) -> crate::Result<Json<crate::db::FullPackage>> {
|
|
let entry = db::query::package::full(&global.db, id)
|
|
.await?
|
|
.ok_or(axum::http::StatusCode::NOT_FOUND)?;
|
|
|
|
Ok(Json(entry))
|
|
}
|