use crate::aur::aur::query_aur; use crate::db::migration::JoinType; use crate::db::prelude::{Builds, Packages}; use crate::db::{builds, packages, versions}; use rocket::response::status::NotFound; use rocket::serde::json::Json; use rocket::serde::{Deserialize, Serialize}; use rocket::{get, State}; use rocket_okapi::okapi::schemars; use rocket_okapi::{openapi, JsonSchema}; use sea_orm::{ColumnTrait, QueryFilter}; use sea_orm::{DatabaseConnection, EntityTrait, FromQueryResult, QuerySelect, RelationTrait}; #[derive(Serialize, JsonSchema)] #[serde(crate = "rocket::serde")] pub struct ApiPackage { name: String, version: String, } #[openapi(tag = "test")] #[get("/search?")] pub async fn search(query: &str) -> Result>, String> { return match query_aur(query).await { Ok(v) => { let mapped = v .iter() .map(|x| ApiPackage { name: x.name.clone(), version: x.version.clone(), }) .collect(); Ok(Json(mapped)) } Err(e) => Err(format!("{}", e)), }; } #[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] #[serde(crate = "rocket::serde")] pub struct ListPackageModel { id: i32, name: String, count: i32, status: i32, } #[openapi(tag = "test")] #[get("/packages/list")] pub async fn package_list( db: &State, ) -> Result>, String> { let db = db as &DatabaseConnection; let all: Vec = Packages::find() .join_rev(JoinType::InnerJoin, versions::Relation::Packages.def()) .select_only() .column_as(versions::Column::Id.count(), "count") .column(packages::Column::Name) .column(packages::Column::Id) .column(packages::Column::Status) .group_by(packages::Column::Name) .into_model::() .all(db) .await .unwrap(); Ok(Json(all)) } #[openapi(tag = "test")] #[get("/builds/output?&")] pub async fn build_output( db: &State, buildid: i32, startline: Option, ) -> Result> { let db = db as &DatabaseConnection; let build = Builds::find_by_id(buildid) .one(db) .await .map_err(|e| NotFound(e.to_string()))? .ok_or(NotFound("couldn't find id".to_string()))?; return match build.ouput { None => Err(NotFound("No Output".to_string())), Some(v) => match startline { None => Ok(v), Some(startline) => { let output: Vec = v.split("\n").map(|x| x.to_string()).collect(); let len = output.len(); let len_missing = len as i32 - startline; let output = output .iter() .rev() .take(if len_missing > 0 { len_missing as usize } else { 0 }) .rev() .map(|x1| x1.clone()) .collect::>(); let output = output.join("\n"); Ok(output) } }, }; } #[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] #[serde(crate = "rocket::serde")] pub struct ListBuildsModel { id: i32, pkg_id: i32, version_id: i32, status: Option, } #[openapi(tag = "test")] #[get("/builds?")] pub async fn list_builds( db: &State, pkgid: i32, ) -> Result>, NotFound> { let db = db as &DatabaseConnection; let build = Builds::find() .filter(builds::Column::PkgId.eq(pkgid)) .all(db) .await .map_err(|e| NotFound(e.to_string()))?; Ok(Json( build .iter() .map(|x| ListBuildsModel { id: x.id, status: x.status, pkg_id: x.pkg_id, version_id: x.version_id, }) .collect::>(), )) }