created api routes for signup login and getting user tests
This commit is contained in:
parent
9f2a5641db
commit
d53fe82cc7
@ -14,6 +14,7 @@ use rocket::{
|
|||||||
json::Json
|
json::Json
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
use crate::sql::Test;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn test() -> String {
|
fn test() -> String {
|
||||||
@ -80,10 +81,16 @@ fn login(username: &str, password: &str) -> String {
|
|||||||
user_id.to_string()
|
user_id.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/get_user_tests/<user_id>")]
|
||||||
|
fn get_user_tests(user_id: u32) -> Json<Vec<Test>> {
|
||||||
|
let tests = sql::get_user_tests(user_id).expect("error finding user_id");
|
||||||
|
Json(tests)
|
||||||
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
fn rocket() -> Rocket<Build> {
|
fn rocket() -> Rocket<Build> {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.mount("/test", routes![test]) // testing only, should return "Hello world"
|
.mount("/test", routes![test]) // testing only, should return "Hello world"
|
||||||
.mount("/api", routes![post_test, create_user, login])
|
.mount("/api", routes![post_test, create_user, login, get_user_tests])
|
||||||
.mount("/typing", FileServer::from(relative!("website"))) // hosts the fileserver
|
.mount("/typing", FileServer::from(relative!("website"))) // hosts the fileserver
|
||||||
}
|
}
|
46
src/sql.rs
46
src/sql.rs
@ -1,4 +1,5 @@
|
|||||||
use rusqlite::{Connection, Result};
|
use rusqlite::{Connection, Result};
|
||||||
|
use rocket::serde::Serialize;
|
||||||
|
|
||||||
fn get_connection() -> rusqlite::Connection {
|
fn get_connection() -> rusqlite::Connection {
|
||||||
Connection::open("database/database.sqlite")
|
Connection::open("database/database.sqlite")
|
||||||
@ -144,3 +145,48 @@ pub fn find_user(
|
|||||||
|
|
||||||
Ok(user_id)
|
Ok(user_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(crate = "rocket::serde")]
|
||||||
|
pub struct Test {
|
||||||
|
test_type: String,
|
||||||
|
test_length: u32,
|
||||||
|
test_time: u32,
|
||||||
|
test_seed: i64,
|
||||||
|
quote_id: i32,
|
||||||
|
wpm: u8,
|
||||||
|
accuracy: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_user_tests(
|
||||||
|
user_id: u32
|
||||||
|
) -> Result<Vec<Test>> {
|
||||||
|
let connection = get_connection();
|
||||||
|
let mut statement = connection.prepare(
|
||||||
|
"SELECT test_type, test_length, test_time, test_seed, quote_id, wpm, accuracy
|
||||||
|
FROM tests
|
||||||
|
WHERE user_id=:user_id",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut user_id: u32 = 0;
|
||||||
|
|
||||||
|
let test_iter = statement
|
||||||
|
.query_map(&[(":user_id", &user_id.to_string())], |row| {
|
||||||
|
Ok( Test {
|
||||||
|
test_type: row.get(0)?,
|
||||||
|
test_length: row.get(1)?,
|
||||||
|
test_time: row.get(2)?,
|
||||||
|
test_seed: row.get(3)?,
|
||||||
|
quote_id: row.get(4)?,
|
||||||
|
wpm: row.get(5)?,
|
||||||
|
accuracy: row.get(6)?
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let mut tests: Vec<Test> = vec![];
|
||||||
|
for test in test_iter {
|
||||||
|
tests.push(test.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(tests)
|
||||||
|
}
|
@ -54,7 +54,7 @@ class API {
|
|||||||
* Validates all the parameters used for the postTest function which it then calls
|
* Validates all the parameters used for the postTest function which it then calls
|
||||||
*/
|
*/
|
||||||
validateTest() {
|
validateTest() {
|
||||||
const test = screenManager.textbox.getLetters();
|
const test = screenManager.screen.textbox.getLetters();
|
||||||
const testType = "words";
|
const testType = "words";
|
||||||
let testLength = test.length;
|
let testLength = test.length;
|
||||||
let testTime = screenManager.timer.getTime();
|
let testTime = screenManager.timer.getTime();
|
||||||
@ -62,7 +62,7 @@ class API {
|
|||||||
const quoteId = 0;
|
const quoteId = 0;
|
||||||
let wpm;
|
let wpm;
|
||||||
const accuracy = 0;
|
const accuracy = 0;
|
||||||
const userId = 0;
|
const userId = Number(user.userId);
|
||||||
|
|
||||||
// this is the wpm calculation factoring in the time of test
|
// this is the wpm calculation factoring in the time of test
|
||||||
// it assumes that all words are 5 characters long because on average
|
// it assumes that all words are 5 characters long because on average
|
||||||
@ -184,12 +184,14 @@ class API {
|
|||||||
xhr.send(
|
xhr.send(
|
||||||
JSON.stringify(user)
|
JSON.stringify(user)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.login(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
login(pUsername, pPassword) {
|
login(pUsername, pPassword) {
|
||||||
if (localStorage.getItem("userId") === null) {
|
if (localStorage.userId === null || localStorage.userId === 0 || localStorage.userId === undefined) {
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', `${this.url}/login/${pUsername}/${pPassword}`);
|
xhr.open('GET', `${this.url}login/${pUsername}/${pPassword}`);
|
||||||
xhr.send();
|
xhr.send();
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
user.userId = Number(xhr.response);
|
user.userId = Number(xhr.response);
|
||||||
@ -199,11 +201,16 @@ class API {
|
|||||||
localStorage.setItem("username", pUsername);
|
localStorage.setItem("username", pUsername);
|
||||||
localStorage.setItem("password", pPassword);
|
localStorage.setItem("password", pPassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
} else {
|
} else if (localStorage.userId > 0) {
|
||||||
this.userId = localStorage.getItem("userId");
|
user.userId = localStorage.userId;
|
||||||
this.username = localStorage.getItem("username");
|
user.username = localStorage.username;
|
||||||
|
user.password = localStorage.password;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logout() {
|
||||||
|
user = new User();
|
||||||
|
localStorage.clear();
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
class User {
|
class User {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.username;
|
this.username = "not logged in";
|
||||||
this.password;
|
this.password = "there";
|
||||||
this.userId;
|
this.userId = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,6 +23,7 @@
|
|||||||
<script src="./screens/testscreen.js"></script>
|
<script src="./screens/testscreen.js"></script>
|
||||||
<script src="./screens/endscreen.js"></script>
|
<script src="./screens/endscreen.js"></script>
|
||||||
<script src="./screens/signUpScreen.js"></script>
|
<script src="./screens/signUpScreen.js"></script>
|
||||||
|
<script src="./screens/loginscreen.js"></script>
|
||||||
|
|
||||||
<!-- API Script Files -->
|
<!-- API Script Files -->
|
||||||
<script src="./api/api.js"></script>
|
<script src="./api/api.js"></script>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
let canvas, api, screenManager;
|
let canvas, api, screenManager, user;
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
// Creating the canvas
|
// Creating the canvas
|
||||||
@ -11,6 +11,10 @@ function setup() {
|
|||||||
api = new API();
|
api = new API();
|
||||||
screenManager = new ScreenManager();
|
screenManager = new ScreenManager();
|
||||||
screenManager.setScreen(new StartScreen());
|
screenManager.setScreen(new StartScreen());
|
||||||
|
user = new User();
|
||||||
|
|
||||||
|
// will log the user in if there details are in local storage
|
||||||
|
api.login();
|
||||||
}
|
}
|
||||||
|
|
||||||
// this function is called once per frame and draws all other elements
|
// this function is called once per frame and draws all other elements
|
||||||
|
@ -10,4 +10,8 @@ class EndScreen {
|
|||||||
fill(0);
|
fill(0);
|
||||||
text("Test Complete\nPress enter to start another test", 0, 0, windowWidth - 100, windowHeight - 100);
|
text("Test Complete\nPress enter to start another test", 0, 0, windowWidth - 100, windowHeight - 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
letterTyped() {
|
||||||
|
screenManager.setScreen(new StartScreen());
|
||||||
|
}
|
||||||
}
|
}
|
98
website/screens/loginscreen.js
Normal file
98
website/screens/loginscreen.js
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
class LoginScreen {
|
||||||
|
constructor() {
|
||||||
|
this.textboxes = [
|
||||||
|
new Textbox(
|
||||||
|
120,250,
|
||||||
|
500,100,
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
"#000",
|
||||||
|
false, "#000",
|
||||||
|
"#000"
|
||||||
|
),
|
||||||
|
|
||||||
|
new Textbox(
|
||||||
|
120,400,
|
||||||
|
500,100,
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
"#000",
|
||||||
|
false,"000",
|
||||||
|
"#000"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
this.buttons = [
|
||||||
|
new Button(
|
||||||
|
100,200,
|
||||||
|
500,100,
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
"#000",
|
||||||
|
false,"#000",
|
||||||
|
"#fff",""
|
||||||
|
),
|
||||||
|
|
||||||
|
new Button(
|
||||||
|
100,350,
|
||||||
|
500,100,
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
"#000",
|
||||||
|
false,"#000",
|
||||||
|
"#fff",""
|
||||||
|
),
|
||||||
|
|
||||||
|
new Button(
|
||||||
|
700,300,
|
||||||
|
100,50,
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
"#000",
|
||||||
|
false,"#000",
|
||||||
|
"#00ff00","Login"
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
this.activeTextBox = 0
|
||||||
|
// keeps track of which textbox the user last clicked on
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the SignUpScreen class with all
|
||||||
|
* appropriate elements
|
||||||
|
*/
|
||||||
|
draw() {
|
||||||
|
background("#eeeee4");
|
||||||
|
for (let i = 0; i < this.buttons.length; i++) {
|
||||||
|
this.buttons[i].draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < this.textboxes.length; i++) {
|
||||||
|
this.textboxes[i].draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
text("Username", 110, 175);
|
||||||
|
text("Password", 110, 325);
|
||||||
|
|
||||||
|
if (this.buttons[0].isPressed()) {
|
||||||
|
this.activeTextBox=0;
|
||||||
|
} else if (this.buttons[1].isPressed()) {
|
||||||
|
this.activeTextBox=1;
|
||||||
|
} else if (this.buttons[2].isPressed()) {
|
||||||
|
api.login(
|
||||||
|
this.textboxes[0].getWords(),
|
||||||
|
this.textboxes[1].getWords()
|
||||||
|
)
|
||||||
|
screenManager.setScreen(new StartScreen());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {key} key
|
||||||
|
*/
|
||||||
|
letterTyped(key) {
|
||||||
|
this.textboxes[this.activeTextBox].letterTyped(key);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,10 @@
|
|||||||
class StartScreen {
|
class StartScreen {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.buttons = [new Button(0,0,100,30,0,true,"#fff",false,"#000","#000","Sign Up")]
|
this.buttons = [
|
||||||
|
new Button(0,0,100,30,0,true,"#fff",false,"#000","#000","Sign Up"),
|
||||||
|
new Button(110,0,100,30,0,true,"#fff",false,"#000","#000","Login"),
|
||||||
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {
|
||||||
@ -10,9 +14,17 @@ class StartScreen {
|
|||||||
fill("#000");
|
fill("#000");
|
||||||
text("Press enter to start test", 0, 0, windowWidth - 100, windowHeight - 100);
|
text("Press enter to start test", 0, 0, windowWidth - 100, windowHeight - 100);
|
||||||
this.buttons[0].draw();
|
this.buttons[0].draw();
|
||||||
|
this.buttons[1].draw();
|
||||||
|
this.buttons[2].draw();
|
||||||
if (this.buttons[0].isPressed()) {
|
if (this.buttons[0].isPressed()) {
|
||||||
screenManager.setScreen(new SignUpScreen());
|
screenManager.setScreen(new SignUpScreen());
|
||||||
|
} else if (this.buttons[1].isPressed()) {
|
||||||
|
screenManager.setScreen(new LoginScreen());
|
||||||
|
} else if (this.buttons[2].isPressed()) {
|
||||||
|
api.logout();
|
||||||
}
|
}
|
||||||
|
fill("#000");
|
||||||
|
text(`${user.username}`, windowWidth-100, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
letterTyped(key) {
|
letterTyped(key) {
|
||||||
|
@ -133,7 +133,7 @@ class Textbox {
|
|||||||
|
|
||||||
if (pKey === "Backspace" && this.letters.length > 0) {
|
if (pKey === "Backspace" && this.letters.length > 0) {
|
||||||
this.letters.pop();
|
this.letters.pop();
|
||||||
this.words.substring(0, this.words.length-1)
|
this.words = this.words.substring(0, this.words.length-1)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user