diff options
-rw-r--r-- | src/models/post.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/models/post.rs b/src/models/post.rs index f7c35ac..b8453ce 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -2,7 +2,7 @@ use ::schema::posts; use diesel::prelude::*; use diesel::{self, ExecuteDsl}; -#[derive(FromForm, Serialize, Queryable)] +#[derive(AsChangeset, FromForm, Identifiable, Serialize, Queryable)] pub struct Post { pub id: i32, pub title: String, @@ -27,12 +27,17 @@ impl Post { .expect("Error loading posts") } - pub fn get(post_id: i32, conn: ::DbConn) -> Post { + fn get_internal(post_id: i32, conn: &::DbConn) -> Post { use ::schema::posts::dsl::*; - posts.find(post_id).get_result(&*conn) + posts.find(post_id) + .get_result(&**conn) .expect(&format!("Unable to find post with id={}", post_id)) } + pub fn get(post_id: i32, conn: ::DbConn) -> Post { + Post::get_internal(post_id, &conn) + } + pub fn create(new_post: &NewPost, conn: ::DbConn) { diesel::insert(new_post) .into(posts::table) @@ -40,12 +45,11 @@ impl Post { .expect("Error saving post."); } - pub fn update(_updated_post: &Post, _conn: ::DbConn) { - //use ::schema::posts::dsl::*; - - //diesel::update(updated_post) - // .into(posts) - // .execute(&*conn) - // .expect("Error saving post."); + pub fn update(updated_post: &Post, conn: ::DbConn) { + let p = Post::get_internal(updated_post.id, &conn); + diesel::update(&p) + .set(updated_post) + .execute(&*conn) + .expect("Error saving post."); } } |