diff --git a/backend/src/api/eatery.rs b/backend/src/api/eatery.rs index 7bb63cd..8aba59a 100644 --- a/backend/src/api/eatery.rs +++ b/backend/src/api/eatery.rs @@ -2,9 +2,7 @@ use serde::{Serialize, Deserialize}; use actix_web::{web::Path, Responder, HttpResponse, web::Json}; use oracle::Connection; use log::error; - -const ORACLE_USER: &str = "timmy"; -const ORACLE_PASS: &str = "timmy"; +use crate::config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR}; #[derive(Serialize, Deserialize, Debug, Default)] pub struct FoodItem { @@ -43,7 +41,7 @@ pub async fn menu(eatery: Path) -> Json> { fn grab_rows(eatery: String) -> oracle::Result> { - let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, "")?; + let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR)?; let mut stmt = conn.statement("select * from menu_item natural join nutrition_info where eatery_id = :1").build()?; diff --git a/backend/src/api/mod.rs b/backend/src/api/mod.rs index 80bcc35..e575e43 100644 --- a/backend/src/api/mod.rs +++ b/backend/src/api/mod.rs @@ -1,3 +1,4 @@ pub mod user; pub mod eatery; +pub mod plan; diff --git a/backend/src/api/plan.rs b/backend/src/api/plan.rs new file mode 100644 index 0000000..0740074 --- /dev/null +++ b/backend/src/api/plan.rs @@ -0,0 +1,55 @@ +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)] +struct PlanData{ + net_id: String, + total_cal: f32, + total_fat: f32, + total_sat_fat: f32, + total_trans_fat: f32, + total_carbs: f32, + total_fiber: f32, + total_sugar: f32, + total_protein: f32, + total_sodium: f32, + total_potassium: f32, + total_cholesterol: f32 +} + +pub async fn plan(body: Json) -> 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)?; + + let mut stmt = conn.statement("insert into table 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()?; + + stmt.execute_named(&[("net_id", &plan.net_id), ("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_sugar", &plan.total_sugar), + ("total_protein", &plan.total_protein), + ("total_potassium", &plan.total_potassium), + ("total_cholesterol", &plan.total_cholesterol)])?; + + info!("Created new plan for user: {}", plan.net_id); + Ok(()) + +} + diff --git a/backend/src/api/user.rs b/backend/src/api/user.rs index 0cffdee..11b348a 100644 --- a/backend/src/api/user.rs +++ b/backend/src/api/user.rs @@ -5,10 +5,8 @@ use log::{info, warn, error}; use actix_identity::Identity; use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse, cookie}; use serde::{Deserialize, Serialize}; +use crate::config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR}; - -static SQL_USERNAME: &str = "group09_user"; -static SQL_PASSWORD: &str = "group09_user"; static SALT_LEN: usize = 16; #[derive(Deserialize, Serialize, Debug, Default)] @@ -80,7 +78,7 @@ fn authenticate(username: &str, password: &str) -> Option { info!("Authenticating user: {}", username); - let conn = match Connection::connect(SQL_USERNAME,SQL_PASSWORD, ""){ + let conn = match Connection::connect(ORACLE_USER,ORACLE_PASS, ORACLE_CON_STR){ Ok(c) => c, Err(e) => { error!("unable to open connection to server: {}", e); @@ -130,7 +128,7 @@ fn authenticate(username: &str, password: &str) -> Option { fn create_user(username: &str, password: &str, first_name: &str, last_name: &str) -> Result<(), Error> { info!("Creating user: {}", username); - let conn = Connection::connect(SQL_USERNAME, SQL_PASSWORD, "")?; + let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR)?; let mut stmt = conn.statement("insert into student values(:net_id, :first_name, :last_name, :pswd, :salt)").build()?; let salt: String = rand::thread_rng().sample_iter(&Alphanumeric).take(SALT_LEN).map(char::from).collect(); diff --git a/backend/src/config.rs b/backend/src/config.rs new file mode 100644 index 0000000..3e8e3db --- /dev/null +++ b/backend/src/config.rs @@ -0,0 +1,4 @@ +pub const ORACLE_USER: &str = "timmy"; +pub const ORACLE_PASS: &str = "timmy"; +pub const ORACLE_CON_STR: &str = ""; + diff --git a/backend/src/main.rs b/backend/src/main.rs index ea3061d..80a9766 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -6,6 +6,7 @@ use actix_identity::IdentityMiddleware; use actix_session::{SessionMiddleware, storage::CookieSessionStore}; mod api; +mod config; static PORT: u16 = 8000; const ALLOWED_ORIGIN: &str = "http://localhost:8009"; @@ -54,6 +55,10 @@ async fn main() -> std::io::Result<()> { web::resource("/eatery/{eatery_id}") .route(web::get().to(api::eatery::menu)) ) + .service( + web::resource("/goal") + .route(web::post().to(api::plan::plan)) + ) .route("/", web::get().to(api_index)) ) .route("/", web::get().to(index))