From 4454bd245ca48dbed06726b8dd18422a5554ed3e Mon Sep 17 00:00:00 2001 From: Artus Date: Tue, 11 Jun 2019 16:14:10 +0200 Subject: [PATCH] init rust db backend --- .gitignore | 6 +- Cargo.toml | 5 +- lootalot_db/Cargo.toml | 12 ++++ lootalot_db/db.sqlite3 | Bin 0 -> 28672 bytes lootalot_db/diesel.toml | 5 ++ lootalot_db/migrations/.gitkeep | 0 .../2019-06-10-123922_create_items/down.sql | 2 + .../2019-06-10-123922_create_items/up.sql | 5 ++ .../2019-06-10-123940_create_players/down.sql | 1 + .../2019-06-10-123940_create_players/up.sql | 9 +++ .../2019-06-10-124013_create_looted/down.sql | 1 + .../2019-06-10-124013_create_looted/up.sql | 8 +++ lootalot_db/src/lib.rs | 52 +++++++++++++++ lootalot_db/src/models.rs | 61 ++++++++++++++++++ lootalot_db/src/schema.rs | 37 +++++++++++ src/main.rs | 2 + 16 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 lootalot_db/Cargo.toml create mode 100644 lootalot_db/db.sqlite3 create mode 100644 lootalot_db/diesel.toml create mode 100644 lootalot_db/migrations/.gitkeep create mode 100644 lootalot_db/migrations/2019-06-10-123922_create_items/down.sql create mode 100644 lootalot_db/migrations/2019-06-10-123922_create_items/up.sql create mode 100644 lootalot_db/migrations/2019-06-10-123940_create_players/down.sql create mode 100644 lootalot_db/migrations/2019-06-10-123940_create_players/up.sql create mode 100644 lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql create mode 100644 lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql create mode 100644 lootalot_db/src/lib.rs create mode 100644 lootalot_db/src/models.rs create mode 100644 lootalot_db/src/schema.rs diff --git a/.gitignore b/.gitignore index f0e3bca..9fe5041 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ /target -**/*.rs.bk \ No newline at end of file +**/*.rs.bk + +node_modules +fontawesome + diff --git a/Cargo.toml b/Cargo.toml index 3b82c02..baa7348 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,7 @@ version = "0.1.0" authors = ["Artus "] edition = "2018" -[dependencies] +[workspace] +members = [ + "lootalot_db" +] diff --git a/lootalot_db/Cargo.toml b/lootalot_db/Cargo.toml new file mode 100644 index 0000000..4b8ddf0 --- /dev/null +++ b/lootalot_db/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "lootalot-db" +version = "0.1.0" +authors = ["Artus "] +edition = "2018" + +[dependencies] +dotenv = "*" + +[dependencies.diesel] +version = "1.0" +features = ["sqlite"] diff --git a/lootalot_db/db.sqlite3 b/lootalot_db/db.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..95ea61df7aacc8e8221aaceaac44ff347d40ff24 GIT binary patch literal 28672 zcmeI)&ui0Q7zgk-O|y2zHU2=#*ySAzY;ct{-3r^u*k!ZWbu;Uf;UQ%0yBTPknxvu^ z!R_up;mNxvuU@>$PAY=nT|9d9P1bg;t_p&%!S`ivlRWS9=EtX@kaq98b+_UKjPAK! z%MNIPTqJ@(Zc<7JN%NZMRe>*)yvBL$moWCXk|sCiH>czuM7mHW=@0VD^v=}X3*~Uv zC=h@E1Rwwb2tWV=5P-nB6*!QjL}q$gIAnpn+hk4G4Orv&b)sY%dd;9U{Z_@G$1$2s zQR*~kxmq*U43ln}<#pZMp?8d(dEU}#+E1Bh4JA}JYP7mtsf4ZkiKR8J&8|P{Iv#6S z4VypLtc1s1F;?~M3h%ygKkU7_VH)MNYIrD`9UMrZrmuhM*BSuHH* zi*vD&tisZQ8usaDY0Ha+Ma1hGq^GI^3!Skkoc_kThDO86fN1Rwwb2tWV=5P$## zAOHafKmY>gS71hnlSQ$_oOaOl7#|xFj`rtcCaxEyXE(#=|G4~y$lv8J@_W950s#m> z00Izz00bZa0SG_<0uX?}84|b}Pm*iNu+;c051XF5$9&)Qro<%4MB&x9ulAgDEJ>!L z&5GONQxv3y{&qAk1Unx8e-!@z|7ZE=3|$^h1p*L&00bZa0SG_<0uX=z1Rwx`KM5p7 ziA+vj;j>-Jv2bm+KkQ#EUKAuEOwPumh^FP#g`piy@O6pT;W_??nz0H22tWV=5P$## RAOHafKmY;|fWUbb_zBGra_#^C literal 0 HcmV?d00001 diff --git a/lootalot_db/diesel.toml b/lootalot_db/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/lootalot_db/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/lootalot_db/migrations/.gitkeep b/lootalot_db/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql b/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql new file mode 100644 index 0000000..dfed2b1 --- /dev/null +++ b/lootalot_db/migrations/2019-06-10-123922_create_items/down.sql @@ -0,0 +1,2 @@ +DROP TABLE items; + diff --git a/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql b/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql new file mode 100644 index 0000000..1be5066 --- /dev/null +++ b/lootalot_db/migrations/2019-06-10-123922_create_items/up.sql @@ -0,0 +1,5 @@ +CREATE TABLE items ( + id INTEGER PRIMARY KEY, + name VARCHAR NOT NULL, + base_price INTEGER NOT NULL +); diff --git a/lootalot_db/migrations/2019-06-10-123940_create_players/down.sql b/lootalot_db/migrations/2019-06-10-123940_create_players/down.sql new file mode 100644 index 0000000..afa1e7d --- /dev/null +++ b/lootalot_db/migrations/2019-06-10-123940_create_players/down.sql @@ -0,0 +1 @@ +DROP TABLE players; diff --git a/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql b/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql new file mode 100644 index 0000000..b14a350 --- /dev/null +++ b/lootalot_db/migrations/2019-06-10-123940_create_players/up.sql @@ -0,0 +1,9 @@ +CREATE TABLE players ( + id INTEGER PRIMARY KEY, + name VARCHAR NOT NULL, + debt INTEGER DEFAULT 0, -- debt to the group in copper pieces + cp INTEGER DEFAULT 0, + sp INTEGER DEFAULT 0, + gp INTEGER DEFAULT 0, + pp INTEGER DEFAULT 0 +); diff --git a/lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql b/lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql new file mode 100644 index 0000000..f01e49c --- /dev/null +++ b/lootalot_db/migrations/2019-06-10-124013_create_looted/down.sql @@ -0,0 +1 @@ +DROP TABLE looted; diff --git a/lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql b/lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql new file mode 100644 index 0000000..5169a5b --- /dev/null +++ b/lootalot_db/migrations/2019-06-10-124013_create_looted/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE looted ( + id INTEGER PRIMARY KEY, + player_id INTEGER NOT NULL, + item_id INTEGER NOT NULL, + acquired_date DATE DEFAULT NOW, + FOREIGN KEY (player_id) REFERENCES players(id), + FOREIGN KEY (item_id) REFERENCES items(id) +); diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs new file mode 100644 index 0000000..f90b0d1 --- /dev/null +++ b/lootalot_db/src/lib.rs @@ -0,0 +1,52 @@ +#[macro_use] extern crate diesel; + extern crate dotenv; + +use diesel::prelude::*; +use dotenv::dotenv; +use std::env; + +pub fn establish_connection() -> Result { + dotenv().ok(); + let database_url = + env::var("DATABASE_URL") + .expect("DATABASE_URL must be set !"); + SqliteConnection::establish(&database_url) + .map_err(|e| format!("Error connecting to {} : {:?}", database_url, e)) +} + +pub mod schema; +pub mod models; + +pub fn list_players() -> Vec { + use schema::players::dsl::*; + let conn = establish_connection().unwrap(); + players + .load::(&conn) + .expect("Error loading players") + .collect() +} + + + + +#[cfg(test)] +mod tests { + use super::*; + + fn with_db(f: F) -> () + where F: Fn(&SqliteConnection) -> (), + { + let conn = establish_connection().unwrap(); + conn.test_transaction::<_,diesel::result::Error,_>(|| { + f(&conn); + Ok(()) + }); + } + + #[test] + fn database_connection() { + use crate::establish_connection; + let conn = establish_connection(); + assert_eq!(conn.is_ok(), true); + } +} diff --git a/lootalot_db/src/models.rs b/lootalot_db/src/models.rs new file mode 100644 index 0000000..93c9977 --- /dev/null +++ b/lootalot_db/src/models.rs @@ -0,0 +1,61 @@ +use crate::*; +use crate::schema::players; + +#[derive(Queryable)] +pub struct Player { + id: i32, + name: String, + debt: i32, + cp: i32, + sp: i32, + gp: i32, + pp: i32, +} + +#[derive(Insertable)] +#[table_name="players"] +pub struct NewPlayer<'a> { + name: &'a str, + cp: i32, + sp: i32, + gp: i32, + pp: i32, +} + +impl<'a> NewPlayer<'a> { + + fn new( + name: &'a str, + wealth: Option<(i32,i32,i32,i32)>, + ) -> Self { + let (cp,sp,gp,pp) = wealth.unwrap_or((0,0,0,0)); + NewPlayer { name, cp, sp, gp, pp } + } + + fn insert(&self) -> Result<(),()> { + Err(()) + } +} + +#[cfg(test)] +mod tests { + use crate::tests; + use super::*; + + #[test] + fn new_player_only_with_name() { + let n = NewPlayer::new("Féfi", None); + assert_eq!(n.name, "Féfi"); + assert_eq!(n.cp, 0); + assert_eq!(n.sp, 0); + assert_eq!(n.gp, 0); + assert_eq!(n.pp, 0); + } + + #[test] + fn new_player_with_wealth() { + let initial_wealth = (1, 2, 3, 4); + let n = NewPlayer::new("Féfi", Some(initial_wealth)); + assert_eq!(initial_wealth, (n.cp, n.sp, n.gp, n.pp)); + } +} diff --git a/lootalot_db/src/schema.rs b/lootalot_db/src/schema.rs new file mode 100644 index 0000000..d7f7c90 --- /dev/null +++ b/lootalot_db/src/schema.rs @@ -0,0 +1,37 @@ +table! { + items (id) { + id -> Nullable, + name -> Text, + base_price -> Integer, + } +} + +table! { + looted (id) { + id -> Nullable, + player_id -> Integer, + item_id -> Integer, + acquired_date -> Nullable, + } +} + +table! { + players (id) { + id -> Nullable, + name -> Text, + debt -> Nullable, + cp -> Nullable, + sp -> Nullable, + gp -> Nullable, + pp -> Nullable, + } +} + +joinable!(looted -> items (item_id)); +joinable!(looted -> players (player_id)); + +allow_tables_to_appear_in_same_query!( + items, + looted, + players, +); diff --git a/src/main.rs b/src/main.rs index e7a11a9..7dfc9f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +extern crate lootalot_db; + fn main() { println!("Hello, world!"); }