diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-01-10 22:54:07 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-01-10 22:54:07 +0100 |
commit | 91b3fb32d39edb6038e4f3f9ba822b1b6ba84091 (patch) | |
tree | 312e53675351f271d8ce133a3c04accdc1223bb5 /src | |
parent | f6e51533077fa13515c40c0a869ab8c589ceb5d2 (diff) | |
download | rocket-blog-91b3fb32d39edb6038e4f3f9ba822b1b6ba84091.tar.gz rocket-blog-91b3fb32d39edb6038e4f3f9ba822b1b6ba84091.tar.bz2 rocket-blog-91b3fb32d39edb6038e4f3f9ba822b1b6ba84091.zip |
Implement actually updating database.
Diffstat (limited to 'src')
-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."); } } |