From 7541a88b90b5756057182499b2e1270f634717d3 Mon Sep 17 00:00:00 2001 From: Ian Stanton Date: Thu, 6 Apr 2023 18:00:51 -0400 Subject: [PATCH] [COR-564] Add latest version and timestamps to `/extensions/all` (#212) --- sqlx-data.json | 20 ++++++++++++++++++++ src/download.rs | 19 +++++++++++++++++++ src/routes.rs | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/sqlx-data.json b/sqlx-data.json index 5ea22aaa..b2daaa91 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -146,6 +146,26 @@ }, "query": "SELECT * FROM extensions WHERE name = $1" }, + "514adcd37018e70d0c1e0ca4c7726d6d4e733d8d3c4c893290bf87eca4452abf": { + "describe": { + "columns": [ + { + "name": "max", + "ordinal": 0, + "type_info": "Text" + } + ], + "nullable": [ + null + ], + "parameters": { + "Left": [ + "Int4" + ] + } + }, + "query": "SELECT MAX(num) FROM versions WHERE extension_id = $1;" + }, "6cdd35899d214093e96bd8cae07778d12089e0a8712cfe94dc6b747c9b37c3d5": { "describe": { "columns": [], diff --git a/src/download.rs b/src/download.rs index f82269bc..b706944a 100644 --- a/src/download.rs +++ b/src/download.rs @@ -1,7 +1,9 @@ //! Functionality for downloading extensions and maintaining download counts use crate::config::Config; +use crate::errors::ExtensionRegistryError; use crate::uploader::extension_location; use actix_web::{get, web, HttpResponse, Responder}; +use sqlx::{Pool, Postgres}; /// Handles the `GET /extensions/:extension_name/:version/download` route. /// This returns a URL to the location where the extension is stored. @@ -13,3 +15,20 @@ pub async fn download(cfg: web::Data, path: web::Path<(String, String)>) let url = extension_location(&cfg.bucket_name, &name, &version); HttpResponse::Ok().body(url) } + +pub async fn latest_version( + extension_name: &str, + conn: web::Data>, +) -> Result { + // Create a transaction on the database, if there are no errors, + // commit the transactions to record a new or updated extension. + let mut tx = conn.begin().await?; + let ext = sqlx::query!("SELECT * FROM extensions WHERE name = $1", extension_name) + .fetch_one(&mut tx) + .await?; + let id: i32 = ext.id as i32; + let latest = sqlx::query!("SELECT MAX(num) FROM versions WHERE extension_id = $1;", id) + .fetch_one(&mut tx) + .await?; + Ok(latest.max.unwrap()) +} diff --git a/src/routes.rs b/src/routes.rs index c901c09b..bad75f77 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,3 +1,4 @@ +use crate::download::latest_version; use crate::errors::ExtensionRegistryError; use actix_web::{get, web, HttpResponse, Responder}; use serde_json::{json, Value}; @@ -21,9 +22,14 @@ pub async fn get_all_extensions( .fetch_all(&mut tx) .await?; for row in rows.iter() { + let name = row.name.to_owned().unwrap(); + let latest = latest_version(&name, conn.clone()).await?; let data = json!( { "name": row.name.to_owned(), + "latestVersion": latest, + "createdAt": row.created_at.unwrap().to_string(), + "updatedAt": row.updated_at.unwrap().to_string(), "description": row.description.to_owned(), "homepage": row.homepage.to_owned(), "documentation": row.documentation.to_owned(),