uses tips from diesel.rs guide for application integration
This commit is contained in:
@@ -6,35 +6,24 @@ extern crate serde_derive;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
// Convenience re-export, maybe DbApi should me move here instead ??
|
||||
pub use diesel::query_dsl::RunQueryDsl;
|
||||
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
mod models;
|
||||
mod schema;
|
||||
|
||||
pub use models::Item;
|
||||
pub use models::Player;
|
||||
|
||||
pub type DbConnection = SqliteConnection;
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<DbConnection>>;
|
||||
pub type QueryResult<T> = Result<T, diesel::result::Error>;
|
||||
pub type ActionResult = QueryResult<bool>;
|
||||
|
||||
pub use models::Item;
|
||||
pub use models::Player;
|
||||
|
||||
impl Player {
|
||||
/// Query the list of all players
|
||||
pub fn fetch_list(conn: &SqliteConnection) -> QueryResult<Vec<Self>> {
|
||||
use schema::players::dsl::*;
|
||||
Ok( players
|
||||
.load::<Self>(conn)?
|
||||
)
|
||||
}
|
||||
|
||||
/// Query the list of items belonging to player
|
||||
pub fn fetch_loot(player_id: i32, conn: &SqliteConnection) -> QueryResult<Vec<Item>> {
|
||||
use schema::looted::dsl::*;
|
||||
Ok( looted
|
||||
.filter(owner_id.eq(player_id))
|
||||
.select((id, name, base_price))
|
||||
.load::<Item>(conn)?
|
||||
)
|
||||
Ok( schema::players::table.load::<Self>(conn)? )
|
||||
}
|
||||
|
||||
pub fn action_claim_object(player_id: i32, item_id: i32, conn: &DbConnection) -> ActionResult {
|
||||
@@ -48,8 +37,8 @@ impl Player {
|
||||
|
||||
|
||||
pub fn create_pool() -> Pool {
|
||||
dbg!(dotenv::dotenv().ok());
|
||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||
dbg!( &connspec );
|
||||
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
||||
r2d2::Pool::builder()
|
||||
.build(manager)
|
||||
@@ -58,23 +47,5 @@ pub fn create_pool() -> Pool {
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user