added menu item end

This commit is contained in:
Colin McKechney
2023-04-30 20:57:26 -04:00
parent 7dd488d23f
commit 4f6bffaf17
2 changed files with 54 additions and 0 deletions

View File

@@ -22,6 +22,13 @@ pub struct ItemData{
cholesterol_mg: Option<f32>
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct MenuItems {
net_id: String,
item_list: Vec<u32>
}
pub async fn week(item: Json<ItemData>) -> impl Responder {
let item = item.into_inner();
@@ -34,6 +41,19 @@ pub async fn week(item: Json<ItemData>) -> impl Responder {
}
}
pub async fn week_meals(items: Json<MenuItems>) -> 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(())
}

View File

@@ -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))