Skip to content

Commit

Permalink
move mod update to POST v1/mods/{id}/versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 24, 2024
1 parent d9e2cb6 commit 644183e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ paths:
schema:
$ref: '#/components/schemas/Mod'

/mods/{id}/{version}:
/mods/{id}/versions/{version}:
get:
tags:
- mods
Expand Down
33 changes: 31 additions & 2 deletions src/endpoints/mod_versions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
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 {
id: String,
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<GetOnePath>, data: web::Data<AppData>) -> Result<impl Responder, ApiError> {
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
Expand All @@ -22,4 +33,22 @@ pub async fn download_version(path: web::Path<GetOnePath>, data: web::Data<AppDa
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
let url = ModVersion::get_download_url(&path.id, &path.version, &mut pool).await?;
Ok(HttpResponse::Found().append_header(("Location", url)).finish())
}

#[post("v1/mods/{id}/versions")]
pub async fn create_version(path: web::Path<CreateVersionPath>, data: web::Data<AppData>, payload: web::Json<CreateQueryParams >) -> Result<impl Responder, ApiError> {
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())
}
17 changes: 1 addition & 16 deletions src/endpoints/mods.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -66,19 +66,4 @@ pub async fn create(data: web::Data<AppData>, payload: web::Json<CreateQueryPara
info!("{:?}", tr_res);
}
Ok(HttpResponse::NoContent())
}

#[patch("/v1/mods")]
pub async fn update(data: web::Data<AppData>, payload: web::Json<CreateQueryParams>) -> Result<impl Responder, ApiError> {
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())
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;

Expand Down

0 comments on commit 644183e

Please sign in to comment.