database functionality added
This commit is contained in:
parent
cb31082580
commit
f9581e9b3f
@ -1,9 +1,12 @@
|
||||
// relevant macros and imports for rocket.rs
|
||||
#[macro_use] extern crate rocket;
|
||||
use rocket::{Rocket, Build, fs::{FileServer, relative}, serde::{Deserialize, json::Json}};
|
||||
pub mod sql;
|
||||
|
||||
#[get("/")]
|
||||
fn test() -> String {
|
||||
sql::create_database()
|
||||
.expect(&format!("couldn't create database"));
|
||||
format!("Hello world")
|
||||
}
|
||||
|
||||
@ -32,7 +35,9 @@ fn post_test(test: Json<PostTest<'_>>) {
|
||||
test.wpm,
|
||||
test.accuracy,
|
||||
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]
|
||||
|
51
src/sql.rs
51
src/sql.rs
@ -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(())
|
||||
}
|
@ -7,6 +7,7 @@ const data = {
|
||||
quote_id: 0,
|
||||
wpm: 60,
|
||||
accuracy: 100,
|
||||
user_id: 0
|
||||
}
|
||||
const xhr = new XMLHttpRequest();
|
||||
const button = document.getElementById("button");
|
||||
|
111165
website/lib/p5.js
Normal file
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
6768
website/lib/p5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user