works on making event models work
This commit is contained in:
@@ -9,6 +9,7 @@ dotenv = "*"
|
||||
diesel_migrations = "*"
|
||||
serde = "*"
|
||||
serde_derive = "*"
|
||||
serde_json = "*"
|
||||
|
||||
[dependencies.diesel]
|
||||
version = "1.4"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use diesel::prelude::*;
|
||||
use serde_json;
|
||||
use crate::schema::history;
|
||||
use crate::{DbConnection, QueryResult};
|
||||
use crate::{DbConnection, QueryResult, Update};
|
||||
|
||||
/// An event in history
|
||||
#[derive(Debug, Queryable)]
|
||||
@@ -9,24 +10,32 @@ pub struct Event {
|
||||
player_id: i32,
|
||||
event_date: String,
|
||||
text: String,
|
||||
updates: Option<String>,
|
||||
updates: Option<Vec<Update>>,
|
||||
}
|
||||
|
||||
impl diesel::deserialize::FromSql<diesel::sql_types::Text, diesel::sqlite::Sqlite> for Vec<Update> {
|
||||
fn from_sql(bytes: Option<<diesel::sqlite::Sqlite as diesel::backend::Backend>::RawValue>) -> diesel::deserialize::Result<Self> {
|
||||
let repr = String::from_sql(bytes)?;
|
||||
serde_json::from_str(&repr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Insertable)]
|
||||
#[table_name = "history"]
|
||||
struct NewEvent<'a> {
|
||||
player_id: i32,
|
||||
text: &'a str,
|
||||
updates: &'a str,
|
||||
updates: Option<String>,
|
||||
}
|
||||
|
||||
/// Insert a new event
|
||||
pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &str) -> QueryResult<Event> {
|
||||
pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &Vec<Update>) -> QueryResult<Event> {
|
||||
diesel::insert_into(history::table)
|
||||
.values(&NewEvent {
|
||||
player_id: id,
|
||||
text,
|
||||
updates,
|
||||
updates: serde_json::to_string(updates).ok(),
|
||||
})
|
||||
.execute(conn)?;
|
||||
history::table
|
||||
|
||||
Reference in New Issue
Block a user