diff --git a/backend/src/api/plan.rs b/backend/src/api/plan.rs index 1173b80..072b075 100644 --- a/backend/src/api/plan.rs +++ b/backend/src/api/plan.rs @@ -6,19 +6,19 @@ use crate::config::{ORACLE_PASS, ORACLE_USER, ORACLE_CON_STR}; #[derive(Deserialize, Serialize, Debug, Default)] pub struct PlanData{ - net_id: String, - week_date: String, - total_cal: Option, - total_fat: Option, - total_sat_fat: Option, - total_trans_fat: Option, - total_carbs: Option, - total_fiber: Option, - total_sugar: Option, - total_protein: Option, - total_sodium: Option, - total_potassium: Option, - total_cholesterol: Option + pub net_id: String, + pub week_date: String, + pub total_cal: Option, + pub total_fat: Option, + pub total_sat_fat: Option, + pub total_trans_fat: Option, + pub total_carbs: Option, + pub total_fiber: Option, + pub total_sugar: Option, + pub total_protein: Option, + pub total_sodium: Option, + pub total_potassium: Option, + pub total_cholesterol: Option } pub async fn plan(body: Json) -> impl Responder { diff --git a/backend/src/api/result.rs b/backend/src/api/result.rs index fcc79e4..5bb308f 100644 --- a/backend/src/api/result.rs +++ b/backend/src/api/result.rs @@ -1,5 +1,5 @@ use log::error; -use actix_web::{web::Json, web::Path}; +use actix_web::{web::Json, web::Path, Responder, HttpResponse}; use oracle::{Connection, Result}; use crate::{api::plan::PlanData, config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR}}; @@ -17,6 +17,18 @@ pub async fn all_result(net_id: Path) -> Json> { } } +pub async fn add_result(result: Json) -> impl Responder { + let result = result.into_inner(); + + match push_result(&result){ + Ok(_) => HttpResponse::Ok(), + Err(e) => { + error!("failed to add result for user {}: {}", result.net_id, e); + HttpResponse::InternalServerError() + } + } +} + fn get_result(net_id: &str) -> Result> { @@ -46,3 +58,27 @@ fn get_result(net_id: &str) -> Result> { Ok(row_vec) } + + +fn push_result(plan: &PlanData) -> Result<()> { + let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR)?; + let mut stmt = conn.statement("insert into result values(:net_id, :week_date, :total_cal, :total_fat, :total_sat_fat, :total_trans_fat, :total_carbs, :total_fiber, :total_sugar, :total_protein, :total_sodium, :total_potassium, :total_cholesterol)").build()?; + + stmt.execute_named(&[ + ("net_id", &plan.net_id), + ("week_date", &plan.week_date), + ("total_cal", &plan.total_cal), + ("total_fat", &plan.total_fat), + ("total_sat_fat", &plan.total_sat_fat), + ("total_trans_fat", &plan.total_trans_fat), + ("total_carbs", &plan.total_carbs), + ("total_fiber", &plan.total_fiber), + ("total_sugar", &plan.total_sugar), + ("total_protein", &plan.total_protein), + ("total_sodium", &plan.total_sodium), + ("total_potassium", &plan.total_potassium), + ("total_cholesterol", &plan.total_cholesterol)])?; + + Ok(()) + +} diff --git a/backend/src/main.rs b/backend/src/main.rs index 6bf9992..98009df 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -92,6 +92,10 @@ async fn main() -> std::io::Result<()> { web::resource("result/{net_id}") .route(web::get().to(api::result::all_result)) ) + .service( + web::resource("add_result/") + .route(web::post().to(api::result::add_result)) + ) .route("/", web::get().to(api_index)) ) .route("/", web::get().to(index))