From 0ac2bce183862bb737a5eaaf67bdb2a389ed6741 Mon Sep 17 00:00:00 2001 From: Artus Date: Mon, 28 Oct 2019 00:22:28 +0100 Subject: [PATCH] adds newtype --- lootalot_db/src/models/history.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lootalot_db/src/models/history.rs b/lootalot_db/src/models/history.rs index f43cc99..5b99b93 100644 --- a/lootalot_db/src/models/history.rs +++ b/lootalot_db/src/models/history.rs @@ -1,8 +1,14 @@ -use diesel::prelude::*; use serde_json; +use diesel::prelude::*; +use diesel::sql_types::Text; +use diesel::deserialize::FromSql; +use diesel::backend::Backend; use crate::schema::history; use crate::{DbConnection, QueryResult, Update}; +#[derive(Debug, FromSqlRow)] +pub struct UpdateList(Vec); + /// An event in history #[derive(Debug, Queryable)] pub struct Event { @@ -10,13 +16,16 @@ 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 { +impl FromSql for UpdateList +where + String: FromSql, +{ + fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result { let repr = String::from_sql(bytes)?; - serde_json::from_str(&repr) + Ok(UpdateList(serde_json::from_str::>(&repr)?)) } } @@ -49,3 +58,12 @@ pub fn get_last_of_player(conn: &DbConnection, id: i32) -> QueryResult { .order(history::dsl::id.desc()) .first(conn) } + +#[cfg(test)] +mod tests { + + #[test] + fn test_insert_event_with_updates() { + + } +}