🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2021-10-10 15:15:59.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2021-10-10 15:15:59.0 +01:00:00
commit
cecc4dba23c2a5c4969deb24467de8e3b6d7892a [patch]
tree
ad8d22a838d32ce9230c1f22a99e51b0b7eb6019
parent
d13bc75c1e9cbf02bec42751580431bdee57fbc7
download
cecc4dba23c2a5c4969deb24467de8e3b6d7892a.tar.gz

Fetch latest pushed version for crate search results



Diff

 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<Self>,
        conn: ConnectionPool,
    ) -> Result<Option<CrateVersion<'static>>> {
        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::<CrateVersion>(&conn)
                .optional()?)
        })
        .await?
    }

    pub async fn version(
        self: Arc<Self>,
        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}
                      </h4>
                    </Link>
                    <h6 className="text-secondary m-0 mt-1">0.1.2</h6>
                    <h6 className="text-secondary m-0 mt-1">{crate.version}</h6>
                  </div>

                  <p className="m-0">{crate.description}</p>
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<String>,
    repository: Option<String>,
    updated: DateTime<Utc>,
    permissions: UserPermission,
}

@@ -32,25 +30,38 @@
    extract::Extension(user): extract::Extension<Arc<User>>,
    extract::Query(req): extract::Query<RequestParams>,
) -> Result<Json<Response>, 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 }))
}