adds history table

This commit is contained in:
2019-10-27 15:52:46 +01:00
parent 559ce804a7
commit 40e39d5a65
3 changed files with 51 additions and 0 deletions

View 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)
}