From 5102606f518c6d4f1da830a6f62b7833aca103df Mon Sep 17 00:00:00 2001 From: ArloFilley Date: Mon, 4 Sep 2023 17:38:37 +0100 Subject: [PATCH] finished database code migration --- src/typing/leaderboard.rs | 22 ++++++++++++++++------ src/typing/test.rs | 30 ++++++++++++------------------ src/typing/user.rs | 8 ++------ 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/typing/leaderboard.rs b/src/typing/leaderboard.rs index ec9cffb..b9232d1 100644 --- a/src/typing/leaderboard.rs +++ b/src/typing/leaderboard.rs @@ -1,14 +1,24 @@ -use rocket::serde::json::Json; +use rocket::{ serde::json::Json, State, Data }; use crate::typing::sql::{ - get_leaderboard, + Database, LeaderBoardTest }; +type LeaderBoardTests = Vec; + /// Returns the highest test data from each user as /// a json array /// Acessible from http://url/api/leaderboard #[get("/leaderboard")] -pub fn leaderboard() -> Json> { - let leaderboard: Vec = get_leaderboard(0).expect("error finding user_id"); - Json(leaderboard) -} \ No newline at end of file +pub async fn leaderboard( database: &State ) -> Option> { + let leaderboard = match database.get_leaderboard(0).await { + Err(why) => { + println!("Error getting leaderboard, {why}"); + return None + } + Ok(leaderboard) => { leaderboard } + }; + + Some(Json(leaderboard)) +} + diff --git a/src/typing/test.rs b/src/typing/test.rs index 19047f2..7f339c7 100644 --- a/src/typing/test.rs +++ b/src/typing/test.rs @@ -1,7 +1,7 @@ -use crate::typing::sql::post_test; -use rocket::serde::{ - Deserialize, - json::Json +use crate::typing::sql::Database; +use rocket::{ + serde::{ Deserialize, json::Json }, + State }; use rand::{ Rng, @@ -30,17 +30,11 @@ pub struct PostTest<'r> { /// Api Route that accepts test data and posts it to the database /// Acessible from http://url/api/post_test #[post("/post_test", data = "")] -pub fn create_test(test: Json>) { - post_test( - test.test_type, - test.test_length, - test.test_time, - test.test_seed, - test.quote_id, - test.wpm, - test.accuracy, - test.user_id - ).expect("error in posting test to tests table"); +pub async fn create_test(test: Json>, database: &State) { + match database.create_test(test.test_type, test.test_length, test.test_time, test.test_seed, test.quote_id, test.wpm, test.accuracy, test.user_id).await { + Err(why) => { println!("A database error occured creating a test, {why}"); } + Ok(()) => { println!("Successfully created test for {}", test.user_id); } + } } /// Returns an array of words as Json @@ -56,9 +50,9 @@ pub fn new_test() -> Json> { let mut return_list: Vec = vec![]; let mut rng: ThreadRng = rand::thread_rng(); - for _i in 0..100 { - let hi: u32 = rng.gen_range(0..999); - return_list.push(word_vec[hi as usize].to_string()) + for _ in 0..100 { + let word = rng.gen_range(0..999); + return_list.push(word_vec[word].to_string()) } Json(return_list.clone()) diff --git a/src/typing/user.rs b/src/typing/user.rs index c1d4d1d..7c998f7 100644 --- a/src/typing/user.rs +++ b/src/typing/user.rs @@ -19,12 +19,8 @@ pub struct User<'r> { #[post("/create_user", data = "")] pub async fn sign_up(user: Json>, database: &State) { match database.create_user( user.username, &sha256::digest(user.password) ).await { - Err(why) => { - println!("A database error occured during signup, {}", why); - } - Ok(()) => { - println!("Succesfully Signed up user {}", user.username); - } + Err(why) => { println!("A database error occured during signup, {why}"); } + Ok(()) => { println!("Succesfully Signed up user {}", user.username); } } }