aboutsummaryrefslogblamecommitdiffstats
path: root/src/controllers/posts_controller.rs
blob: 38618d362a38eec892880b1c31904c9519429ce8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                               
          


                                       
                            


                           
                                          
 
                                    






                                                              


                                 
                                                                           
                                             

                     









                                           
                                                                        
                                          





                                   
 









                                           
                                                                        
                                          



                                             


                                 
                                                                        


                                           





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

#[derive(BartDisplay)]
#[template = "templates/new_post.html"]
pub struct NewPostTemplate {
    post: ::models::NewPost
}

implement_responder_for!(NewPostTemplate);

#[get("/new", format = "text/html")]
fn new(_conn: utils::DbConn) -> utils::Page<NewPostTemplate> {
    utils::Page {
        title: String::from("New post"),
        content: NewPostTemplate {
            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(id: i32, conn: utils::DbConn) -> utils::Page<ShowPostTemplate> {
    let p = ::models::Post::get(id, conn);
    utils::Page {
        title: p.title.clone(),
        content: 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) -> utils::Page<EditPostTemplate> {
    let p = ::models::Post::get(id, conn);
    utils::Page {
        title: String::from("Edit post"),
        content: 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("/")
}

#[get("/<id>/delete", format = "text/html")]
fn delete(id: i32, conn: utils::DbConn) -> Redirect {
    ::models::Post::delete(id, conn);
    Redirect::to("/")
}