feat(server): serve full package info from api
parent
2df52320d1
commit
5c8b7ac3e0
|
@ -4,7 +4,6 @@ use axum::extract::{Path, Query, State};
|
|||
use axum::routing::get;
|
||||
use axum::Json;
|
||||
use axum::Router;
|
||||
use sea_orm::ModelTrait;
|
||||
|
||||
use pagination::PaginatedResponse;
|
||||
|
||||
|
@ -60,31 +59,15 @@ async fn get_packages(
|
|||
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(
|
||||
State(global): State<crate::Global>,
|
||||
Path(id): Path<i32>,
|
||||
) -> crate::Result<Json<PackageRes>> {
|
||||
) -> crate::Result<Json<crate::db::FullPackage>> {
|
||||
let entry = global
|
||||
.db
|
||||
.package(id)
|
||||
.full_package(id)
|
||||
.await?
|
||||
.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(res))
|
||||
Ok(Json(entry))
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ mod migrator;
|
|||
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult,
|
||||
DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, NotSet, PaginatorTrait, QueryFilter,
|
||||
QueryOrder, Set,
|
||||
DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, ModelTrait, NotSet, PaginatorTrait,
|
||||
QueryFilter, QueryOrder, Set,
|
||||
};
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -28,6 +28,18 @@ pub enum PackageDepend {
|
|||
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)]
|
||||
pub struct RieterDb {
|
||||
pub conn: DatabaseConnection,
|
||||
|
@ -220,4 +232,63 @@ impl RieterDb {
|
|||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue