impls chest content query, still working on the api...
This commit is contained in:
Binary file not shown.
@@ -7,33 +7,42 @@ extern crate serde_derive;
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
|
||||
pub fn establish_connection() -> Result<SqliteConnection, String> {
|
||||
dotenv::dotenv().ok();
|
||||
let database_url = std::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 models;
|
||||
pub mod schema;
|
||||
|
||||
pub type DbConnection = SqliteConnection;
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<DbConnection>>;
|
||||
pub type QueryResult<T> = Result<T, diesel::result::Error>;
|
||||
|
||||
pub use models::Item;
|
||||
pub use models::Player;
|
||||
|
||||
impl Player {
|
||||
pub fn fetch_list(
|
||||
conn: &SqliteConnection,
|
||||
) -> Result<Vec<models::Player>, diesel::result::Error> {
|
||||
pub fn fetch_list(conn: &SqliteConnection) -> QueryResult<Vec<Self>> {
|
||||
use schema::players::dsl::*;
|
||||
Ok(players.load::<models::Player>(conn)?)
|
||||
Ok(players.load::<Self>(conn)?)
|
||||
}
|
||||
|
||||
pub fn fetch_chest(player: i32, conn: &SqliteConnection) -> QueryResult<Vec<Item>> {
|
||||
let owned = {
|
||||
use schema::looted::dsl::*;
|
||||
looted.filter(player_id.eq(player)).select(item_id).load::<i32>(conn)?
|
||||
};
|
||||
dbg!(&owned);
|
||||
let chest = {
|
||||
use schema::items::dsl::*;
|
||||
items.filter(id.eq_any(owned)).load::<Item>(conn)?
|
||||
};
|
||||
dbg!(&chest);
|
||||
Ok(chest)
|
||||
}
|
||||
}
|
||||
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
pub fn create_pool() -> Pool {
|
||||
dotenv::dotenv().ok();
|
||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
||||
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
||||
r2d2::Pool::builder()
|
||||
.build(manager)
|
||||
.expect("Failed to create pool.")
|
||||
|
||||
8
lootalot_db/src/models/item.rs
Normal file
8
lootalot_db/src/models/item.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use crate::*;
|
||||
|
||||
#[derive(Debug, Queryable, Serialize)]
|
||||
pub struct Item {
|
||||
id: i32,
|
||||
name: String,
|
||||
base_value: i32,
|
||||
}
|
||||
5
lootalot_db/src/models/mod.rs
Normal file
5
lootalot_db/src/models/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod item;
|
||||
mod player;
|
||||
|
||||
pub use item::Item;
|
||||
pub use player::Player;
|
||||
Reference in New Issue
Block a user