From bf0030b7f174e07b5b076f09b46b8fbc15173e79 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 5 Mar 2023 09:08:51 -0400 Subject: [PATCH] Updated user service to use u32 instead of i32 and updated the dev-dtos lib --- .DS_Store | Bin 6148 -> 6148 bytes Cargo.toml | 4 ++-- migrations/1_user.sql | 2 +- migrations/2_token.sql | 4 ++-- src/dao/main_dao.rs | 14 +++++++------- src/dao/token_dao.rs | 2 +- src/dao/user_dao.rs | 2 +- src/main.rs | 4 ++-- src/routes/main_router.rs | 3 +-- src/routes/user_routes.rs | 10 +++++----- 10 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.DS_Store b/.DS_Store index 281020a0228f2ec50465b740561ff01eabba6ee2..0eb346e89c01751e8a06ad079a242fc19ed5cb92 100644 GIT binary patch delta 210 zcmZoMXfc=|#>B)qu~2NI-d)DY+$=e~Yz!$3sSE`SISly>m6Lr~5c|C?=h78Y~{N$vZ{3Hej1_2;m55)8Sg8`7mz`(`e0@Ml7Q^b(Q zkO{OS6;%(TEQX%k$yF>zH%GIrVcghI!nB#4gP#NF;?05_-B!ku~2NI-d)B;EKHNRnQ|t}GoPquV<=%rWGG@tXGmoz@yy9jPRhwo zVqjnpU|?WM2hw`~!2rl&U_eouR9;*FRl6gpASbi9#K7PhBNH) -> Result{ let db_url = match env_vars.get("DATABASE_URL") { @@ -9,14 +9,14 @@ pub async fn start_database_connection(env_vars: &HashMap) -> Re None => panic!("DATABASE_URL env var not found") }; let formatted_db_url = &db_url; - let mut options = MySqlConnectOptions::from_str(formatted_db_url)?; - options.log_slow_statements(LevelFilter::Warn, Duration::from_secs(1)) - .log_statements(LevelFilter::Trace); + let options = MySqlConnectOptions::from_str(formatted_db_url)?; + //options.log_slow_statements(LevelFilter::Warn, Duration::from_secs(1)) + //.log_statements(LevelFilter::Trace); MySqlPoolOptions::new().connect_with(options).await } pub async fn run_all_migrations(conn: &MySqlPool){ match sqlx::migrate!("./migrations").run(conn).await { - Ok(()) => {info!("Successfully ran migrations.")}, + Ok(()) => {println!("Successfully ran migrations.")}, Err(error) => {panic!("{error}")} } } \ No newline at end of file diff --git a/src/dao/token_dao.rs b/src/dao/token_dao.rs index 8062ebf..7bc3f92 100644 --- a/src/dao/token_dao.rs +++ b/src/dao/token_dao.rs @@ -5,7 +5,7 @@ use sqlx::{mysql::MySqlQueryResult}; pub async fn insert_token(conn: &MySqlPool, token: &Token) -> Result { sqlx::query_file!("sql/schema/token/insert.sql", token.user_id, token.auth_token, token.refresh_token).execute(conn).await } -pub async fn get_tokens_with_user_id(conn: &MySqlPool, user_id: &i32) -> Result, sqlx::Error> { +pub async fn get_tokens_with_user_id(conn: &MySqlPool, user_id: &u32) -> Result, sqlx::Error> { sqlx::query_file_as!(Token, "sql/schema/token/find_with_user_id.sql", user_id).fetch_all(conn).await } pub async fn update_token_with_id(conn: &MySqlPool, token: &Token ) -> Result { diff --git a/src/dao/user_dao.rs b/src/dao/user_dao.rs index 0486341..8662523 100644 --- a/src/dao/user_dao.rs +++ b/src/dao/user_dao.rs @@ -9,6 +9,6 @@ pub async fn insert_user(conn: &MySqlPool, user_to_insert: &User) -> Result Result{ sqlx::query_file_as!(User, "sql/schema/user/find_with_credential.sql", credential, credential_type, app).fetch_one(conn).await } -pub async fn find_user_by_id(conn: &MySqlPool, id: &i32) -> Result { +pub async fn find_user_by_id(conn: &MySqlPool, id: &u32) -> Result { sqlx::query_file_as!(User, "sql/schema/user/find_with_id.sql", id).fetch_one(conn).await } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a888ba4..16b1eb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod routes; mod service; mod util; mod dto; mod validation; mod resources; -use actix_web_utils::utils::logger_util::{self}; +//use actix_web_utils::utils::logger_util::{self}; use r#do::shared_state::SharedStateObj; use util::env_util; use routes::main_router::{start_all_routes, after_startup_fn}; @@ -14,7 +14,7 @@ use dao::{main_dao::{self, run_all_migrations}}; #[tokio::main] async fn main(){ - logger_util::init_logger_custom(2).expect("LOGGER FAILED TO LOAD"); + //logger_util::init_logger_custom(2).expect("LOGGER FAILED TO LOAD"); // Retrieve env variables and send to services that need them. let env_vars = env_util::get_dot_env_map(); diff --git a/src/routes/main_router.rs b/src/routes/main_router.rs index eeb1bd5..9a14b79 100644 --- a/src/routes/main_router.rs +++ b/src/routes/main_router.rs @@ -1,13 +1,12 @@ use std::sync::{Mutex, Arc}; use actix_web::{HttpServer, App, web, middleware::Logger}; -use log::info; use crate::{r#do::shared_state::SharedStateObj}; use super::user_routes; // This function is to be used in case code is meant to be run after server startup pub fn after_startup_fn() { - info!("Started server.") + println!("Started server.") } pub async fn start_all_routes(after_startup_fn_call: &dyn Fn(), state: SharedStateObj) diff --git a/src/routes/user_routes.rs b/src/routes/user_routes.rs index 08307f0..903934b 100644 --- a/src/routes/user_routes.rs +++ b/src/routes/user_routes.rs @@ -42,7 +42,7 @@ pub async fn create_user(incoming_user: web::Json, db_conn: // Insert user in DB match insert_user(&db_conn, &user_to_insert).await{ Ok(resultrs) => { - user_to_insert.id = resultrs.last_insert_id() as i32; + user_to_insert.id = resultrs.last_insert_id() as u32; }, Err(error) => { println!("Error while inserting user in database from create_user method. Log: {}", error); @@ -59,7 +59,7 @@ pub async fn create_user(incoming_user: web::Json, db_conn: // Insert token in DB match insert_token(&db_conn, &token_to_insert).await{ - Ok(resultrs) => {token_to_insert.id = resultrs.last_insert_id() as i32}, + Ok(resultrs) => {token_to_insert.id = resultrs.last_insert_id() as u32}, Err(_e) => {return HttpResponse::InternalServerError().finish()} } @@ -113,7 +113,7 @@ pub async fn authenticate_user_with_password(incoming_user: web::Json {token_to_insert.id = resultrs.last_insert_id() as i32}, + Ok(resultrs) => {token_to_insert.id = resultrs.last_insert_id() as u32}, Err(_) => { message_resources.push(MessageResourceDto::new_from_error_message(ERROR_CREATING_TOKEN)); return HttpResponse::InternalServerError().json(message_resources) @@ -126,7 +126,7 @@ pub async fn authenticate_user_with_password(incoming_user: web::Json, db_conn: Data>) -> HttpResponse{ +pub async fn authenticate_user_with_auth_token(request: HttpRequest, user_id: web::Path, db_conn: Data>) -> HttpResponse{ let mut message_resources: Vec = Vec::new(); let headers = request.headers(); let auth_token = match headers.get("auth-token") { @@ -181,7 +181,7 @@ pub async fn authenticate_user_with_auth_token(request: HttpRequest, user_id: we #[patch("/user/refresh/{user_id}")] -pub async fn refresh_auth_token(request: HttpRequest, user_id: web::Path, db_conn: Data>) -> HttpResponse{ +pub async fn refresh_auth_token(request: HttpRequest, user_id: web::Path, db_conn: Data>) -> HttpResponse{ let mut message_resources: Vec = Vec::new(); let headers = request.headers(); let refresh_token = match headers.get("refresh-token") {