aboutsummaryrefslogtreecommitdiffstats
path: root/src/posts.rs
blob: a5e25245d0fb9148af60cef0a7091f9cce70e281 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use rocket::request::Form;
use rocket::response::Redirect;

#[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 {
    ::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: ::rocket_blog::DbConn) -> ShowPostTemplate {
    let p = ::models::Post::get(id, conn);
    ShowPostTemplate { post: p }
}