From 4f6bffaf17891e97714f7ca5012923c8657add7d Mon Sep 17 00:00:00 2001 From: Colin McKechney Date: Sun, 30 Apr 2023 20:57:26 -0400 Subject: [PATCH] added menu item end --- backend/src/api/week.rs | 50 +++++++++++++++++++++++++++++++++++++++++ backend/src/main.rs | 4 ++++ 2 files changed, 54 insertions(+) diff --git a/backend/src/api/week.rs b/backend/src/api/week.rs index ff8a681..359bcd5 100644 --- a/backend/src/api/week.rs +++ b/backend/src/api/week.rs @@ -22,6 +22,13 @@ pub struct ItemData{ 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(); @@ -34,6 +41,19 @@ pub async fn week(item: Json) -> impl Responder { } } +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)? ; @@ -72,3 +92,33 @@ fn add_item(item: &ItemData) -> Result<()> { 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, + 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(()) +} diff --git a/backend/src/main.rs b/backend/src/main.rs index 3905c03..3e536fc 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -63,6 +63,10 @@ async fn main() -> std::io::Result<()> { web::resource("/week_plan") .route(web::post().to(api::week::week)) ) + .service( + web::resource("/week_meals") + .route(web::post().to(api::week::week_meals)) + ) .route("/", web::get().to(api_index)) ) .route("/", web::get().to(index))