Add some to_json functions to models for serialization in templates

This commit is contained in:
Bat
2018-06-18 17:34:29 +01:00
parent 58cc35691d
commit 7e3cdec0b6
7 changed files with 28 additions and 77 deletions
+2 -18
View File
@@ -24,26 +24,10 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Temp
let comments = Comment::find_by_post(&*conn, post.id);
Template::render("posts/details", json!({
"author": ({
let author = &post.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
"author": post.get_authors(&*conn)[0].to_json(&*conn),
"post": post,
"blog": blog,
"comments": comments.into_iter().map(|c| {
json!({
"id": c.id,
"content": c.content,
"author": ({
let author = &c.get_author(&*conn);
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
})
})
}).collect::<Vec<serde_json::Value>>(),
"comments": comments.into_iter().map(|c| c.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"n_likes": post.get_likes(&*conn).len(),
"has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
"n_reshares": post.get_reshares(&*conn).len(),