From d880d9528e42b82a7a4e4b41fc0f934f6bdc2c84 Mon Sep 17 00:00:00 2001 From: Artus Date: Sun, 27 Oct 2019 21:56:24 +0100 Subject: [PATCH] works on making event models work --- lootalot_db/Cargo.toml | 1 + lootalot_db/src/models/history.rs | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lootalot_db/Cargo.toml b/lootalot_db/Cargo.toml index 18cd0b8..ceb2cb8 100644 --- a/lootalot_db/Cargo.toml +++ b/lootalot_db/Cargo.toml @@ -9,6 +9,7 @@ dotenv = "*" diesel_migrations = "*" serde = "*" serde_derive = "*" +serde_json = "*" [dependencies.diesel] version = "1.4" diff --git a/lootalot_db/src/models/history.rs b/lootalot_db/src/models/history.rs index c45197b..f43cc99 100644 --- a/lootalot_db/src/models/history.rs +++ b/lootalot_db/src/models/history.rs @@ -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, + updates: Option>, } +impl diesel::deserialize::FromSql for Vec { + fn from_sql(bytes: Option<::RawValue>) -> diesel::deserialize::Result { + 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, } /// Insert a new event -pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &str) -> QueryResult { +pub fn insert_event(conn: &DbConnection, id: i32, text: &str, updates: &Vec) -> QueryResult { diesel::insert_into(history::table) - .values(&NewEvent{ + .values(&NewEvent { player_id: id, text, - updates, + updates: serde_json::to_string(updates).ok(), }) .execute(conn)?; history::table