adds history table
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE history;
|
||||||
8
lootalot_db/migrations/2019-10-27-135235_history/up.sql
Normal file
8
lootalot_db/migrations/2019-10-27-135235_history/up.sql
Normal file
@@ -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)
|
||||||
|
);
|
||||||
42
lootalot_db/src/models/history.rs
Normal file
42
lootalot_db/src/models/history.rs
Normal file
@@ -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<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<Event> {
|
||||||
|
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<Event> {
|
||||||
|
history::table
|
||||||
|
.filter(history::dsl::player_id.eq(id))
|
||||||
|
.order(history::dsl::id.desc())
|
||||||
|
.first(conn)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user