use ::schema::posts; use diesel::prelude::*; use diesel::{self, ExecuteDsl}; #[derive(FromForm, Serialize, Queryable)] pub struct Post { pub id: i32, pub title: String, pub body: String, pub published: bool, } #[derive(Default, FromForm, Insertable)] #[table_name="posts"] pub struct NewPost { pub title: String, pub body: String, pub published: bool, } impl Post { pub fn get_all(conn: ::DbConn) -> Vec { use ::schema::posts::dsl::*; posts.filter(published.eq(false)) .limit(5) .load::(&*conn) .expect("Error loading posts") } pub fn get(post_id: i32, conn: ::DbConn) -> Post { use ::schema::posts::dsl::*; posts.find(post_id).get_result(&*conn) .expect(&format!("Unable to find post with id={}", post_id)) } pub fn create(new_post: &NewPost, conn: ::DbConn) { diesel::insert(new_post) .into(posts::table) .execute(&*conn) .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."); } }