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

                                
  
                          


                                       
                            
                  

 
                                          
 
                                    

                                                
                                        
                    
                                  

                                     
     

 
                                   
                                                                 
                                    
                                                                   
 
 
                      

                                        
                   



                                           





                                                                    
                                     
                                                          
                                 
          
                               
                    
                                              
     
 



                                        
               




                                           
                                                          
                                 
          
                                         
                    
                                              
     

 
                                   
                                                              
                                    
                                                                   
 

                                            
                                                     
                            
                                                      
 
 
                               
                                                    
 
#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
use comrak::{markdown_to_html, ComrakOptions};
use models::{NewPost, Post};
use rocket::{
    request::Form,
    response::{Flash, Redirect},
    Route,
};
use utils::{DbConn, Page};

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

implement_responder_for!(NewPostTemplate);

#[get("/new", format = "text/html")]
fn new(_conn: DbConn) -> Page<NewPostTemplate> {
    Page {
        title: String::from("New post"),
        flash: None,
        content: NewPostTemplate {
            post: Default::default(),
        },
    }
}

#[post("/create", data = "<post>")]
fn create(post: Form<NewPost>, conn: DbConn) -> Flash<Redirect> {
    Post::create(post.get(), &conn);
    Flash::success(Redirect::to("/"), "Post successfully created!")
}

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

implement_responder_for!(ShowPostTemplate);

impl ShowPostTemplate {
    pub fn rendered_body(&self) -> String {
        markdown_to_html(&self.post.body, &ComrakOptions::default())
    }
}

#[get("/<id>", format = "text/html")]
fn show(id: i32, conn: DbConn) -> Page<ShowPostTemplate> {
    let p = Post::get(id, &conn);
    Page {
        title: p.title.clone(),
        flash: None,
        content: ShowPostTemplate { post: p },
    }
}

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

implement_responder_for!(EditPostTemplate);

#[get("/<id>/edit", format = "text/html")]
fn edit(id: i32, conn: DbConn) -> Page<EditPostTemplate> {
    let p = Post::get(id, &conn);
    Page {
        title: String::from("Edit post"),
        flash: None,
        content: EditPostTemplate { post: p },
    }
}

#[post("/update", data = "<post>")]
fn update(post: Form<Post>, conn: DbConn) -> Flash<Redirect> {
    Post::update(post.get(), &conn);
    Flash::success(Redirect::to("/"), "Post updated successfully!")
}

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

pub fn routes() -> Vec<Route> {
    routes![new, create, show, edit, update, delete]
}