Skip to content

Commit

Permalink
Remove item API
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaq committed Oct 5, 2024
1 parent f47cf57 commit 7f18023
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions crates/market-helper-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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<Arc<AppState>>,
Json(body): Json<Value>,
) -> 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(())
}
Binary file modified database/market_helper.db-shm
Binary file not shown.
Binary file modified database/market_helper.db-wal
Binary file not shown.

0 comments on commit 7f18023

Please sign in to comment.