44 lines
1.0 KiB
Rust
44 lines
1.0 KiB
Rust
use axum::{
|
|
extract::{Path, Query, State},
|
|
routing::get,
|
|
Json, Router,
|
|
};
|
|
|
|
use super::pagination::{self, PaginatedResponse};
|
|
use crate::db;
|
|
|
|
pub fn router() -> Router<crate::Global> {
|
|
Router::new()
|
|
.route("/", get(get_distros))
|
|
.route("/:id", get(get_single_distro))
|
|
}
|
|
|
|
async fn get_distros(
|
|
State(global): State<crate::Global>,
|
|
Query(pagination): Query<pagination::Query>,
|
|
) -> crate::Result<Json<PaginatedResponse<db::distro::Model>>> {
|
|
let (total_pages, repos) = global
|
|
.db
|
|
.distro
|
|
.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_distro(
|
|
State(global): State<crate::Global>,
|
|
Path(id): Path<i32>,
|
|
) -> crate::Result<Json<db::distro::Model>> {
|
|
let repo = global
|
|
.db
|
|
.distro
|
|
.by_id(id)
|
|
.await?
|
|
.ok_or(axum::http::StatusCode::NOT_FOUND)?;
|
|
|
|
Ok(Json(repo))
|
|
}
|