diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2017-12-23 16:48:25 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2017-12-23 16:48:25 +0100 |
commit | f91bc37b14ec958f67e217ffa65cdf98b8f7417d (patch) | |
tree | 9ee7a51249e71629311127605ed3148db5cbe580 | |
parent | e5b5a9dc28401815f8e9a68007281d23b3f52825 (diff) | |
download | rocket-blog-f91bc37b14ec958f67e217ffa65cdf98b8f7417d.tar.gz rocket-blog-f91bc37b14ec958f67e217ffa65cdf98b8f7417d.tar.bz2 rocket-blog-f91bc37b14ec958f67e217ffa65cdf98b8f7417d.zip |
Implement posts/show handler.
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/models.rs | 6 | ||||
-rw-r--r-- | src/posts.rs | 14 | ||||
-rw-r--r-- | templates/show_post.html | 19 |
4 files changed, 40 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 4aa448d..ed1d72a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,6 @@ fn main() { rocket::ignite() .manage(rocket_blog::init_db_pool()) .mount("/", routes![index, index_json]) - .mount("/posts", routes![posts::new, posts::create]) + .mount("/posts", routes![posts::new, posts::create, posts::show]) .launch(); } diff --git a/src/models.rs b/src/models.rs index 4259d2c..ffa07c4 100644 --- a/src/models.rs +++ b/src/models.rs @@ -17,6 +17,12 @@ impl Post { .load::<Post>(&*conn) .expect("Error loading posts") } + + pub fn get(post_id: i32, conn: ::DbConn) -> Post { + use super::schema::posts::dsl::*; + posts.find(post_id).get_result(&*conn) + .expect(&format!("Unable to find post with id={}", post_id)) + } } #[derive(Default, FromForm, Insertable)] diff --git a/src/posts.rs b/src/posts.rs index 350ab53..a9cbcf1 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -27,3 +27,17 @@ fn create(post: Form<::models::NewPost>, conn: ::rocket_blog::DbConn) -> Redirec 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 } +} diff --git a/templates/show_post.html b/templates/show_post.html new file mode 100644 index 0000000..5a7d806 --- /dev/null +++ b/templates/show_post.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> + <head> + <title>{{ post.title }}</title> + </head> + <body> + <article class="post-teaser"> + <header> + <h1>{{ post.title }}</h1> + <div class="post-actions"> + Edit | Publish | Delete + </div> + </header> + <section class="teaser"> + {{ post.body }} + </section> + </article> + </body> +</html> |