diff --git a/lootalot_db/migrations/2019-10-27-135235_history/down.sql b/lootalot_db/migrations/2019-10-27-135235_history/down.sql new file mode 100644 index 0000000..c412ef7 --- /dev/null +++ b/lootalot_db/migrations/2019-10-27-135235_history/down.sql @@ -0,0 +1 @@ +DROP TABLE history; diff --git a/lootalot_db/migrations/2019-10-27-135235_history/up.sql b/lootalot_db/migrations/2019-10-27-135235_history/up.sql new file mode 100644 index 0000000..f2f93b5 --- /dev/null +++ b/lootalot_db/migrations/2019-10-27-135235_history/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE history ( + id INTEGER PRIMARY KEY NOT NULL, + player_id INTEGER NOT NULL, + event_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + text VARCHAR NOT NULL, + updates VARCHAR, + FOREIGN KEY (player_id) REFERENCES players(id) +); diff --git a/lootalot_db/src/models/history.rs b/lootalot_db/src/models/history.rs new file mode 100644 index 0000000..c45197b --- /dev/null +++ b/lootalot_db/src/models/history.rs @@ -0,0 +1,42 @@ +use diesel::prelude::*; +use crate::schema::history; +use crate::{DbConnection, QueryResult}; + +/// An event in history +#[derive(Debug, Queryable)] +pub struct Event { + id: i32, + player_id: i32, + event_date: String, + text: String, + updates: Option, +} + +#[derive(Debug, Insertable)] +#[table_name = "history"] +struct NewEvent<'a> { + player_id: i32, + text: &'a str, + updates: &'a str, +} + +/// Insert a new event +pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &str) -> QueryResult { + diesel::insert_into(history::table) + .values(&NewEvent{ + player_id: id, + text, + updates, + }) + .execute(conn)?; + history::table + .order(history::dsl::id.desc()) + .first(conn) +} + +pub fn get_last_of_player(conn: &DbConnection, id: i32) -> QueryResult { + history::table + .filter(history::dsl::player_id.eq(id)) + .order(history::dsl::id.desc()) + .first(conn) +}