From dc0874bd12d69cca4263c190d3069320e043a56c Mon Sep 17 00:00:00 2001 From: Artus Date: Sat, 26 Oct 2019 11:06:08 +0200 Subject: [PATCH] preps undo actions --- src/api.rs | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/api.rs b/src/api.rs index 26f3a0c..d0cbc05 100644 --- a/src/api.rs +++ b/src/api.rs @@ -120,25 +120,15 @@ pub fn execute( query: ApiActions, ) -> Result { let mut response = ApiResponse::default(); - match query { - ApiActions::FetchPlayers => { - response.set_value(Value::PlayerList(db::Players(conn).all()?)); - } - ApiActions::FetchInventory => { - response.set_value(Value::ItemList(db::Inventory(conn).all()?)); - } - ApiActions::FetchClaims => { - response.set_value(Value::ClaimList(db::fetch_claims(conn)?)); - } - 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()?)); - } + // TODO: Return an Option that describes what happened. + // If there is some value, store the actions in db so that it can be reversed. + let _action_text: () = match query { + ApiActions::FetchPlayers => response.set_value(Value::PlayerList(db::Players(conn).all()?)), + ApiActions::FetchInventory => response.set_value(Value::ItemList(db::Inventory(conn).all()?)), + ApiActions::FetchClaims => response.set_value(Value::ClaimList(db::fetch_claims(conn)?)), + 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()?)), ApiActions::UpdateWealth(id, amount) => { response.push_update(Update::Wealth( db::AsPlayer(conn, id).update_wealth(amount)?, @@ -224,6 +214,19 @@ pub fn execute( response.push_error(format!("Erreur durant la notification : {:?}", e)); }; } - } + }; + // match _action_text -> Save updates in DB + Ok(response) +} + +/// Reverts the last action stored for player +fn revert_last_action( + conn: &DbConnection, + id: i32, +) -> Result { + let mut response = ApiResponse::default(); + // 1. Load the last action + // 2. Iterate trought updates and reverse them (in a single transaction) + // 3. Send nice message back ("'action desc text' annulé !") Ok(response) }