added config + plan
This commit is contained in:
@@ -2,9 +2,7 @@ use serde::{Serialize, Deserialize};
|
|||||||
use actix_web::{web::Path, Responder, HttpResponse, web::Json};
|
use actix_web::{web::Path, Responder, HttpResponse, web::Json};
|
||||||
use oracle::Connection;
|
use oracle::Connection;
|
||||||
use log::error;
|
use log::error;
|
||||||
|
use crate::config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR};
|
||||||
const ORACLE_USER: &str = "timmy";
|
|
||||||
const ORACLE_PASS: &str = "timmy";
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||||
pub struct FoodItem {
|
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>> {
|
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()?;
|
let mut stmt = conn.statement("select * from menu_item natural join nutrition_info where eatery_id = :1").build()?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
pub mod user;
|
pub mod user;
|
||||||
pub mod eatery;
|
pub mod eatery;
|
||||||
|
pub mod plan;
|
||||||
|
|
||||||
|
|||||||
55
backend/src/api/plan.rs
Normal file
55
backend/src/api/plan.rs
Normal 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(())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -5,10 +5,8 @@ use log::{info, warn, error};
|
|||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse, cookie};
|
use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse, cookie};
|
||||||
use serde::{Deserialize, Serialize};
|
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;
|
static SALT_LEN: usize = 16;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||||
@@ -80,7 +78,7 @@ fn authenticate(username: &str, password: &str) -> Option<User> {
|
|||||||
|
|
||||||
info!("Authenticating user: {}", username);
|
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,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to open connection to server: {}", 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> {
|
fn create_user(username: &str, password: &str, first_name: &str, last_name: &str) -> Result<(), Error> {
|
||||||
|
|
||||||
info!("Creating user: {}", username);
|
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 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();
|
let salt: String = rand::thread_rng().sample_iter(&Alphanumeric).take(SALT_LEN).map(char::from).collect();
|
||||||
|
|||||||
4
backend/src/config.rs
Normal file
4
backend/src/config.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub const ORACLE_USER: &str = "timmy";
|
||||||
|
pub const ORACLE_PASS: &str = "timmy";
|
||||||
|
pub const ORACLE_CON_STR: &str = "";
|
||||||
|
|
||||||
@@ -6,6 +6,7 @@ use actix_identity::IdentityMiddleware;
|
|||||||
use actix_session::{SessionMiddleware, storage::CookieSessionStore};
|
use actix_session::{SessionMiddleware, storage::CookieSessionStore};
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
mod config;
|
||||||
|
|
||||||
static PORT: u16 = 8000;
|
static PORT: u16 = 8000;
|
||||||
const ALLOWED_ORIGIN: &str = "http://localhost:8009";
|
const ALLOWED_ORIGIN: &str = "http://localhost:8009";
|
||||||
@@ -54,6 +55,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
web::resource("/eatery/{eatery_id}")
|
web::resource("/eatery/{eatery_id}")
|
||||||
.route(web::get().to(api::eatery::menu))
|
.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(api_index))
|
||||||
)
|
)
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
|
|||||||
Reference in New Issue
Block a user