You've already forked AstralRinth
forked from didirus/AstralRinth
Rework download route (#345)
This commit is contained in:
55
src/routes/admin.rs
Normal file
55
src/routes/admin.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use crate::routes::ApiError;
|
||||
use crate::util::guards::admin_key_guard;
|
||||
use actix_web::{patch, web, HttpResponse};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct DownloadBody {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
// This is an internal route, cannot be used without key
|
||||
#[patch("/_count-download", guard = "admin_key_guard")]
|
||||
pub async fn version_count_patch(
|
||||
pool: web::Data<PgPool>,
|
||||
download_body: web::Json<DownloadBody>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let 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())
|
||||
})?;
|
||||
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE versions
|
||||
SET downloads = downloads + 1
|
||||
WHERE (id = $1)",
|
||||
version.id
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE mods
|
||||
SET downloads = downloads + 1
|
||||
WHERE (id = $1)",
|
||||
version.project_id
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(HttpResponse::Ok().body(""))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod v1;
|
||||
pub use v1::v1_config;
|
||||
|
||||
mod admin;
|
||||
mod auth;
|
||||
mod health;
|
||||
mod index;
|
||||
@@ -87,7 +88,6 @@ pub fn versions_config(cfg: &mut web::ServiceConfig) {
|
||||
web::scope("version")
|
||||
.service(versions::version_get)
|
||||
.service(versions::version_delete)
|
||||
.service(versions::version_count_patch)
|
||||
.service(version_creation::upload_file_to_version)
|
||||
.service(versions::version_edit),
|
||||
);
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use super::ApiError;
|
||||
use crate::database;
|
||||
use crate::database::models as db_models;
|
||||
use crate::models;
|
||||
use crate::models::projects::{Dependency, Version};
|
||||
use crate::models::teams::Permissions;
|
||||
use crate::util::auth::{get_user_from_headers, is_authorized};
|
||||
use crate::util::guards::admin_key_guard;
|
||||
use crate::util::validate::validation_errors_to_string;
|
||||
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -496,55 +494,6 @@ pub async fn version_edit(
|
||||
}
|
||||
}
|
||||
|
||||
// This is an internal route, cannot be used without key
|
||||
#[patch(
|
||||
"{project_id}/{version_name}/_count-download",
|
||||
guard = "admin_key_guard"
|
||||
)]
|
||||
pub async fn version_count_patch(
|
||||
info: web::Path<(models::ids::ProjectId, String)>,
|
||||
pool: web::Data<PgPool>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let (project, version_name) = info.into_inner();
|
||||
let project = db_models::ids::ProjectId::from(project);
|
||||
|
||||
let version = sqlx::query!(
|
||||
"SELECT id FROM versions
|
||||
WHERE (version_number = $1 AND mod_id = $2)",
|
||||
version_name,
|
||||
project as db_models::ids::ProjectId
|
||||
)
|
||||
.fetch_optional(pool.as_ref())
|
||||
.await?;
|
||||
let version = match version {
|
||||
Some(version) => db_models::ids::VersionId(version.id),
|
||||
_ => {
|
||||
return Ok(HttpResponse::NotFound().body("Could not find version!"))
|
||||
}
|
||||
};
|
||||
|
||||
futures::future::try_join(
|
||||
sqlx::query!(
|
||||
"UPDATE versions
|
||||
SET downloads = downloads + 1
|
||||
WHERE (id = $1)",
|
||||
version as db_models::ids::VersionId
|
||||
)
|
||||
.execute(pool.as_ref()),
|
||||
sqlx::query!(
|
||||
"UPDATE mods
|
||||
SET downloads = downloads + 1
|
||||
WHERE (id = $1)",
|
||||
project as db_models::ids::ProjectId
|
||||
)
|
||||
.execute(pool.as_ref()),
|
||||
)
|
||||
.await
|
||||
.map_err(ApiError::SqlxDatabase)?;
|
||||
|
||||
Ok(HttpResponse::Ok().body(""))
|
||||
}
|
||||
|
||||
#[delete("{version_id}")]
|
||||
pub async fn version_delete(
|
||||
req: HttpRequest,
|
||||
|
||||
@@ -160,8 +160,8 @@ pub async fn search_for_project(
|
||||
"relevance" => ("projects", ["downloads:desc"]),
|
||||
"downloads" => ("projects_filtered", ["downloads:desc"]),
|
||||
"follows" => ("projects_filtered", ["follows:desc"]),
|
||||
"updated" => ("projects_filtered", ["date_created:desc"]),
|
||||
"newest" => ("projects_filtered", ["date_modified:desc"]),
|
||||
"updated" => ("projects_filtered", ["date_created:asc"]),
|
||||
"newest" => ("projects_filtered", ["date_modified:asc"]),
|
||||
i => return Err(SearchError::InvalidIndex(i.to_string())),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user