impls chest content query, still working on the api...

This commit is contained in:
2019-06-20 15:28:06 +02:00
parent 48f71e048e
commit 472e24a62c
6 changed files with 73 additions and 19 deletions

View File

@@ -14,10 +14,31 @@ mod api {
use futures::Future;
use super::Pool;
use lootalot_db::Player;
struct Item;
use lootalot_db::Player as PlayerData;
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 || {
let conn = pool.get().unwrap();
println!("Waiting for player list...");
@@ -35,9 +56,19 @@ mod api {
})
}
fn chest_content(_id: i32) -> Result<Json<Vec<Item>>> {
let items: Vec<Item> = Vec::new();
Ok(Json(items))
pub fn chest_content(
player_id: web::Path<i32>,
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>> {
@@ -64,6 +95,7 @@ fn main() -> std::io::Result<()> {
App::new()
.data(pool.clone())
.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"))
})
.bind("127.0.0.1:8088")?