From 644183e289b05cc58b8ea5d52b7246d9f6f431f0 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Wed, 24 Jan 2024 19:15:02 +0200 Subject: [PATCH] move mod update to POST v1/mods/{id}/versions --- openapi.yml | 2 +- src/endpoints/mod_versions.rs | 33 +++++++++++++++++++++++++++++++-- src/endpoints/mods.rs | 17 +---------------- src/main.rs | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/openapi.yml b/openapi.yml index 34653c4..4ff95a8 100644 --- a/openapi.yml +++ b/openapi.yml @@ -100,7 +100,7 @@ paths: schema: $ref: '#/components/schemas/Mod' - /mods/{id}/{version}: + /mods/{id}/versions/{version}: get: tags: - mods diff --git a/src/endpoints/mod_versions.rs b/src/endpoints/mod_versions.rs index 2c08f0c..63c6962 100644 --- a/src/endpoints/mod_versions.rs +++ b/src/endpoints/mod_versions.rs @@ -1,7 +1,8 @@ -use actix_web::{get, web, Responder, HttpResponse}; +use actix_web::{get, post, web, Responder, HttpResponse}; use serde::Deserialize; +use sqlx::Acquire; -use crate::{AppData, types::{api::{ApiError, ApiResponse}, models::mod_version::ModVersion}}; +use crate::{AppData, types::{api::{ApiError, ApiResponse}, models::{mod_version::ModVersion, mod_entity::{download_geode_file, Mod}}, mod_json::ModJson}}; #[derive(Deserialize)] struct GetOnePath { @@ -9,6 +10,16 @@ struct GetOnePath { version: String } +#[derive(Deserialize)] +struct CreateQueryParams { + download_url: String +} + +#[derive(Deserialize)] +struct CreateVersionPath { + id: String +} + #[get("v1/mods/{id}/versions/{version}")] pub async fn get_one(path: web::Path, data: web::Data) -> Result { let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?; @@ -22,4 +33,22 @@ pub async fn download_version(path: web::Path, data: web::Data, data: web::Data, payload: web::Json) -> Result { + let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?; + let mut file_path = download_geode_file(&payload.download_url).await?; + let json = ModJson::from_zip(&mut file_path, payload.download_url.as_str()).or(Err(ApiError::FilesystemError))?; + if json.id != path.id { + return Err(ApiError::BadRequest(format!("Request id {} does not match mod.json id {}", path.id, json.id))); + } + let mut transaction = pool.begin().await.or(Err(ApiError::DbError))?; + let result = Mod::new_version(&json, &mut transaction).await; + if result.is_err() { + let _ = transaction.rollback().await; + return Err(result.err().unwrap()); + } + let _ = transaction.commit().await; + Ok(HttpResponse::NoContent()) } \ No newline at end of file diff --git a/src/endpoints/mods.rs b/src/endpoints/mods.rs index 7b35b78..ce6bae0 100644 --- a/src/endpoints/mods.rs +++ b/src/endpoints/mods.rs @@ -1,4 +1,4 @@ -use actix_web::{get, web, Responder, post, HttpResponse, patch}; +use actix_web::{get, web, Responder, post, HttpResponse}; use serde::Deserialize; use sqlx::Acquire; use log::info; @@ -66,19 +66,4 @@ pub async fn create(data: web::Data, payload: web::Json, payload: web::Json) -> Result { - let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?; - let mut file_path = download_geode_file(&payload.download_url).await?; - let json = ModJson::from_zip(&mut file_path, payload.download_url.as_str()).or(Err(ApiError::FilesystemError))?; - let mut transaction = pool.begin().await.or(Err(ApiError::DbError))?; - let result = Mod::new_version(&json, &mut transaction).await; - if result.is_err() { - let _ = transaction.rollback().await; - return Err(result.err().unwrap()); - } - let _ = transaction.commit().await; - Ok(HttpResponse::NoContent()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0afb273..3d92f11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,9 @@ async fn main() -> anyhow::Result<()> { .service(endpoints::mods::index) .service(endpoints::mods::get) .service(endpoints::mods::create) - .service(endpoints::mods::update) .service(endpoints::mod_versions::get_one) .service(endpoints::mod_versions::download_version) + .service(endpoints::mod_versions::create_version) .service(health) }).bind((addr, port))?;