init rust db backend
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,2 +1,6 @@
|
|||||||
/target
|
/target
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
fontawesome
|
||||||
|
|
||||||
|
|||||||
@@ -4,4 +4,7 @@ version = "0.1.0"
|
|||||||
authors = ["Artus <artus@landoftheunicorn.ovh>"]
|
authors = ["Artus <artus@landoftheunicorn.ovh>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"lootalot_db"
|
||||||
|
]
|
||||||
|
|||||||
12
lootalot_db/Cargo.toml
Normal file
12
lootalot_db/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "lootalot-db"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Artus <artus@landoftheunicorn.ovh>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dotenv = "*"
|
||||||
|
|
||||||
|
[dependencies.diesel]
|
||||||
|
version = "1.0"
|
||||||
|
features = ["sqlite"]
|
||||||
BIN
lootalot_db/db.sqlite3
Normal file
BIN
lootalot_db/db.sqlite3
Normal file
Binary file not shown.
5
lootalot_db/diesel.toml
Normal file
5
lootalot_db/diesel.toml
Normal file
@@ -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"
|
||||||
0
lootalot_db/migrations/.gitkeep
Normal file
0
lootalot_db/migrations/.gitkeep
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
DROP TABLE items;
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE items (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
name VARCHAR NOT NULL,
|
||||||
|
base_price INTEGER NOT NULL
|
||||||
|
);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE players;
|
||||||
@@ -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
|
||||||
|
);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE looted;
|
||||||
@@ -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)
|
||||||
|
);
|
||||||
52
lootalot_db/src/lib.rs
Normal file
52
lootalot_db/src/lib.rs
Normal file
@@ -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<SqliteConnection, String> {
|
||||||
|
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<models::Player> {
|
||||||
|
use schema::players::dsl::*;
|
||||||
|
let conn = establish_connection().unwrap();
|
||||||
|
players
|
||||||
|
.load::<models::Player>(&conn)
|
||||||
|
.expect("Error loading players")
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn with_db<F>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
lootalot_db/src/models.rs
Normal file
61
lootalot_db/src/models.rs
Normal file
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
37
lootalot_db/src/schema.rs
Normal file
37
lootalot_db/src/schema.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
table! {
|
||||||
|
items (id) {
|
||||||
|
id -> Nullable<Integer>,
|
||||||
|
name -> Text,
|
||||||
|
base_price -> Integer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table! {
|
||||||
|
looted (id) {
|
||||||
|
id -> Nullable<Integer>,
|
||||||
|
player_id -> Integer,
|
||||||
|
item_id -> Integer,
|
||||||
|
acquired_date -> Nullable<Date>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table! {
|
||||||
|
players (id) {
|
||||||
|
id -> Nullable<Integer>,
|
||||||
|
name -> Text,
|
||||||
|
debt -> Nullable<Integer>,
|
||||||
|
cp -> Nullable<Integer>,
|
||||||
|
sp -> Nullable<Integer>,
|
||||||
|
gp -> Nullable<Integer>,
|
||||||
|
pp -> Nullable<Integer>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
joinable!(looted -> items (item_id));
|
||||||
|
joinable!(looted -> players (player_id));
|
||||||
|
|
||||||
|
allow_tables_to_appear_in_same_query!(
|
||||||
|
items,
|
||||||
|
looted,
|
||||||
|
players,
|
||||||
|
);
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
extern crate lootalot_db;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user