From c76a63735adaa37815ec6550841032a00a27867d Mon Sep 17 00:00:00 2001 From: Colin McKechney Date: Sat, 29 Apr 2023 22:10:54 -0400 Subject: [PATCH] added table creation --- backend/src/api/user.rs | 29 +++++++++++++++++++++-------- backend/src/main.rs | 8 ++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/backend/src/api/user.rs b/backend/src/api/user.rs index c01e588..81d1057 100644 --- a/backend/src/api/user.rs +++ b/backend/src/api/user.rs @@ -3,7 +3,7 @@ use rand::{prelude::Rng, distributions::Alphanumeric }; use oracle::{Connection, Error}; use log::{info, warn, error}; use actix_identity::Identity; -use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse}; +use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse, cookie}; use serde::{Deserialize, Serialize}; @@ -26,7 +26,7 @@ pub struct User { #[derive(Deserialize, Serialize, Debug, Default)] pub struct NewUser { - netid: String, + net_id: String, password: String, first_name: String, last_name: String @@ -43,7 +43,7 @@ pub async fn login(request: HttpRequest, body: web::Json) -> impl Respond println!("{:?}", body); match authenticate(net_id, password) { Some(user) => { - Identity::login(&request.extensions(), net_id.into()); + let id = Identity::login(&request.extensions(), net_id.into()).unwrap(); web::Json(user); HttpResponse::Ok() }, @@ -62,10 +62,10 @@ pub async fn logout(user: Identity) -> impl Responder { pub async fn signup(request: HttpRequest, body: web::Json) -> impl Responder { let body = body.into_inner(); - match create_user(body.netid.as_str(), body.password.as_str(), body.first_name.as_str(), body.last_name.as_str()) { + match create_user(body.net_id.as_str(), body.password.as_str(), body.first_name.as_str(), body.last_name.as_str()) { Ok(_) => { - Identity::login(&request.extensions(), body.netid.clone()); - web::Json(User { id: body.netid, first_name: body.first_name, last_name: body.last_name}); + Identity::login(&request.extensions(), body.net_id.clone()).unwrap(); + web::Json(User { id: body.net_id, first_name: body.first_name, last_name: body.last_name}); HttpResponse::Ok() } Err(e) => { @@ -131,7 +131,7 @@ fn create_user(username: &str, password: &str, first_name: &str, last_name: &str info!("Creating user: {}", username); let conn = Connection::connect(SQL_USERNAME, SQL_PASSWORD, "")?; - let mut stmt = conn.statement("insert into student values(:net_id, :first_name, :last_name, :password, :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 mut hasher = Sha256::new(); @@ -145,7 +145,7 @@ fn create_user(username: &str, password: &str, first_name: &str, last_name: &str hash_string += &format!("{:x}", value); } - match stmt.execute_named(&[("net_id", &username), ("first_name", &first_name), ("last_name", &last_name), ("password", &hash_string), ("salt", &salt)]) { + match stmt.execute_named(&[("net_id", &username), ("first_name", &first_name), ("last_name", &last_name), ("pswd", &hash_string), ("salt", &salt)]) { Ok(_) => { info!("User {} successfully created", username); conn.commit()?; @@ -155,6 +155,19 @@ fn create_user(username: &str, password: &str, first_name: &str, last_name: &str conn.rollback()?; } }; + + let mut new_table = conn.statement("create table week_:netid ( item_id number(5), foreign key (item_id) references menu_item (id))").build()?; + + match new_table.execute_named(&[("net_id", &username)]) { + Ok(_) => { + info!("User {} week table created", username); + conn.commit()?; + }, + Err(_) => { + warn!("Failed to create week table for {}", username); + conn.rollback()?; + } + }; conn.close()?; Ok(()) diff --git a/backend/src/main.rs b/backend/src/main.rs index a03802f..ef0f741 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,6 +1,5 @@ -use log::{info, warn, error}; use env_logger::Env; -use actix_web::{web, get, post, web::Json, App, HttpResponse, HttpServer, Responder, middleware, cookie::Key}; +use actix_web::{web, App, HttpResponse, HttpServer, Responder, middleware, cookie::Key}; use serde::{Deserialize, Serialize}; use actix_cors::Cors; use actix_identity::IdentityMiddleware; @@ -8,8 +7,8 @@ use actix_session::{SessionMiddleware, storage::CookieSessionStore}; mod api; -static PORT: u16 = 5000; -const ALLOWED_ORIGIN: &str = "http://localhost"; +static PORT: u16 = 8000; +const ALLOWED_ORIGIN: &str = "http://localhost:8009"; #[derive(Default, Debug, Serialize, Deserialize, Clone)] @@ -36,6 +35,7 @@ async fn main() -> std::io::Result<()> { .allowed_origin(ALLOWED_ORIGIN) .allowed_methods(vec!["GET","POST","DELETE"]) .supports_credentials() + .allow_any_header() ) .wrap(IdentityMiddleware::default()) .wrap(SessionMiddleware::new(CookieSessionStore::default(), secret_key.clone()))