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

                               
          







                                       

                                              
                                    
                                                         



                                                                  
                                                                           
                                             

                     









                                           
                                                               


                                          









                                           
                                                           




                                          
                                                                        


                                           
use rocket::request::Form;
use rocket::response::Redirect;
use utils;

#[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: utils::DbConn) -> NewPostTemplate<'a> {
    NewPostTemplate { title: "Bloggen", post: Default::default() }
}

#[post("/create", data="<post>")]
fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Redirect {
    ::models::Post::create(post.get(), conn);
    Redirect::to("/")
}

#[derive(BartDisplay)]
#[template = "templates/show_post.html"]
pub struct ShowPostTemplate {
    post: ::models::Post
}

implement_responder_for!(ShowPostTemplate);

#[get("/<id>", format = "text/html")]
fn show<'a>(id: i32, conn: utils::DbConn) -> ShowPostTemplate {
    let p = ::models::Post::get(id, conn);
    ShowPostTemplate { post: p }
}

#[derive(BartDisplay)]
#[template = "templates/edit_post.html"]
pub struct EditPostTemplate {
    post: ::models::Post
}

implement_responder_for!(EditPostTemplate);

#[get("/<id>/edit", format = "text/html")]
fn edit(id: i32, conn: utils::DbConn) -> EditPostTemplate {
    let p = ::models::Post::get(id, conn);
    EditPostTemplate { post: p }
}

#[post("/update", data="<post>")]
fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Redirect {
  ::models::Post::update(post.get(), conn);
  Redirect::to("/")
}