Skip to content

Commit

Permalink
[COR-564] Add latest version and timestamps to /extensions/all (tem…
Browse files Browse the repository at this point in the history
  • Loading branch information
ianstanton committed Apr 6, 2023
1 parent 078990c commit 7541a88
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
19 changes: 19 additions & 0 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -13,3 +15,20 @@ pub async fn download(cfg: web::Data<Config>, 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<Pool<Postgres>>,
) -> Result<String, ExtensionRegistryError> {
// 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())
}
6 changes: 6 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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(),
Expand Down

0 comments on commit 7541a88

Please sign in to comment.