moves API logic inside its own module
This commit is contained in:
@@ -5,6 +5,7 @@ use futures::Future;
|
||||
use std::env;
|
||||
|
||||
use lootalot_db as db;
|
||||
use crate::api;
|
||||
|
||||
type AppPool = web::Data<db::Pool>;
|
||||
type PlayerId = web::Path<i32>;
|
||||
@@ -14,11 +15,11 @@ type ItemListWithMods = web::Json<Vec<(i32, Option<f32>)>>;
|
||||
/// Wraps call to the database query and convert its result as a async HttpResponse
|
||||
pub fn db_call(
|
||||
pool: AppPool,
|
||||
query: db::ApiActions,
|
||||
query: api::ApiActions,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
{
|
||||
let conn = pool.get().unwrap();
|
||||
web::block(move || db::execute(&conn, query)).then(|res| match res {
|
||||
web::block(move || api::execute(&conn, query)).then(|res| match res {
|
||||
Ok(r) => HttpResponse::Ok().json(r),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
@@ -28,13 +29,14 @@ pub fn db_call(
|
||||
}
|
||||
|
||||
fn configure_app(config: &mut web::ServiceConfig) {
|
||||
use api::ApiActions as Q;
|
||||
config.service(
|
||||
web::scope("/api")
|
||||
.service(
|
||||
web::scope("/players")
|
||||
.service(
|
||||
web::resource("/").route(
|
||||
web::get().to_async(|pool| db_call(pool, db::ApiActions::FetchPlayers)),
|
||||
web::get().to_async(|pool| db_call(pool, Q::FetchPlayers)),
|
||||
), //.route(web::post().to_async(endpoints::new_player))
|
||||
) // List of players
|
||||
.service(
|
||||
@@ -45,14 +47,14 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
||||
//.route(web::get().to_async(endpoints::player_claims))
|
||||
.route(web::put().to_async(
|
||||
|pool, (player, data): (PlayerId, ItemId)| {
|
||||
db_call(pool, db::ApiActions::ClaimItem(*player, *data))
|
||||
db_call(pool, Q::ClaimItem(*player, *data))
|
||||
},
|
||||
))
|
||||
.route(web::delete().to_async(
|
||||
|pool, (player, data): (PlayerId, ItemId)| {
|
||||
db_call(
|
||||
pool,
|
||||
db::ApiActions::UnclaimItem(*player, *data),
|
||||
Q::UnclaimItem(*player, *data),
|
||||
)
|
||||
},
|
||||
)),
|
||||
@@ -64,7 +66,7 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
||||
|pool, (player, data): (PlayerId, web::Json<f32>)| {
|
||||
db_call(
|
||||
pool,
|
||||
db::ApiActions::UpdateWealth(*player, *data),
|
||||
Q::UpdateWealth(*player, *data),
|
||||
)
|
||||
},
|
||||
)),
|
||||
@@ -72,26 +74,26 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
||||
.service(
|
||||
web::resource("/loot")
|
||||
.route(web::get().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, db::ApiActions::FetchLoot(*player))
|
||||
db_call(pool, Q::FetchLoot(*player))
|
||||
}))
|
||||
.route(web::put().to_async(
|
||||
move |pool, (player, data): (PlayerId, ItemListWithMods)| {
|
||||
db_call(pool, db::ApiActions::BuyItems(*player, data.into_inner()))
|
||||
db_call(pool, Q::BuyItems(*player, data.into_inner()))
|
||||
},
|
||||
))
|
||||
.route(web::delete().to_async(
|
||||
move |pool, (player, data): (PlayerId, ItemListWithMods)| {
|
||||
db_call(pool, db::ApiActions::SellItems(*player, data.into_inner()))
|
||||
db_call(pool, Q::SellItems(*player, data.into_inner()))
|
||||
},
|
||||
)),
|
||||
),
|
||||
),
|
||||
)
|
||||
//.route("/claims", web::get().to_async(endpoints::player_claims))
|
||||
.route("/claims", web::get().to_async(|pool| db_call(pool, Q::FetchClaims)))
|
||||
.route(
|
||||
"/items",
|
||||
web::get()
|
||||
.to_async(move |pool: AppPool| db_call(pool, db::ApiActions::FetchInventory)),
|
||||
.to_async(move |pool: AppPool| db_call(pool, Q::FetchInventory)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user