Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Fix download count logic (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically authored May 14, 2022
1 parent 3533d2a commit b9b4f2b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
21 changes: 21 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,27 @@
"nullable": []
}
},
"331041c6a4f27f4a6ac2873332074c0127e7368c8ab803843760530d29aaef08": {
"query": "SELECT id FROM versions\n WHERE (version_number = $1 AND mod_id = $2)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Text",
"Int8"
]
},
"nullable": [
false
]
}
},
"33a965c7dc615d3b701c05299889357db8dd36d378850625d2602ba471af4885": {
"query": "\n UPDATE mods\n SET downloads = downloads + $1\n WHERE (id = $2)\n ",
"describe": {
Expand Down
31 changes: 23 additions & 8 deletions src/routes/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use crate::util::guards::admin_key_guard;
use actix_web::{patch, web, HttpResponse};
use serde::Deserialize;
use sqlx::PgPool;
use crate::models::ids::ProjectId;

#[derive(Deserialize)]
pub struct DownloadBody {
pub url: String,
pub hash: ProjectId,
pub version_name: String,
}

// This is an internal route, cannot be used without key
Expand All @@ -15,27 +18,39 @@ pub async fn count_download(
pool: web::Data<PgPool>,
download_body: web::Json<DownloadBody>,
) -> Result<HttpResponse, ApiError> {
let version = sqlx::query!(
let project_id : crate::database::models::ids::ProjectId = download_body.hash.into();

let version_id = if let Some(version) = sqlx::query!(
"SELECT id FROM versions
WHERE (version_number = $1 AND mod_id = $2)",
download_body.version_name,
project_id as crate::database::models::ids::ProjectId
)
.fetch_optional(pool.as_ref())
.await? {
version.id
} else if let Some(version) = sqlx::query!(
"
SELECT v.id id, v.mod_id project_id FROM files f
INNER JOIN versions v ON v.id = f.version_id
WHERE f.url = $1
",
download_body.url,
)
.fetch_optional(pool.as_ref())
.await?
.ok_or_else(|| {
ApiError::InvalidInput("Specified version does not exist!".to_string())
})?;
.fetch_optional(pool.as_ref())
.await? {
version.id
} else {
return Err(ApiError::InvalidInput("Specified version does not exist!".to_string()));
};

let mut transaction = pool.begin().await?;

sqlx::query!(
"UPDATE versions
SET downloads = downloads + 1
WHERE (id = $1)",
version.id
version_id
)
.execute(&mut *transaction)
.await?;
Expand All @@ -44,7 +59,7 @@ pub async fn count_download(
"UPDATE mods
SET downloads = downloads + 1
WHERE (id = $1)",
version.project_id
version_id
)
.execute(&mut *transaction)
.await?;
Expand Down
1 change: 1 addition & 0 deletions src/validate/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct PackFormat<'a> {
}

#[derive(Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
pub struct PackFile<'a> {
pub path: &'a str,
pub hashes: std::collections::HashMap<FileHash, &'a str>,
Expand Down

0 comments on commit b9b4f2b

Please sign in to comment.