From 7f18023ad72b845b56bfde63b3080619f1763909 Mon Sep 17 00:00:00 2001 From: bakaq Date: Sat, 5 Oct 2024 00:21:39 -0300 Subject: [PATCH] Remove item API --- crates/market-helper-server/src/main.rs | 24 ++++++++++++++++++++++++ database/market_helper.db-shm | Bin 32768 -> 32768 bytes database/market_helper.db-wal | Bin 4152 -> 8272 bytes 3 files changed, 24 insertions(+) diff --git a/crates/market-helper-server/src/main.rs b/crates/market-helper-server/src/main.rs index 967cc24..a41a88d 100644 --- a/crates/market-helper-server/src/main.rs +++ b/crates/market-helper-server/src/main.rs @@ -12,6 +12,7 @@ use axum::{ routing::get, Json, Router, }; +use serde::{Deserialize, Serialize}; use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode}; use sqlx::{ConnectOptions, Row, SqliteConnection}; use tokio::sync::Mutex; @@ -44,6 +45,7 @@ async fn main() -> anyhow::Result<()> { let app = Router::new() .route("/item/get_all", get(get_items)) .route("/item/add", post(add_item)) + .route("/item/remove", post(remove_item)) .with_state(app_state); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; @@ -108,3 +110,25 @@ async fn add_item( Ok(()) } + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct RemoveRequest { + id: i64, +} + +async fn remove_item( + State(state): State>, + Json(body): Json, +) -> Result<(), StatusCode> { + let request: RemoveRequest = + serde_json::from_value(body).map_err(|_| StatusCode::BAD_REQUEST)?; + + let mut conn = state.database_connection.lock().await; + sqlx::query("DELETE FROM items WHERE id = $1;") + .bind(request.id) + .execute(&mut *conn) + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + Ok(()) +} diff --git a/database/market_helper.db-shm b/database/market_helper.db-shm index ec5ecc5059eee70d8871d545c4485ea9a964e17a..65318962e8b41724bfb8c280966123ea93e8009c 100644 GIT binary patch delta 154 zcmZo@U}|V!s+V}A%K!q55G=q1q(S(2vzuJ}$28TijAvwCH7kELXS%@>)}AwwRP{ik gzyM_Cepw-orbP~i