finished database code migration

This commit is contained in:
Arlo Filley 2023-09-04 17:38:37 +01:00
parent b3ed15811e
commit 5102606f51
3 changed files with 30 additions and 30 deletions

View File

@ -1,14 +1,24 @@
use rocket::serde::json::Json; use rocket::{ serde::json::Json, State, Data };
use crate::typing::sql::{ use crate::typing::sql::{
get_leaderboard, Database,
LeaderBoardTest LeaderBoardTest
}; };
type LeaderBoardTests = Vec<LeaderBoardTest>;
/// Returns the highest test data from each user as /// Returns the highest test data from each user as
/// a json array /// a json array
/// Acessible from http://url/api/leaderboard /// Acessible from http://url/api/leaderboard
#[get("/leaderboard")] #[get("/leaderboard")]
pub fn leaderboard() -> Json<Vec<LeaderBoardTest>> { pub async fn leaderboard( database: &State<Database> ) -> Option<Json<LeaderBoardTests>> {
let leaderboard: Vec<LeaderBoardTest> = get_leaderboard(0).expect("error finding user_id"); let leaderboard = match database.get_leaderboard(0).await {
Json(leaderboard) Err(why) => {
} println!("Error getting leaderboard, {why}");
return None
}
Ok(leaderboard) => { leaderboard }
};
Some(Json(leaderboard))
}

View File

@ -1,7 +1,7 @@
use crate::typing::sql::post_test; use crate::typing::sql::Database;
use rocket::serde::{ use rocket::{
Deserialize, serde::{ Deserialize, json::Json },
json::Json State
}; };
use rand::{ use rand::{
Rng, Rng,
@ -30,17 +30,11 @@ pub struct PostTest<'r> {
/// Api Route that accepts test data and posts it to the database /// Api Route that accepts test data and posts it to the database
/// Acessible from http://url/api/post_test /// Acessible from http://url/api/post_test
#[post("/post_test", data = "<test>")] #[post("/post_test", data = "<test>")]
pub fn create_test(test: Json<PostTest<'_>>) { pub async fn create_test(test: Json<PostTest<'_>>, database: &State<Database>) {
post_test( 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 {
test.test_type, Err(why) => { println!("A database error occured creating a test, {why}"); }
test.test_length, Ok(()) => { println!("Successfully created test for {}", test.user_id); }
test.test_time, }
test.test_seed,
test.quote_id,
test.wpm,
test.accuracy,
test.user_id
).expect("error in posting test to tests table");
} }
/// Returns an array of words as Json /// Returns an array of words as Json
@ -56,9 +50,9 @@ pub fn new_test() -> Json<Vec<String>> {
let mut return_list: Vec<String> = vec![]; let mut return_list: Vec<String> = vec![];
let mut rng: ThreadRng = rand::thread_rng(); let mut rng: ThreadRng = rand::thread_rng();
for _i in 0..100 { for _ in 0..100 {
let hi: u32 = rng.gen_range(0..999); let word = rng.gen_range(0..999);
return_list.push(word_vec[hi as usize].to_string()) return_list.push(word_vec[word].to_string())
} }
Json(return_list.clone()) Json(return_list.clone())

View File

@ -19,12 +19,8 @@ pub struct User<'r> {
#[post("/create_user", data = "<user>")] #[post("/create_user", data = "<user>")]
pub async fn sign_up(user: Json<User<'_>>, database: &State<Database>) { pub async fn sign_up(user: Json<User<'_>>, database: &State<Database>) {
match database.create_user( user.username, &sha256::digest(user.password) ).await { match database.create_user( user.username, &sha256::digest(user.password) ).await {
Err(why) => { Err(why) => { println!("A database error occured during signup, {why}"); }
println!("A database error occured during signup, {}", why); Ok(()) => { println!("Succesfully Signed up user {}", user.username); }
}
Ok(()) => {
println!("Succesfully Signed up user {}", user.username);
}
} }
} }