add to result and pub plan

This commit is contained in:
Colin McKechney
2023-05-02 18:08:03 -04:00
parent 1517e72833
commit c52bc3ce7e
3 changed files with 54 additions and 14 deletions

View File

@@ -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<f32>,
total_fat: Option<f32>,
total_sat_fat: Option<f32>,
total_trans_fat: Option<f32>,
total_carbs: Option<f32>,
total_fiber: Option<f32>,
total_sugar: Option<f32>,
total_protein: Option<f32>,
total_sodium: Option<f32>,
total_potassium: Option<f32>,
total_cholesterol: Option<f32>
pub net_id: String,
pub week_date: String,
pub total_cal: Option<f32>,
pub total_fat: Option<f32>,
pub total_sat_fat: Option<f32>,
pub total_trans_fat: Option<f32>,
pub total_carbs: Option<f32>,
pub total_fiber: Option<f32>,
pub total_sugar: Option<f32>,
pub total_protein: Option<f32>,
pub total_sodium: Option<f32>,
pub total_potassium: Option<f32>,
pub total_cholesterol: Option<f32>
}
pub async fn plan(body: Json<PlanData>) -> impl Responder {

View File

@@ -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<String>) -> Json<Vec<PlanData>> {
}
}
pub async fn add_result(result: Json<PlanData>) -> 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<Vec<PlanData>> {
@@ -46,3 +58,27 @@ fn get_result(net_id: &str) -> Result<Vec<PlanData>> {
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(())
}