diff --git a/Cargo.lock b/Cargo.lock index 3ce3446..9bb7a3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,7 +19,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "api" -version = "2.5.0" +version = "2.6.0" dependencies = [ "axum", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index f9fc099..c3736af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "api" -version = "2.5.0" +version = "2.6.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/routes/splash.rs b/src/routes/splash.rs index cd9741a..71fd89e 100644 --- a/src/routes/splash.rs +++ b/src/routes/splash.rs @@ -6,7 +6,7 @@ use axum::{ extract::{Path, Query}, http::{HeaderMap, StatusCode}, response::{IntoResponse, Response}, - routing::{delete, get, post}, + routing::{delete, get, patch, post}, Json, Router, }; use serde::{Deserialize, Serialize}; @@ -24,6 +24,8 @@ pub fn route() -> Router { .route("/splashes", post(splashes_post)) // DELETE .route("/splashes/:id", delete(splashes_id_delete)) + // PATCH + .route("/splashes/:id", patch(splashes_id_patch)) } pub static NO_SPLASHES: &str = "No splashes found."; @@ -204,3 +206,32 @@ async fn splashes_id_delete(headers: HeaderMap, Path(id): Path) -> Respo } } } + +async fn splashes_id_patch( + headers: HeaderMap, + Path(id): Path, + Json(body): Json, +) -> Response { + let auth = match get_basic_auth_from_headers(&headers) { + Some(auth) => auth, + None => return StatusCode::UNAUTHORIZED.into_response(), + }; + match validate_password_hash_from_basic_auth(&auth) { + StatusCode::OK => (), + code => return code.into_response(), + } + + let conn = database::initialise_sqlite_connection(); + let query = "UPDATE splashes SET splash = :splash WHERE id = :id"; + let mut statement = conn.prepare(query).unwrap(); + statement.bind((":id", id.as_str())).unwrap(); + statement.bind((":splash", body.splash.as_str())).unwrap(); + + match statement.next() { + Ok(_) => return StatusCode::OK.into_response(), + Err(e) => { + error!("Returned 500 in PATCH /splashes/:id due to error: {e}"); + return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(); + } + } +}