database functionality added

This commit is contained in:
Arlo Filley 2022-09-29 14:50:49 +01:00
parent cb31082580
commit f9581e9b3f
5 changed files with 117990 additions and 2 deletions

View File

@ -1,9 +1,12 @@
// relevant macros and imports for rocket.rs // relevant macros and imports for rocket.rs
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
use rocket::{Rocket, Build, fs::{FileServer, relative}, serde::{Deserialize, json::Json}}; use rocket::{Rocket, Build, fs::{FileServer, relative}, serde::{Deserialize, json::Json}};
pub mod sql;
#[get("/")] #[get("/")]
fn test() -> String { fn test() -> String {
sql::create_database()
.expect(&format!("couldn't create database"));
format!("Hello world") format!("Hello world")
} }
@ -32,7 +35,9 @@ fn post_test(test: Json<PostTest<'_>>) {
test.wpm, test.wpm,
test.accuracy, test.accuracy,
test.user_id test.user_id
) );
sql::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");
} }
#[launch] #[launch]

View File

@ -1 +1,50 @@
use rusqlite::{params, Connection, Result}; use rusqlite::{Connection, Result};
fn get_connection() -> rusqlite::Connection {
Connection::open("database/database.sqlite")
.expect("Error creating database connection")
}
pub fn create_database() -> Result<()> {
let connection = get_connection();
connection.execute(
"CREATE TABLE IF NOT EXISTS tests (
test_id INTEGER PRIMARY KEY,
test_type TEXT NOT NULL,
test_length INTEGER,
test_time INTEGER,
test_seed INTEGER,
quote_id INTEGER,
wpm INTEGER,
accuracy INTEGER,
user_id INTEGER
)",
(), // empty parameters list
)?;
Ok(())
}
pub fn post_test(test_type: &str, test_length: i64, test_time: i32, test_seed: i64, quote_id: i32, wpm: i16, accuracy: i8, user_id: i32)
-> Result<()> {
let connection = get_connection();
connection.execute(
"INSERT INTO tests (
test_type,
test_length,
test_time,
test_seed,
quote_id,
wpm,
accuracy,
user_id
)
VALUES
(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
", (test_type, test_length, test_time, test_seed, quote_id,
wpm, accuracy,user_id))?;
Ok(())
}

View File

@ -7,6 +7,7 @@ const data = {
quote_id: 0, quote_id: 0,
wpm: 60, wpm: 60,
accuracy: 100, accuracy: 100,
user_id: 0
} }
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const button = document.getElementById("button"); const button = document.getElementById("button");

111165
website/lib/p5.js Normal file

File diff suppressed because one or more lines are too long

6768
website/lib/p5.min.js vendored Normal file

File diff suppressed because one or more lines are too long