diff options
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/models.rs | 13 | ||||
-rw-r--r-- | src/posts.rs | 20 | ||||
-rw-r--r-- | templates/edit_post.html | 30 | ||||
-rw-r--r-- | templates/post_teaser.html | 4 |
5 files changed, 63 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index 40fa802..5084fbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,6 @@ fn main() { rocket::ignite() .manage(rocket_blog::init_db_pool()) .mount("/", routes![index, index_json]) - .mount("/posts", routes![posts::new, posts::create, posts::show]) + .mount("/posts", routes![posts::new, posts::create, posts::show, posts::edit, posts::update]) .launch(); } diff --git a/src/models.rs b/src/models.rs index 325ee44..33a5fe7 100644 --- a/src/models.rs +++ b/src/models.rs @@ -2,7 +2,7 @@ use super::schema::posts; use diesel::prelude::*; use diesel::{self, ExecuteDsl}; -#[derive(Serialize, Queryable)] +#[derive(FromForm, Serialize, Queryable)] pub struct Post { pub id: i32, pub title: String, @@ -34,11 +34,18 @@ impl Post { } pub fn create(new_post: &NewPost, conn: ::DbConn) { - use super::schema::posts; - diesel::insert(new_post) .into(posts::table) .execute(&*conn) .expect("Error saving post."); } + + pub fn update(_updated_post: &Post, _conn: ::DbConn) { + //use super::schema::posts::dsl::*; + + //diesel::update(updated_post) + // .into(posts) + // .execute(&*conn) + // .expect("Error saving post."); + } } diff --git a/src/posts.rs b/src/posts.rs index a5e2524..859cce5 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -34,3 +34,23 @@ fn show<'a>(id: i32, conn: ::rocket_blog::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: ::rocket_blog::DbConn) -> EditPostTemplate { + let p = ::models::Post::get(id, conn); + EditPostTemplate { post: p } +} + +#[post("/update", data="<post>")] +fn update(post: Form<::models::Post>, conn: ::rocket_blog::DbConn) -> Redirect { + ::models::Post::update(post.get(), conn); + Redirect::to("/") +} diff --git a/templates/edit_post.html b/templates/edit_post.html new file mode 100644 index 0000000..9154e3d --- /dev/null +++ b/templates/edit_post.html @@ -0,0 +1,30 @@ +<!DOCTYPE html> +<html> + <head> + <title>Edit {{ post.title }}</title> + </head> + <body> + <h1>Edit post</h1> + <form id="new_post" name="post" method="post" action="/posts/update"> + <input type="hidden" name="id" value="{{ post.id }}"> + <div class="field"> + <label>Title:</label> + <input type="text" name="title" value="{{ post.title }}"> + </div> + + <div class="field"> + <label>Contents:</label> + <textarea name="body">{{ post.body }}</textarea> + </div> + + <div class="field"> + <input type="checkbox" checked="{{ post.published }}"> + <label>Published</label> + </div> + + <div class="actions"> + <input type="submit" value="Save"> + </div> + </form> + </body> +</html> diff --git a/templates/post_teaser.html b/templates/post_teaser.html index 19a30ec..751f4ea 100644 --- a/templates/post_teaser.html +++ b/templates/post_teaser.html @@ -2,10 +2,10 @@ <header> <h1><a href="/posts/{{ .id }}">{{ .title }}</a></h1> <div class="post-actions"> - Edit | Publish | Delete + <a href="/posts/{{ .id }}/edit">Edit</a> | Publish | Delete </div> </header> <section class="teaser"> {{ .body }} </section> -</article>
\ No newline at end of file +</article> |