diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index f243542..d70c6bd 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -13,24 +13,36 @@ pub mod schema; pub type DbConnection = SqliteConnection; pub type Pool = r2d2::Pool>; pub type QueryResult = Result; +pub type ActionResult = QueryResult; pub use models::Item; pub use models::Player; impl Player { + /// Query the list of all players pub fn fetch_list(conn: &SqliteConnection) -> QueryResult> { use schema::players::dsl::*; - Ok(players.load::(conn)?) + Ok( players + .load::(conn)? + ) } - pub fn fetch_chest(player_id: i32, conn: &SqliteConnection) -> QueryResult> { + /// Query the list of items belonging to player + pub fn fetch_loot(player_id: i32, conn: &SqliteConnection) -> QueryResult> { use schema::looted::dsl::*; - let owned = looted - .filter(owner_id.eq(player_id)) - .select((id, name, base_price)) - .load::(conn)?; - dbg!(&owned); - Ok(owned) + Ok( looted + .filter(owner_id.eq(player_id)) + .select((id, name, base_price)) + .load::(conn)? + ) + } + + pub fn action_claim_object(player_id: i32, item_id: i32, conn: &DbConnection) -> ActionResult { + Ok(false) + } + + pub fn action_withdraw_claim(player_id: i32, item_id: i32, conn: &DbConnection) -> ActionResult { + Ok(false) } } diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000..8cbee32 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,83 @@ +use actix_web::{web, web::Json, Error, HttpResponse, Result}; +use futures::Future; + +use lootalot_db::{Pool}; + +// Wrapper for database queries. +//fn db_query(q: fn(...)) -> impl Future { +// +//} + +mod player { + use lootalot_db::{Player as PlayerData, Item, ActionResult, QueryResult, DbConnection}; + + /// A reference to a player in database + pub(super) struct Player(pub(super) i32); + + impl Player { + /// Fetch all players from db + pub(super) fn all(conn: &DbConnection) -> QueryResult> { + PlayerData::fetch_list(conn) + } + /// Fetch the content of a player's chest + pub(super) fn loot(self, conn: &DbConnection) -> QueryResult> { + PlayerData::fetch_loot(self.0, conn) + } + pub(super) fn claim(self, item: i32, conn: &DbConnection) -> ActionResult { + PlayerData::action_claim_object(self.0, item, conn) + } + + } +} + + + +pub fn players_list( + pool: web::Data +) -> impl Future { + web::block(move || { + let conn = pool.get().unwrap(); + player::Player::all(&conn) + }) + .then(|res| match res { + Ok(players) => { + HttpResponse::Ok().json(players) + } + Err(_) => { + HttpResponse::InternalServerError().finish() + } + }) +} + +pub fn chest_content( + player_id: web::Path, + pool: web::Data +) -> impl Future { + web::block(move || { + let conn = pool.get().unwrap(); + player::Player(*player_id).loot(&conn) + }) + .then(|res| match res { + Ok(items) => HttpResponse::Ok().json(items), + Err(_) => HttpResponse::InternalServerError().finish(), + }) +} + +fn put_request( + params: web::Path<(i32, i32)>, + pool: web::Data +) -> impl Future { + web::block(move || { + let (player_id, item_id) = *params; + let conn = pool.get().unwrap(); + player::Player(player_id).claim(item_id, &conn) + }) + .then(|res| match res { + Ok(status) => HttpResponse::Ok().json(status), + Err(_) => HttpResponse::InternalServerError().finish(), + }) +} + +fn withdraw_request(_player_id: i32, _item_id: i32) -> Result> { + Ok(Json(false)) +} diff --git a/src/main.rs b/src/main.rs index 6655fdf..8f2e0c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,79 +6,9 @@ extern crate lootalot_db; use actix_files as fs; use actix_web::{web, App, HttpServer}; -use lootalot_db::Pool; use std::env; -mod api { - use actix_web::{web, web::Json, Error, HttpResponse, Result}; - use futures::Future; - - use super::Pool; - use lootalot_db::Player as PlayerData; - use lootalot_db::{Item, QueryResult, DbConnection}; - - // Wrapper for database queries. - //fn db_query(q: fn(...)) -> impl Future { - // - //} - - struct Player(i32); - - impl Player { - - /// Fetch all players from db - fn fetch_list(conn: &DbConnection) -> QueryResult> { - PlayerData::fetch_list(conn) - } - /// Fetch the content of a player's chest - fn chest(self, conn: &DbConnection) -> QueryResult> { - PlayerData::fetch_chest(self.0, conn) - } - } - - pub fn players_list( - pool: web::Data - ) -> impl Future { - web::block(move || { - let conn = pool.get().unwrap(); - println!("Waiting for player list..."); - Player::fetch_list(&conn) - }) - .then(|res| match res { - Ok(players) => { - println!("Ok! {:?}", &players); - HttpResponse::Ok().json(players) - } - Err(_) => { - println!("Error!"); - HttpResponse::InternalServerError().finish() - } - }) - } - - pub fn chest_content( - player_id: web::Path, - pool: web::Data - ) -> impl Future { - 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> { - Ok(Json(false)) - } - - fn withdraw_request(_player_id: i32, _item_id: i32) -> Result> { - Ok(Json(false)) - } -} +mod api; fn main() -> std::io::Result<()> { println!("Hello, world!");