impls undoable events
This commit is contained in:
@@ -9,6 +9,9 @@ use crate::{DbConnection, QueryResult, Update};
|
||||
#[derive(Debug, FromSqlRow)]
|
||||
pub struct UpdateList(Vec<Update>);
|
||||
|
||||
// TODO: decide if updates is really optionnal or not
|
||||
// (like if storing an event without update is usefull ?)
|
||||
|
||||
/// An event in history
|
||||
#[derive(Debug, Queryable)]
|
||||
pub struct Event {
|
||||
@@ -19,6 +22,26 @@ pub struct Event {
|
||||
updates: Option<UpdateList>,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn name(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
|
||||
/// TODO: why a move here ??
|
||||
/// Undo all updates in a single transaction
|
||||
pub fn undo(self, conn: &DbConnection) -> QueryResult<Self> {
|
||||
conn.transaction(move || {
|
||||
if let Some(ref updates) = self.updates {
|
||||
for update in updates.0.iter() {
|
||||
update.undo(conn, self.player_id)?;
|
||||
}
|
||||
diesel::delete(history::table.find(self.id)).execute(conn)?;
|
||||
}
|
||||
Ok(self)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<DB: Backend> FromSql<Text, DB> for UpdateList
|
||||
where
|
||||
String: FromSql<Text, DB>,
|
||||
@@ -38,7 +61,11 @@ struct NewEvent<'a> {
|
||||
updates: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
/// Insert a new event
|
||||
///
|
||||
/// # Warning
|
||||
/// This actually swallow up conversion errors
|
||||
pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &Vec<Update>) -> QueryResult<Event> {
|
||||
diesel::insert_into(history::table)
|
||||
.values(&NewEvent {
|
||||
|
||||
Reference in New Issue
Block a user