refactor(server): separate api pieces into modules
parent
80fb6d22f8
commit
b7be311485
|
@ -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<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))
|
||||
}
|
|
@ -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<crate::Global> {
|
||||
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<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))
|
||||
}
|
||||
|
||||
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))
|
||||
.nest("/distros", distros::router())
|
||||
.nest("/repos", repos::router())
|
||||
.nest("/packages", packages::router())
|
||||
}
|
||||
|
|
|
@ -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<crate::Global> {
|
||||
Router::new()
|
||||
.route("/", get(get_packages))
|
||||
.route("/:id", get(get_single_package))
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
|
@ -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<crate::Global> {
|
||||
Router::new()
|
||||
.route("/", get(get_repos))
|
||||
.route("/:id", get(get_single_repo))
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
Loading…
Reference in New Issue