init rust db backend
This commit is contained in:
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,
|
||||
);
|
||||
Reference in New Issue
Block a user