feat(server): add read-only distro api routes
parent
0565328ea8
commit
1b80bcd757
|
@ -11,12 +11,43 @@ 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>,
|
||||
|
|
|
@ -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 {}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod distro;
|
||||
pub mod package;
|
||||
pub mod package_conflicts;
|
||||
pub mod package_depends;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
//! `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_conflicts::Entity as PackageConflicts;
|
||||
pub use super::package_depends::Entity as PackageDepends;
|
||||
|
|
|
@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
|
|||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub distro_id: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
|
@ -15,10 +16,24 @@ pub struct Model {
|
|||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
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")]
|
||||
Package,
|
||||
}
|
||||
|
||||
impl Related<super::distro::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Distro.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::package::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Package.def()
|
||||
|
|
|
@ -42,6 +42,7 @@ pub struct RieterDb {
|
|||
conn: DatabaseConnection,
|
||||
pub pkg: query::PackageQuery,
|
||||
pub repo: query::RepoQuery,
|
||||
pub distro: query::DistroQuery,
|
||||
}
|
||||
|
||||
impl RieterDb {
|
||||
|
@ -54,6 +55,7 @@ impl RieterDb {
|
|||
conn: db.clone(),
|
||||
pkg: query::PackageQuery::new(db.clone()),
|
||||
repo: query::RepoQuery::new(db.clone()),
|
||||
distro: query::DistroQuery::new(db.clone()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
mod distro;
|
||||
mod package;
|
||||
mod repo;
|
||||
|
||||
pub use distro::DistroQuery;
|
||||
pub use package::PackageQuery;
|
||||
pub use repo::RepoQuery;
|
||||
|
||||
|
|
|
@ -40,6 +40,8 @@ impl RepoQuery {
|
|||
) -> Result<InsertResult<repo::ActiveModel>> {
|
||||
let model = repo::ActiveModel {
|
||||
id: NotSet,
|
||||
// TODO CHANGE THIS
|
||||
distro_id: NotSet,
|
||||
name: Set(String::from(name)),
|
||||
description: Set(description.map(String::from)),
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue