adds forbidden_to_players wrapper for db_call

This commit is contained in:
2019-10-20 21:23:57 +02:00
parent edf236ef8c
commit 05a08ea337

View File

@@ -1,6 +1,6 @@
use actix_cors::Cors;
use actix_files as fs;
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer};
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer, Either};
use futures::{Future, IntoFuture};
use std::env;
@@ -18,8 +18,10 @@ struct NewGroupLoot {
items: Vec<db::Item>,
}
type MaybeForbidden = actix_web::Either<Box<dyn Future<Item = HttpResponse, Error = Error>>, HttpResponse>;
/// Wraps call to the database query and convert its result as a async HttpResponse
pub fn db_call(
fn db_call(
pool: AppPool,
query: api::ApiActions,
) -> impl Future<Item = HttpResponse, Error = Error>
@@ -34,6 +36,15 @@ pub fn db_call(
})
}
fn forbidden_to_players(id: i32, params: (AppPool, api::ApiActions)) -> MaybeForbidden {
if id != 0 {
Either::B(HttpResponse::Forbidden().finish())
} else {
Either::A(Box::new(db_call(params.0, params.1)))
}
}
fn configure_app(config: &mut web::ServiceConfig) {
use api::ApiActions as Q;
config.service(
@@ -92,12 +103,9 @@ fn configure_app(config: &mut web::ServiceConfig) {
db_call(pool, Q::BuyItems(*player, data.into_inner()))
},
))
.route(web::post().to_async(
.route(web::post().to(
move |pool, (player, data): (PlayerId, web::Json<NewGroupLoot>)| {
match *player {
0 => db_call(pool, Q::AddLoot(data.items.clone())),
_ => HttpResponse::Forbidden().finish().into_future(),
}
forbidden_to_players(*player, (pool, Q::AddLoot(data.items.clone())))
},
))
.route(web::delete().to_async(