aboutsummaryrefslogblamecommitdiffstats
path: root/src/posts.rs
blob: 350ab53f5a6405253018f934afc52f37dd23ab7c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                       

                                              















                                                                                   
use rocket::request::Form;
use rocket::response::Redirect;
use diesel::{self, ExecuteDsl};

#[derive(BartDisplay)]
#[template = "templates/new_post.html"]
pub struct NewPostTemplate<'a> {
    title: &'a str,
    post: ::models::NewPost
}

implement_responder_for!(NewPostTemplate<'a>);

#[get("/new", format = "text/html")]
fn new<'a>(_conn: ::rocket_blog::DbConn) -> NewPostTemplate<'a> {
    NewPostTemplate { title: "Bloggen", post: Default::default() }
}

#[post("/create", data="<post>")]
fn create(post: Form<::models::NewPost>, conn: ::rocket_blog::DbConn) -> Redirect {
    use ::schema::posts;

    diesel::insert(post.get())
        .into(posts::table)
        .execute(&*conn)
        .expect("Error saving post.");

    Redirect::to("/")
}