diff --git a/server/src/web/api/distros.rs b/server/src/web/api/distros.rs new file mode 100644 index 0000000..feb3679 --- /dev/null +++ b/server/src/web/api/distros.rs @@ -0,0 +1,43 @@ +use axum::{ + extract::{Path, Query, State}, + routing::get, + Json, Router, +}; + +use super::pagination::{self, PaginatedResponse}; +use crate::db; + +pub fn router() -> Router { + Router::new() + .route("/", get(get_distros)) + .route("/:id", get(get_single_distro)) +} + +async fn get_distros( + State(global): State, + Query(pagination): Query, +) -> crate::Result>> { + 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, + Path(id): Path, +) -> crate::Result> { + let repo = global + .db + .distro + .by_id(id) + .await? + .ok_or(axum::http::StatusCode::NOT_FOUND)?; + + Ok(Json(repo)) +} diff --git a/server/src/web/api/mod.rs b/server/src/web/api/mod.rs index 7557956..16940b4 100644 --- a/server/src/web/api/mod.rs +++ b/server/src/web/api/mod.rs @@ -1,108 +1,13 @@ +mod distros; +mod packages; mod pagination; +mod repos; -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 { Router::new() - .route("/distros", get(get_distros)) - .route("/distros/:id", get(get_single_distro)) - .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_distros( - State(global): State, - Query(pagination): Query, -) -> crate::Result>> { - 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, - Path(id): Path, -) -> crate::Result> { - let repo = global - .db - .distro - .by_id(id) - .await? - .ok_or(axum::http::StatusCode::NOT_FOUND)?; - - Ok(Json(repo)) -} - -async fn get_repos( - State(global): State, - Query(pagination): Query, -) -> crate::Result>> { - 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, - Path(id): Path, -) -> crate::Result> { - 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, - Query(pagination): Query, -) -> crate::Result>> { - 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, - Path(id): Path, -) -> crate::Result> { - let entry = global - .db - .pkg - .full(id) - .await? - .ok_or(axum::http::StatusCode::NOT_FOUND)?; - - Ok(Json(entry)) + .nest("/distros", distros::router()) + .nest("/repos", repos::router()) + .nest("/packages", packages::router()) } diff --git a/server/src/web/api/packages.rs b/server/src/web/api/packages.rs new file mode 100644 index 0000000..c0c9fc3 --- /dev/null +++ b/server/src/web/api/packages.rs @@ -0,0 +1,44 @@ +use axum::{ + extract::{Path, Query, State}, + routing::get, + Json, Router, +}; + +use super::pagination::{self, PaginatedResponse}; +use crate::db; + +pub fn router() -> Router { + Router::new() + .route("/", get(get_packages)) + .route("/:id", get(get_single_package)) +} + +async fn get_packages( + State(global): State, + Query(pagination): Query, +) -> crate::Result>> { + 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, + Path(id): Path, +) -> crate::Result> { + let entry = global + .db + .pkg + .full(id) + .await? + .ok_or(axum::http::StatusCode::NOT_FOUND)?; + + Ok(Json(entry)) +} diff --git a/server/src/web/api/repos.rs b/server/src/web/api/repos.rs new file mode 100644 index 0000000..601ccb9 --- /dev/null +++ b/server/src/web/api/repos.rs @@ -0,0 +1,43 @@ +use axum::{ + extract::{Path, Query, State}, + routing::get, + Json, Router, +}; + +use super::pagination::{self, PaginatedResponse}; +use crate::db; + +pub fn router() -> Router { + Router::new() + .route("/", get(get_repos)) + .route("/:id", get(get_single_repo)) +} + +async fn get_repos( + State(global): State, + Query(pagination): Query, +) -> crate::Result>> { + 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, + Path(id): Path, +) -> crate::Result> { + let repo = global + .db + .repo + .by_id(id) + .await? + .ok_or(axum::http::StatusCode::NOT_FOUND)?; + + Ok(Json(repo)) +}