Implement ToSql for custom types in Site model.
This commit is contained in:
parent
9f02fe5f75
commit
11311997fd
35
src/lib.rs
35
src/lib.rs
|
@ -20,8 +20,13 @@ pub mod security;
|
|||
pub mod web;
|
||||
|
||||
use diesel::Queryable;
|
||||
use diesel::serialize::{Output, Result, ToSql};
|
||||
use diesel::sql_types::Jsonb;
|
||||
use diesel::pg::Pg;
|
||||
use std::collections::HashMap;
|
||||
use std::default::Default;
|
||||
use std::io::Write;
|
||||
use std::vec::Vec;
|
||||
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use serde_json::Value;
|
||||
|
@ -144,9 +149,9 @@ pub struct Site {
|
|||
pub landing_page: String,
|
||||
pub header: String,
|
||||
pub footer: String,
|
||||
pub menu: Vec<MenuItems>,
|
||||
pub menu: MenuItemsWrapper,
|
||||
|
||||
pub config: HashMap<String, Value>,
|
||||
pub config: ConfigWrapper,
|
||||
|
||||
#[serde(skip_deserializing)]
|
||||
links: HashMap<String, Value>,
|
||||
|
@ -207,9 +212,31 @@ impl Queryable<schema::sites::SqlType, diesel::pg::Pg> for Site {
|
|||
landing_page: row.2,
|
||||
header: row.3,
|
||||
footer: row.4,
|
||||
menu,
|
||||
config,
|
||||
menu: MenuItemsWrapper(menu),
|
||||
config: ConfigWrapper(config),
|
||||
links: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
pub struct MenuItemsWrapper(Vec<MenuItems>);
|
||||
|
||||
impl ToSql<Jsonb, Pg> for MenuItemsWrapper {
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> Result {
|
||||
let value = serde_json::to_value(*(Box::new(&self.0))).unwrap();
|
||||
ToSql::<Jsonb, diesel::pg::Pg>::to_sql(&value, out)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
pub struct ConfigWrapper(HashMap<String, Value>);
|
||||
|
||||
impl ToSql<Jsonb, Pg> for ConfigWrapper {
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> Result {
|
||||
let value = serde_json::to_value(*(Box::new(&self.0))).unwrap();
|
||||
ToSql::<Jsonb, diesel::pg::Pg>::to_sql(&value, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -303,8 +303,8 @@ fn update_site((request, updated_site): (HttpRequest, Json<Site>)) -> HttpRespon
|
|||
landing_page.eq(&updated_site.landing_page),
|
||||
header.eq(&updated_site.header),
|
||||
footer.eq(&updated_site.footer),
|
||||
menu.eq(&updated_site.menu),
|
||||
config.eq(&updated_site.config),
|
||||
menu.eq(Some(&updated_site.menu)),
|
||||
config.eq(Some(&updated_site.config)),
|
||||
))
|
||||
.get_result(&database);
|
||||
|
||||
|
|
Loading…
Reference in New Issue