Save post author

This commit is contained in:
Bat 2018-04-23 15:39:06 +01:00
parent a816bb00b7
commit 9eb1b987b2

View File

@ -8,6 +8,7 @@ use utils;
use db_conn::DbConn; use db_conn::DbConn;
use models::blogs::*; use models::blogs::*;
use models::post::*; use models::post::*;
use models::post_authors::*;
use models::user::User; use models::user::User;
#[get("/~/<blog>/<slug>", rank = 3)] #[get("/~/<blog>/<slug>", rank = 3)]
@ -35,11 +36,11 @@ struct NewPostForm {
} }
#[post("/~/<blog_name>/new", data = "<data>")] #[post("/~/<blog_name>/new", data = "<data>")]
fn create(blog_name: String, data: Form<NewPostForm>, _user: User, conn: DbConn) -> Redirect { fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn) -> Redirect {
let blog = Blog::find_by_actor_id(&*conn, blog_name.to_string()).unwrap(); let blog = Blog::find_by_actor_id(&*conn, blog_name.to_string()).unwrap();
let form = data.get(); let form = data.get();
let slug = form.title.to_string().to_kebab_case(); let slug = form.title.to_string().to_kebab_case();
Post::insert(&*conn, NewPost { let post = Post::insert(&*conn, NewPost {
blog_id: blog.id, blog_id: blog.id,
slug: slug.to_string(), slug: slug.to_string(),
title: form.title.to_string(), title: form.title.to_string(),
@ -47,5 +48,9 @@ fn create(blog_name: String, data: Form<NewPostForm>, _user: User, conn: DbConn)
published: true, published: true,
license: form.license.to_string() license: form.license.to_string()
}); });
PostAuthor::insert(&*conn, NewPostAuthor {
post_id: post.id,
author_id: user.id
});
Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str()) Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str())
} }