Basic SQL done
parent
c5781e8804
commit
ff6440a418
|
@ -14,3 +14,11 @@ Cargo.lock
|
|||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
|
||||
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
# Added by BurnyLlama
|
||||
.env
|
|
@ -0,0 +1,22 @@
|
|||
[package]
|
||||
name = "ddguesser"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
dotenvy = "0.15.7"
|
||||
rocket = { version = "0.5.0-rc.3", features = ["json"] }
|
||||
serde = "1.0.183"
|
||||
sqlx = { version = "0.7.1", features = [ "runtime-tokio-rustls", "postgres", "chrono", "uuid" ] }
|
||||
tokio-test = "0.4.2"
|
||||
uuid = { version = "1.4.1", features = ["v4", "fast-rng", "macro-diagnostics", "serde"]}
|
||||
|
||||
[profile.release]
|
||||
debug = false
|
||||
codegen-units = 1
|
||||
lto = true
|
||||
|
||||
[build]
|
||||
rustflags = ["-C", "target-cpu=native"]
|
|
@ -0,0 +1,33 @@
|
|||
use dotenvy::dotenv;
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use std::{env, error::Error};
|
||||
|
||||
pub mod models;
|
||||
|
||||
pub struct DatabaseHandler {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl DatabaseHandler {
|
||||
pub async fn create() -> Result<Self, Box<dyn Error>> {
|
||||
// Load environment variables
|
||||
dotenv().ok();
|
||||
|
||||
// Get the database URL
|
||||
let db_url = env::var("DATABASE_URL")?;
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(12)
|
||||
.connect(&db_url)
|
||||
.await?;
|
||||
|
||||
let row: (i64,) = sqlx::query_as("SELECT $1")
|
||||
.bind(150_i64)
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
|
||||
assert_eq!(row.0, 150);
|
||||
|
||||
Ok(DatabaseHandler { pool })
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
use crate::database::DatabaseHandler;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Location {
|
||||
id: Uuid,
|
||||
map: String,
|
||||
}
|
||||
|
||||
impl Location {
|
||||
pub async fn get_random(db: &DatabaseHandler) -> Result<Location, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Location,
|
||||
"SELECT id, map FROM locations ORDER BY RANDOM() LIMIT 1"
|
||||
)
|
||||
.fetch_one(&db.pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_all_map_names(db: &DatabaseHandler) -> Result<Vec<String>, sqlx::Error> {
|
||||
struct LocationOnlyName {
|
||||
map: String,
|
||||
}
|
||||
|
||||
let locations: Vec<LocationOnlyName> = sqlx::query_as!(
|
||||
LocationOnlyName,
|
||||
"SELECT DISTINCT(map) AS map FROM locations"
|
||||
)
|
||||
.fetch_all(&db.pool)
|
||||
.await?;
|
||||
|
||||
let names = locations.iter().map(|map| map.map.to_owned()).collect();
|
||||
|
||||
Ok(names)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
async fn get_db() -> DatabaseHandler {
|
||||
match DatabaseHandler::create().await {
|
||||
Ok(db) => db,
|
||||
Err(err) => panic!("Error while connecting to database! {:?}", err),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_random_location() {
|
||||
async fn test() {
|
||||
let db = get_db().await;
|
||||
|
||||
let location = match Location::get_random(&db).await {
|
||||
Ok(location) => location,
|
||||
Err(err) => panic!("Error while getting random location! {:?}", err),
|
||||
};
|
||||
|
||||
println!("Location: {:?}", location);
|
||||
}
|
||||
|
||||
tokio_test::block_on(test());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_all_map_names() {
|
||||
async fn test() {
|
||||
let db = get_db().await;
|
||||
|
||||
let map_names = match Location::get_all_map_names(&db).await {
|
||||
Ok(names) => names,
|
||||
Err(err) => panic!("Error while getting random location! {:?}", err),
|
||||
};
|
||||
|
||||
println!("Found maps:");
|
||||
map_names
|
||||
.iter()
|
||||
.for_each(|map_name| println!("{}", map_name));
|
||||
}
|
||||
|
||||
tokio_test::block_on(test());
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod location;
|
|
@ -0,0 +1,22 @@
|
|||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
mod database;
|
||||
|
||||
use database::DatabaseHandler;
|
||||
|
||||
#[rocket::main]
|
||||
async fn main() {
|
||||
let database = match DatabaseHandler::create().await {
|
||||
Ok(db) => db,
|
||||
Err(err) => panic!(
|
||||
"Encountered an error while connecting to database!\n{:?}",
|
||||
err
|
||||
),
|
||||
};
|
||||
|
||||
match rocket::build().manage(database).launch().await {
|
||||
Ok(_) => (),
|
||||
Err(err) => println!("Encountered an error while starting rocket:\n{}", err),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue