refactor(server): separate api pieces into modules
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline failed Details
ci/woodpecker/push/build Pipeline was successful Details

distro
Jef Roosens 2023-08-17 10:24:29 +02:00
parent 80fb6d22f8
commit b7be311485
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 136 additions and 101 deletions

View File

@ -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))
}

View File

@ -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())
}

View File

@ -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))
}

View File

@ -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))
}