refactors api

This commit is contained in:
2019-06-22 14:57:51 +02:00
parent 23adaa3e79
commit f5e495734f
3 changed files with 59 additions and 73 deletions

View File

@@ -12,6 +12,7 @@ dotenv = "*"
env_logger = "*" env_logger = "*"
futures = "0.1" futures = "0.1"
diesel = "*" diesel = "*"
serde = "*"
[workspace] [workspace]
members = [ members = [

View File

@@ -1,32 +1,37 @@
use actix_web::{web, web::Json, Error, HttpResponse, Result}; use actix_web::{web, web::Json, Error, HttpResponse, Result};
use futures::Future; use futures::Future;
use lootalot_db::{Pool}; use lootalot_db::Pool;
use lootalot_db::{ActionResult, DbConnection, Item, Player as PlayerData, QueryResult};
// Wrapper for database queries. pub struct DbApi<'q>(&'q DbConnection);
//fn db_query(q: fn(...)) -> impl Future<Item = HttpResponse, Error = Error> {
//
//}
mod player { impl<'q> DbApi<'q> {
use lootalot_db::{Player as PlayerData, Item, ActionResult, QueryResult, DbConnection}; pub fn with_conn(conn: &'q DbConnection) -> Self {
Self(conn)
/// 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<Vec<PlayerData>> {
PlayerData::fetch_list(conn)
} }
pub fn fetch_players(self) -> QueryResult<Vec<PlayerData>> {
PlayerData::fetch_list(self.0)
}
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
AsPlayer { id, conn: self.0 }
}
}
pub struct AsPlayer<'q> {
id: i32,
conn: &'q DbConnection,
}
impl<'q> AsPlayer<'q> {
/// Fetch the content of a player's chest /// Fetch the content of a player's chest
pub(super) fn loot(self, conn: &DbConnection) -> QueryResult<Vec<Item>> { pub fn loot(self) -> QueryResult<Vec<Item>> {
PlayerData::fetch_loot(self.0, conn) PlayerData::fetch_loot(self.id, self.conn)
} }
pub(super) fn claim(self, item: i32, conn: &DbConnection) -> ActionResult { pub fn claim(self, item: i32) -> ActionResult {
PlayerData::action_claim_object(self.0, item, conn) PlayerData::action_claim_object(self.id, item, self.conn)
}
} }
} }
@@ -39,62 +44,31 @@ mod player {
// .claim(item_id) -> Success status (bool) // .claim(item_id) -> Success status (bool)
// .unclaim(item_id) -> Success status (bool) // .unclaim(item_id) -> Success status (bool)
// .sell(item_id) -> Success status (bool, earned) // .sell(item_id) -> Success status (bool, earned)
// .income(wealth_data) -> Success status (bool, new_wealth)
// .with_admin()
// .resolve_claims()
//
// fn db_call(f: fn(DbApi) -> ApiResponse) { ... } // fn db_call(f: fn(DbApi) -> ApiResponse) { ... }
// ApiResponse needs to be Serialize // ApiResponse needs to be Serialize
// endpoint example pub fn db_call<
// fn endpoint_name(params...) -> impl ... { J: serde::ser::Serialize + Send + 'static,
// let param_1 = { ... }; Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
// db_call(move |api| api.with_player(param_1).loot()) >(
// } pool: web::Data<Pool>,
query: Q,
pub fn players_list(
pool: web::Data<Pool>
) -> impl Future<Item = HttpResponse, Error = Error> { ) -> impl Future<Item = HttpResponse, Error = Error> {
web::block(move || {
let conn = pool.get().unwrap(); let conn = pool.get().unwrap();
player::Player::all(&conn) web::block(move || {
let api = DbApi::with_conn(&conn);
query(api)
}) })
.then(|res| match res { .then(|res| match res {
Ok(players) => { Ok(players) => HttpResponse::Ok().json(players),
HttpResponse::Ok().json(players) Err(e) => {
} dbg!(&e);
Err(_) => {
HttpResponse::InternalServerError().finish() HttpResponse::InternalServerError().finish()
} }
}) })
} }
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();
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<Pool>
) -> impl Future<Item = HttpResponse, Error = Error> {
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<Json<bool>> {
Ok(Json(false))
}

View File

@@ -11,21 +11,32 @@ use std::env;
mod api; mod api;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
println!("Hello, world!");
env::set_var("RUST_LOG", "actix_web=debug"); env::set_var("RUST_LOG", "actix_web=debug");
env_logger::init(); env_logger::init();
dotenv::dotenv().ok(); dotenv::dotenv().ok();
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set"); let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
println!("serving files from: {}", &www_root); println!("serving files from: {}", &www_root);
use lootalot_db::Pool;
type AppPool = web::Data<Pool>;
let pool = lootalot_db::create_pool(); let pool = lootalot_db::create_pool();
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.data(pool.clone()) .data(pool.clone())
.route("/players", web::get().to_async(api::players_list)) .route(
.route("/loot/{player_id}", web::get().to_async(api::chest_content)) "/players",
web::get().to_async(move |pool: AppPool| {
api::db_call(pool, move |api| api.fetch_players())
}),
)
.route(
"/loot/{player_id}",
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
let player_id: i32 = *player_id;
api::db_call(pool, move |api| api.as_player(player_id).loot())
}),
)
.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")?