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::{
get_leaderboard,
Database,
LeaderBoardTest
};
type LeaderBoardTests = Vec<LeaderBoardTest>;
/// Returns the highest test data from each user as
/// a json array
/// Acessible from http://url/api/leaderboard
#[get("/leaderboard")]
pub fn leaderboard() -> Json<Vec<LeaderBoardTest>> {
let leaderboard: Vec<LeaderBoardTest> = get_leaderboard(0).expect("error finding user_id");
Json(leaderboard)
pub async fn leaderboard( database: &State<Database> ) -> Option<Json<LeaderBoardTests>> {
let leaderboard = match database.get_leaderboard(0).await {
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 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 = "<test>")]
pub fn create_test(test: Json<PostTest<'_>>) {
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<PostTest<'_>>, database: &State<Database>) {
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<Vec<String>> {
let mut return_list: Vec<String> = 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())

View File

@ -19,12 +19,8 @@ pub struct User<'r> {
#[post("/create_user", data = "<user>")]
pub async fn sign_up(user: Json<User<'_>>, database: &State<Database>) {
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); }
}
}