From cecc4dba23c2a5c4969deb24467de8e3b6d7892a Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 10 Oct 2021 15:15:59 +0100 Subject: [PATCH] Fetch latest pushed version for crate search results --- chartered-db/src/crates.rs | 16 ++++++++++++++++ chartered-frontend/src/pages/Search.tsx | 3 ++- chartered-web/src/endpoints/web_api/crates/search.rs | 53 ++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/chartered-db/src/crates.rs b/chartered-db/src/crates.rs index ce553b6..b5678c4 100644 --- a/chartered-db/src/crates.rs +++ a/chartered-db/src/crates.rs @@ -282,6 +282,22 @@ } impl CrateWithPermissions { + pub async fn latest_version( + self: Arc, + conn: ConnectionPool, + ) -> Result>> { + tokio::task::spawn_blocking(move || { + let conn = conn.get()?; + + Ok(CrateVersion::belonging_to(&self.crate_) + .order_by(crate_versions::id.desc()) + .limit(1) + .get_result::(&conn) + .optional()?) + }) + .await? + } + pub async fn version( self: Arc, conn: ConnectionPool, diff --git a/chartered-frontend/src/pages/Search.tsx b/chartered-frontend/src/pages/Search.tsx index b1106b9..ecab1a1 100644 --- a/chartered-frontend/src/pages/Search.tsx +++ a/chartered-frontend/src/pages/Search.tsx @@ -110,6 +110,7 @@ description: string; homepage?: string; repository?: string; + version: string; } function CrateResults({ @@ -170,7 +171,7 @@ /{crate.name} -
0.1.2
+
{crate.version}

{crate.description}

diff --git a/chartered-web/src/endpoints/web_api/crates/search.rs b/chartered-web/src/endpoints/web_api/crates/search.rs index f8a2dd4..ab27411 100644 --- a/chartered-web/src/endpoints/web_api/crates/search.rs +++ a/chartered-web/src/endpoints/web_api/crates/search.rs @@ -1,6 +1,5 @@ use axum::{extract, Json}; use chartered_db::{crates::Crate, permissions::UserPermission, users::User, ConnectionPool}; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::sync::Arc; use thiserror::Error; @@ -23,7 +22,6 @@ version: String, homepage: Option, repository: Option, - updated: DateTime, permissions: UserPermission, } @@ -32,25 +30,38 @@ extract::Extension(user): extract::Extension>, extract::Query(req): extract::Query, ) -> Result, Error> { - let crates = Crate::search(db, user.id, req.q, 5) - .await? - .into_iter() - .map(|(org, crates_with_permissions)| { - crates_with_permissions - .into_iter() - .map(move |v| ResponseCrate { - organisation: org.name.to_string(), - name: v.crate_.name, - description: v.crate_.description, - version: "test".to_string(), - homepage: v.crate_.homepage, - repository: v.crate_.repository, - updated: Utc::now(), // todo - permissions: v.permissions, - }) - }) - .flatten() - .collect(); + let crates = futures::future::try_join_all( + Crate::search(db.clone(), user.id, req.q, 5) + .await? + .into_iter() + .map(move |(org, crates_with_permissions)| { + let db = db.clone(); + + crates_with_permissions + .into_iter() + .map(Arc::new) + .map(move |v| { + let db = db.clone(); + let org_name = org.name.clone(); + + async move { + let version = v.clone().latest_version(db).await?; + + Ok::<_, Error>(ResponseCrate { + organisation: org_name, + name: v.crate_.name.clone(), + description: v.crate_.description.clone(), + version: version.map(|v| v.version).unwrap_or_default(), + homepage: v.crate_.homepage.clone(), + repository: v.crate_.repository.clone(), + permissions: v.permissions.clone(), + }) + } + }) + }) + .flatten(), + ) + .await?; Ok(Json(Response { crates })) } -- rgit 0.1.3