use serde::{Serialize, Deserialize}; use log::{error, info}; use actix_web::{self, Responder, web::Json, HttpResponse}; use oracle::{Connection, Result}; use crate::config::{ORACLE_PASS, ORACLE_USER, ORACLE_CON_STR}; #[derive(Serialize, Deserialize, Debug, Default)] pub struct ItemData{ net_id: String, item_name: String, amount: Option, 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 } #[derive(Serialize, Deserialize, Debug, Default)] pub struct MenuItems { net_id: String, item_list: Vec } 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() } } } pub async fn week_meals(items: Json) -> impl Responder { let items = items.into_inner(); let netid = items.net_id.clone(); match add_menu_items(items) { Ok(_) => HttpResponse::Ok(), Err(e) => { error!("Unable to add menu items to table {}: {}", netid, 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, :amount, :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(&[ ("amount", &item.amount), ("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(()) } fn add_menu_items(items: MenuItems) -> Result<()> { let conn = Connection::connect(ORACLE_USER,ORACLE_PASS, ORACLE_CON_STR)?; let mut stmt = conn.statement(format!("insert into {} values ( :item_id, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1)", items.net_id).as_str()).build()?; for item in items.item_list { stmt.execute_named(&[("item_id",&item)])?; } conn.commit()?; conn.close()?; info!("inserted menu items into week table {}", items.net_id); Ok(()) }