adds newtype
This commit is contained in:
@@ -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<Update>);
|
||||
|
||||
/// 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<Vec<Update>>,
|
||||
updates: Option<UpdateList>,
|
||||
}
|
||||
|
||||
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> {
|
||||
impl<DB: Backend> FromSql<Text, DB> for UpdateList
|
||||
where
|
||||
String: FromSql<Text, DB>,
|
||||
{
|
||||
fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
|
||||
let repr = String::from_sql(bytes)?;
|
||||
serde_json::from_str(&repr)
|
||||
Ok(UpdateList(serde_json::from_str::<Vec<Update>>(&repr)?))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,3 +58,12 @@ pub fn get_last_of_player(conn: &DbConnection, id: i32) -> QueryResult<Event> {
|
||||
.order(history::dsl::id.desc())
|
||||
.first(conn)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_insert_event_with_updates() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user