Skip to content

Commit

Permalink
add /splashes/:id patch endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmanczak committed Jul 21, 2024
1 parent ba00749 commit 31d4ec1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 32 additions & 1 deletion src/routes/splash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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.";
Expand Down Expand Up @@ -204,3 +206,32 @@ async fn splashes_id_delete(headers: HeaderMap, Path(id): Path<String>) -> Respo
}
}
}

async fn splashes_id_patch(
headers: HeaderMap,
Path(id): Path<String>,
Json(body): Json<CreateSplash>,
) -> 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();
}
}
}

0 comments on commit 31d4ec1

Please sign in to comment.