feat(server): add read-only distro api routes

distro
Jef Roosens 2023-08-17 09:50:17 +02:00
parent 0565328ea8
commit 1b80bcd757
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
9 changed files with 131 additions and 0 deletions

View File

@ -11,12 +11,43 @@ use crate::db;
pub fn router() -> Router<crate::Global> { pub fn router() -> Router<crate::Global> {
Router::new() Router::new()
.route("/distros", get(get_distros))
.route("/distros/:id", get(get_single_distro))
.route("/repos", get(get_repos)) .route("/repos", get(get_repos))
.route("/repos/:id", get(get_single_repo)) .route("/repos/:id", get(get_single_repo))
.route("/packages", get(get_packages)) .route("/packages", get(get_packages))
.route("/packages/:id", get(get_single_package)) .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( async fn get_repos(
State(global): State<crate::Global>, State(global): State<crate::Global>,
Query(pagination): Query<pagination::Query>, Query(pagination): Query<pagination::Query>,

View File

@ -0,0 +1,31 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "distro")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(unique)]
pub slug: String,
#[sea_orm(unique)]
pub name: String,
pub description: Option<String>,
pub url: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::repo::Entity")]
Repo,
}
impl Related<super::repo::Entity> for Entity {
fn to() -> RelationDef {
Relation::Repo.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -2,6 +2,7 @@
pub mod prelude; pub mod prelude;
pub mod distro;
pub mod package; pub mod package;
pub mod package_conflicts; pub mod package_conflicts;
pub mod package_depends; pub mod package_depends;

View File

@ -1,5 +1,6 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
pub use super::distro::Entity as Distro;
pub use super::package::Entity as Package; pub use super::package::Entity as Package;
pub use super::package_conflicts::Entity as PackageConflicts; pub use super::package_conflicts::Entity as PackageConflicts;
pub use super::package_depends::Entity as PackageDepends; pub use super::package_depends::Entity as PackageDepends;

View File

@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
pub distro_id: i32,
#[sea_orm(unique)] #[sea_orm(unique)]
pub name: String, pub name: String,
pub description: Option<String>, pub description: Option<String>,
@ -15,10 +16,24 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { pub enum Relation {
#[sea_orm(
belongs_to = "super::distro::Entity",
from = "Column::DistroId",
to = "super::distro::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Distro,
#[sea_orm(has_many = "super::package::Entity")] #[sea_orm(has_many = "super::package::Entity")]
Package, Package,
} }
impl Related<super::distro::Entity> for Entity {
fn to() -> RelationDef {
Relation::Distro.def()
}
}
impl Related<super::package::Entity> for Entity { impl Related<super::package::Entity> for Entity {
fn to() -> RelationDef { fn to() -> RelationDef {
Relation::Package.def() Relation::Package.def()

View File

@ -42,6 +42,7 @@ pub struct RieterDb {
conn: DatabaseConnection, conn: DatabaseConnection,
pub pkg: query::PackageQuery, pub pkg: query::PackageQuery,
pub repo: query::RepoQuery, pub repo: query::RepoQuery,
pub distro: query::DistroQuery,
} }
impl RieterDb { impl RieterDb {
@ -54,6 +55,7 @@ impl RieterDb {
conn: db.clone(), conn: db.clone(),
pkg: query::PackageQuery::new(db.clone()), pkg: query::PackageQuery::new(db.clone()),
repo: query::RepoQuery::new(db.clone()), repo: query::RepoQuery::new(db.clone()),
distro: query::DistroQuery::new(db.clone()),
}) })
} }
} }

View File

@ -0,0 +1,46 @@
use sea_orm::*;
use crate::db::*;
#[derive(Clone, Debug)]
pub struct DistroQuery {
conn: DatabaseConnection,
}
impl DistroQuery {
pub fn new(conn: DatabaseConnection) -> Self {
Self { conn }
}
pub async fn page(&self, per_page: u64, page: u64) -> Result<(u64, Vec<distro::Model>)> {
let paginator = Distro::find()
.order_by_asc(distro::Column::Id)
.paginate(&self.conn, per_page);
let results = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, results))
}
pub async fn by_id(&self, id: i32) -> Result<Option<distro::Model>> {
distro::Entity::find_by_id(id).one(&self.conn).await
}
pub async fn insert(
&self,
slug: &str,
name: &str,
description: Option<&str>,
url: Option<&str>,
) -> Result<InsertResult<distro::ActiveModel>> {
let model = distro::ActiveModel {
id: NotSet,
slug: Set(String::from(slug)),
name: Set(String::from(name)),
description: Set(description.map(String::from)),
url: Set(url.map(String::from)),
};
Distro::insert(model).exec(&self.conn).await
}
}

View File

@ -1,6 +1,8 @@
mod distro;
mod package; mod package;
mod repo; mod repo;
pub use distro::DistroQuery;
pub use package::PackageQuery; pub use package::PackageQuery;
pub use repo::RepoQuery; pub use repo::RepoQuery;

View File

@ -40,6 +40,8 @@ impl RepoQuery {
) -> Result<InsertResult<repo::ActiveModel>> { ) -> Result<InsertResult<repo::ActiveModel>> {
let model = repo::ActiveModel { let model = repo::ActiveModel {
id: NotSet, id: NotSet,
// TODO CHANGE THIS
distro_id: NotSet,
name: Set(String::from(name)), name: Set(String::from(name)),
description: Set(description.map(String::from)), description: Set(description.map(String::from)),
}; };