Files
advanced_db/backend/src/api/plan.rs

60 lines
1.9 KiB
Rust
Raw Normal View History

2023-04-30 17:33:47 -04:00
use serde::{Serialize, Deserialize};
use actix_web::{web::Json, Responder, HttpResponse};
use oracle::{Connection, Result};
use log::{error, info};
use crate::config::{ORACLE_PASS, ORACLE_USER, ORACLE_CON_STR};
#[derive(Deserialize, Serialize, Debug, Default)]
2023-04-30 22:06:09 +00:00
pub struct PlanData{
2023-04-30 17:33:47 -04:00
net_id: String,
2023-04-30 22:06:09 +00:00
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>
2023-04-30 17:33:47 -04:00
}
pub async fn plan(body: Json<PlanData>) -> impl Responder {
let body = body.into_inner();
match create_plan(body) {
Ok(_) => HttpResponse::Ok(),
Err(e) => {
error!("failed to create plan {}", e);
HttpResponse::InternalServerError()
}
}
}
fn create_plan(plan: PlanData) -> Result<()> {
let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR)?;
2023-04-30 22:35:58 +00:00
let mut stmt = conn.statement("insert into goal values(:net_id, :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()?;
2023-04-30 17:33:47 -04:00
2023-04-30 22:35:58 +00:00
stmt.execute_named(&[
("net_id", &plan.net_id),
("total_cal", &plan.total_cal),
2023-04-30 17:33:47 -04:00
("total_fat", &plan.total_fat),
("total_sat_fat", &plan.total_sat_fat),
("total_trans_fat", &plan.total_trans_fat),
("total_carbs", &plan.total_carbs),
2023-04-30 22:35:58 +00:00
("total_fiber", &plan.total_fiber),
2023-04-30 17:33:47 -04:00
("total_sugar", &plan.total_sugar),
("total_protein", &plan.total_protein),
2023-04-30 22:35:58 +00:00
("total_sodium", &plan.total_sodium),
2023-04-30 17:33:47 -04:00
("total_potassium", &plan.total_potassium),
("total_cholesterol", &plan.total_cholesterol)])?;
info!("Created new plan for user: {}", plan.net_id);
Ok(())
}