diff --git a/backend/src/api/week.rs b/backend/src/api/week.rs index 727b84c..023305a 100644 --- a/backend/src/api/week.rs +++ b/backend/src/api/week.rs @@ -1,14 +1,71 @@ use serde::{Serialize, Deserialize}; use log::{error, info}; -use actix_web::{self, Responder}; +use actix_web::{self, Responder, web::Json, HttpResponse}; use oracle::{Connection, Result}; use crate::config::{ORACLE_PASS, ORACLE_USER, ORACLE_CON_STR}; - -pub async fn week(item: ItemData) -> impl Responder { - +#[derive(Serialize, Deserialize, Debug, Default)] +pub struct ItemData{ + net_id: String, + item_name: String, + calories: Option, + fat_g: Option, + sat_fat_g: Option, + trans_fat_g: Option, + carbs_g: Option, + fiber_g: Option, + sugar_g: Option, + protein_g: Option, + sodium_mg: Option, + potassium_mg: Option, + cholesterol_mg: Option } -fn add_item(item: ItemData) -> Result<()> { +pub async fn week(item: Json) -> impl Responder { + + let item = item.into_inner(); + match add_item(&item) { + Ok(_) => HttpResponse::Ok(), + Err(e) => { + error!("Unable to add item to table {}: {}", item.net_id, e); + HttpResponse::InternalServerError() + } + } +} + +fn add_item(item: &ItemData) -> Result<()> { + let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR)? ; + + let mut stmt = conn.statement(format!("insert into {} values (NULL, NULL, :item_name, + :calories, + :fat_g, + :sat_fat_g, + :trans_fat_g, + :carbs_g, + :fiber_g, + :sugar_g, + :protein_g, + :sodium_mg, + :potassium_mg, + :cholesterol_mg, + 0)", item.net_id).as_str()).build()?; + + stmt.execute_named(&[("item_name", &item.item_name), + ("calories", &item.calories), + ("fat_g", &item.fat_g), + ("sat_fat_g", &item.sat_fat_g), + ("trans_fat_g", &item.trans_fat_g), + ("carbs_g", &item.carbs_g), + ("fiber_g", &item.fiber_g), + ("sugar_g", &item.sugar_g), + ("protein_g", &item.protein_g), + ("sodium_mg", &item.sodium_mg), + ("potassium_mg", &item.potassium_mg), + ("cholesterol_mg", &item.cholesterol_mg)])?; + + conn.commit()?; + conn.close()?; + info!("Added item {} to table {}", item.item_name, item.net_id); + Ok(()) } diff --git a/backend/src/main.rs b/backend/src/main.rs index 80a9766..3905c03 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -59,6 +59,10 @@ async fn main() -> std::io::Result<()> { web::resource("/goal") .route(web::post().to(api::plan::plan)) ) + .service( + web::resource("/week_plan") + .route(web::post().to(api::week::week)) + ) .route("/", web::get().to(api_index)) ) .route("/", web::get().to(index))