preps undo actions

This commit is contained in:
2019-10-26 11:06:08 +02:00
parent 49dfd8bb14
commit dc0874bd12

View File

@@ -120,25 +120,15 @@ pub fn execute(
query: ApiActions, query: ApiActions,
) -> Result<ApiResponse, diesel::result::Error> { ) -> Result<ApiResponse, diesel::result::Error> {
let mut response = ApiResponse::default(); let mut response = ApiResponse::default();
match query { // TODO: Return an Option<String> that describes what happened.
ApiActions::FetchPlayers => { // If there is some value, store the actions in db so that it can be reversed.
response.set_value(Value::PlayerList(db::Players(conn).all()?)); let _action_text: () = match query {
} ApiActions::FetchPlayers => response.set_value(Value::PlayerList(db::Players(conn).all()?)),
ApiActions::FetchInventory => { ApiActions::FetchInventory => response.set_value(Value::ItemList(db::Inventory(conn).all()?)),
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::FetchClaims => { ApiActions::FetchNotifications(id) => response.set_value(Value::Notifications(db::AsPlayer(conn, id).notifications()?)),
response.set_value(Value::ClaimList(db::fetch_claims(conn)?)); ApiActions::FetchLoot(id) => response.set_value(Value::ItemList(db::LootManager(conn, id).all()?)),
}
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) => { ApiActions::UpdateWealth(id, amount) => {
response.push_update(Update::Wealth( response.push_update(Update::Wealth(
db::AsPlayer(conn, id).update_wealth(amount)?, db::AsPlayer(conn, id).update_wealth(amount)?,
@@ -224,6 +214,19 @@ pub fn execute(
response.push_error(format!("Erreur durant la notification : {:?}", e)); 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<ApiResponse, diesel::result::Error> {
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) Ok(response)
} }