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::prelude::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
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 models;
|
||||||
pub mod schema;
|
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;
|
pub use models::Player;
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
pub fn fetch_list(
|
pub fn fetch_list(conn: &SqliteConnection) -> QueryResult<Vec<Self>> {
|
||||||
conn: &SqliteConnection,
|
|
||||||
) -> Result<Vec<models::Player>, diesel::result::Error> {
|
|
||||||
use schema::players::dsl::*;
|
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 {
|
pub fn create_pool() -> Pool {
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
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()
|
r2d2::Pool::builder()
|
||||||
.build(manager)
|
.build(manager)
|
||||||
.expect("Failed to create pool.")
|
.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;
|
||||||
44
src/main.rs
44
src/main.rs
@@ -14,10 +14,31 @@ mod api {
|
|||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
use super::Pool;
|
use super::Pool;
|
||||||
use lootalot_db::Player;
|
use lootalot_db::Player as PlayerData;
|
||||||
struct Item;
|
use lootalot_db::{Item, QueryResult, DbConnection};
|
||||||
|
|
||||||
pub fn players_list(pool: web::Data<Pool>) -> impl Future<Item = HttpResponse, Error = Error> {
|
// Wrapper for database queries.
|
||||||
|
//fn db_query(q: fn(...)) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
struct Player(i32);
|
||||||
|
|
||||||
|
impl Player {
|
||||||
|
|
||||||
|
/// Fetch all players from db
|
||||||
|
fn fetch_list(conn: &DbConnection) -> QueryResult<Vec<PlayerData>> {
|
||||||
|
PlayerData::fetch_list(conn)
|
||||||
|
}
|
||||||
|
/// Fetch the content of a player's chest
|
||||||
|
fn chest(self, conn: &DbConnection) -> QueryResult<Vec<Item>> {
|
||||||
|
PlayerData::fetch_chest(self.0, conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn players_list(
|
||||||
|
pool: web::Data<Pool>
|
||||||
|
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
web::block(move || {
|
web::block(move || {
|
||||||
let conn = pool.get().unwrap();
|
let conn = pool.get().unwrap();
|
||||||
println!("Waiting for player list...");
|
println!("Waiting for player list...");
|
||||||
@@ -35,9 +56,19 @@ mod api {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chest_content(_id: i32) -> Result<Json<Vec<Item>>> {
|
pub fn chest_content(
|
||||||
let items: Vec<Item> = Vec::new();
|
player_id: web::Path<i32>,
|
||||||
Ok(Json(items))
|
pool: web::Data<Pool>
|
||||||
|
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
web::block(move || {
|
||||||
|
let conn = pool.get().unwrap();
|
||||||
|
dbg!("Fetching chest content...");
|
||||||
|
Player(*player_id).chest(&conn)
|
||||||
|
})
|
||||||
|
.then(|res| match res {
|
||||||
|
Ok(items) => HttpResponse::Ok().json(items),
|
||||||
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_request(_player_id: i32, _item_id: i32) -> Result<Json<bool>> {
|
fn put_request(_player_id: i32, _item_id: i32) -> Result<Json<bool>> {
|
||||||
@@ -64,6 +95,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
.data(pool.clone())
|
.data(pool.clone())
|
||||||
.route("/players", web::get().to_async(api::players_list))
|
.route("/players", web::get().to_async(api::players_list))
|
||||||
|
.route("/loot/{player_id}", web::get().to_async(api::chest_content))
|
||||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")?
|
.bind("127.0.0.1:8088")?
|
||||||
|
|||||||
Reference in New Issue
Block a user