routing the rest of implemented features, hits response problem

This commit is contained in:
2019-10-20 16:09:16 +02:00
parent ed3ac2abcb
commit edf236ef8c
5 changed files with 60 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ pub enum Value {
ItemList(Vec<db::Item>),
ClaimList(Vec<db::Claim>),
PlayerList(Vec<db::Player>),
Notifications(Vec<String>),
}
impl serde::Serialize for Value {
@@ -30,6 +31,7 @@ impl serde::Serialize for Value {
Value::ItemList(v) => v.serialize(serializer),
Value::ClaimList(v) => v.serialize(serializer),
Value::PlayerList(v) => v.serialize(serializer),
Value::Notifications(v) => v.serialize(serializer),
}
}
}
@@ -85,6 +87,7 @@ pub enum ApiActions {
FetchClaims,
// Player actions
FetchPlayer(i32),
FetchNotifications(i32),
FetchLoot(i32),
UpdateWealth(i32, f64),
BuyItems(i32, Vec<(i32, Option<f64>)>),
@@ -120,6 +123,9 @@ pub fn execute(
ApiActions::FetchPlayer(id) => {
response.set_value(Value::Player(db::Players(conn).find(id)?));
}
ApiActions::FetchNotifications(id) => {
response.set_value(Value::Notifications(db::AsPlayer(conn, id).notifications()?));
}
ApiActions::FetchLoot(id) => {
response.set_value(Value::ItemList(db::LootManager(conn, id).all()?));
}
@@ -190,7 +196,12 @@ pub fn execute(
}
}
response.notify(format!("{} objets lootés !", added_items));
// TODO: notify players throught persistent notifications
// Notify players through persistent notifications
if let Err(e) = db::Players(conn)
.notifiy_all("De nouveaux objets ont été ajoutés au coffre de groupe !")
{
response.push_error(format!("Erreur durant la notification : {:?}", e));
};
}
}
Ok(response)

View File

@@ -1,7 +1,7 @@
use actix_cors::Cors;
use actix_files as fs;
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer};
use futures::Future;
use futures::{Future, IntoFuture};
use std::env;
use lootalot_db as db;
@@ -12,6 +12,12 @@ type PlayerId = web::Path<i32>;
type ItemId = web::Json<i32>;
type ItemListWithMods = web::Json<Vec<(i32, Option<f64>)>>;
#[derive(Serialize, Deserialize, Debug)]
struct NewGroupLoot {
source_name: String,
items: Vec<db::Item>,
}
/// Wraps call to the database query and convert its result as a async HttpResponse
pub fn db_call(
pool: AppPool,
@@ -44,6 +50,9 @@ fn configure_app(config: &mut web::ServiceConfig) {
.route("/", web::get().to_async(|pool, player: PlayerId| {
db_call(pool, Q::FetchPlayer(*player))
}))
.route("/notifications", web::get().to_async(|pool, player: PlayerId| {
db_call(pool, Q::FetchNotifications(*player))
}))
.service(
web::resource("/claims")
//.route(web::get().to_async(endpoints::player_claims))
@@ -83,6 +92,14 @@ fn configure_app(config: &mut web::ServiceConfig) {
db_call(pool, Q::BuyItems(*player, data.into_inner()))
},
))
.route(web::post().to_async(
move |pool, (player, data): (PlayerId, web::Json<NewGroupLoot>)| {
match *player {
0 => db_call(pool, Q::AddLoot(data.items.clone())),
_ => HttpResponse::Forbidden().finish().into_future(),
}
},
))
.route(web::delete().to_async(
move |pool, (player, data): (PlayerId, ItemListWithMods)| {
db_call(pool, Q::SellItems(*player, data.into_inner()))