Skip to content

Commit

Permalink
Update item API
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaq committed Oct 5, 2024
1 parent 7f18023 commit 771ff11
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions crates/market-helper-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();

let app_state = Arc::new(AppState::try_new().await?);
// TODO: Use the right HTTP methods.
let app = Router::new()
.route("/item/get_all", get(get_items))
.route("/item/add", post(add_item))
.route("/item/remove", post(remove_item))
.route("/item/update", post(update_item))
.with_state(app_state);

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
Expand Down Expand Up @@ -132,3 +134,43 @@ async fn remove_item(

Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct UpdateRequest {
id: i64,
new_item: ItemDescription,
}

async fn update_item(
State(state): State<Arc<AppState>>,
Json(body): Json<Value>,
) -> Result<(), StatusCode> {
let request: UpdateRequest =
serde_json::from_value(body).map_err(|_| StatusCode::BAD_REQUEST)?;

let mut conn = state.database_connection.lock().await;
let query_str = r#"
UPDATE items
SET
name = $1,
price = $2,
weight = $3,
portion_weight = $4,
calories = $5,
protein = $6
WHERE id = $7;
"#;
sqlx::query(query_str)
.bind(request.new_item.name)
.bind(request.new_item.price)
.bind(request.new_item.weight)
.bind(request.new_item.nutrition.portion_weight)
.bind(request.new_item.nutrition.calories)
.bind(request.new_item.nutrition.protein)
.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 771ff11

Please sign in to comment.