feat(server): serve full package info from api
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline failed Details
ci/woodpecker/push/build Pipeline was successful Details

repo-db
Jef Roosens 2023-08-04 19:08:55 +02:00
parent 2df52320d1
commit 5c8b7ac3e0
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 76 additions and 22 deletions

View File

@ -4,7 +4,6 @@ use axum::extract::{Path, Query, State};
use axum::routing::get; use axum::routing::get;
use axum::Json; use axum::Json;
use axum::Router; use axum::Router;
use sea_orm::ModelTrait;
use pagination::PaginatedResponse; use pagination::PaginatedResponse;
@ -60,31 +59,15 @@ async fn get_packages(
Ok(Json(pagination.res(total_pages, pkgs))) Ok(Json(pagination.res(total_pages, pkgs)))
} }
#[derive(serde::Serialize)]
pub struct PackageRes {
#[serde(flatten)]
entry: db::package::Model,
licenses: Vec<String>,
}
async fn get_single_package( async fn get_single_package(
State(global): State<crate::Global>, State(global): State<crate::Global>,
Path(id): Path<i32>, Path(id): Path<i32>,
) -> crate::Result<Json<PackageRes>> { ) -> crate::Result<Json<crate::db::FullPackage>> {
let entry = global let entry = global
.db .db
.package(id) .full_package(id)
.await? .await?
.ok_or(axum::http::StatusCode::NOT_FOUND)?; .ok_or(axum::http::StatusCode::NOT_FOUND)?;
let licenses = entry
.find_related(db::PackageLicense)
.all(&global.db)
.await?
.iter()
.map(|e| e.value.clone())
.collect();
let res = PackageRes { entry, licenses }; Ok(Json(entry))
Ok(Json(res))
} }

View File

@ -4,8 +4,8 @@ mod migrator;
use sea_orm::{ use sea_orm::{
ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult, ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult,
DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, NotSet, PaginatorTrait, QueryFilter, DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, ModelTrait, NotSet, PaginatorTrait,
QueryOrder, Set, QueryFilter, QueryOrder, Set,
}; };
use sea_orm_migration::MigratorTrait; use sea_orm_migration::MigratorTrait;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -28,6 +28,18 @@ pub enum PackageDepend {
Opt, Opt,
} }
#[derive(Serialize)]
pub struct FullPackage {
#[serde(flatten)]
entry: package::Model,
licenses: Vec<String>,
groups: Vec<String>,
replaces: Vec<String>,
provides: Vec<String>,
depends: Vec<(PackageDepend, String)>,
files: Vec<String>,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct RieterDb { pub struct RieterDb {
pub conn: DatabaseConnection, pub conn: DatabaseConnection,
@ -220,4 +232,63 @@ impl RieterDb {
Ok(()) Ok(())
} }
pub async fn full_package(&self, id: i32) -> Result<Option<FullPackage>> {
if let Some(entry) = self.package(id).await? {
let licenses = entry
.find_related(PackageLicense)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let groups = entry
.find_related(PackageGroup)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let replaces = entry
.find_related(PackageReplaces)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let provides = entry
.find_related(PackageProvides)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let depends = entry
.find_related(PackageDepends)
.all(self)
.await?
.into_iter()
.map(|e| (e.r#type, e.value))
.collect();
let files = entry
.find_related(PackageFile)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
Ok(Some(FullPackage {
entry,
licenses,
groups,
replaces,
provides,
depends,
files,
}))
} else {
Ok(None)
}
}
} }