added config + plan

This commit is contained in:
Colin McKechney
2023-04-30 17:33:47 -04:00
parent 5117a3318f
commit f009dd7166
6 changed files with 70 additions and 9 deletions

View File

@@ -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<String>) -> Json<Vec<FoodItem>> {
fn grab_rows(eatery: String) -> oracle::Result<Vec<FoodItem>> {
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()?;

View File

@@ -1,3 +1,4 @@
pub mod user;
pub mod eatery;
pub mod plan;

55
backend/src/api/plan.rs Normal file
View File

@@ -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<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)?;
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(())
}

View File

@@ -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<User> {
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<User> {
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();