adds newtype
This commit is contained in:
@@ -1,8 +1,14 @@
|
|||||||
use diesel::prelude::*;
|
|
||||||
use serde_json;
|
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::schema::history;
|
||||||
use crate::{DbConnection, QueryResult, Update};
|
use crate::{DbConnection, QueryResult, Update};
|
||||||
|
|
||||||
|
#[derive(Debug, FromSqlRow)]
|
||||||
|
pub struct UpdateList(Vec<Update>);
|
||||||
|
|
||||||
/// An event in history
|
/// An event in history
|
||||||
#[derive(Debug, Queryable)]
|
#[derive(Debug, Queryable)]
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
@@ -10,13 +16,16 @@ pub struct Event {
|
|||||||
player_id: i32,
|
player_id: i32,
|
||||||
event_date: String,
|
event_date: String,
|
||||||
text: String,
|
text: String,
|
||||||
updates: Option<Vec<Update>>,
|
updates: Option<UpdateList>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl diesel::deserialize::FromSql<diesel::sql_types::Text, diesel::sqlite::Sqlite> for Vec<Update> {
|
impl<DB: Backend> FromSql<Text, DB> for UpdateList
|
||||||
fn from_sql(bytes: Option<<diesel::sqlite::Sqlite as diesel::backend::Backend>::RawValue>) -> diesel::deserialize::Result<Self> {
|
where
|
||||||
|
String: FromSql<Text, DB>,
|
||||||
|
{
|
||||||
|
fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
|
||||||
let repr = String::from_sql(bytes)?;
|
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())
|
.order(history::dsl::id.desc())
|
||||||
.first(conn)
|
.first(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_insert_event_with_updates() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user