From f6e51533077fa13515c40c0a869ab8c589ceb5d2 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 10 Jan 2018 21:14:50 +0100 Subject: Make proper module of models --- src/models.rs | 51 --------------------------------------------------- src/models/mod.rs | 3 +++ src/models/post.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 51 deletions(-) delete mode 100644 src/models.rs create mode 100644 src/models/mod.rs create mode 100644 src/models/post.rs (limited to 'src') diff --git a/src/models.rs b/src/models.rs deleted file mode 100644 index 33a5fe7..0000000 --- a/src/models.rs +++ /dev/null @@ -1,51 +0,0 @@ -use super::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 super::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 super::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 super::schema::posts::dsl::*; - - //diesel::update(updated_post) - // .into(posts) - // .execute(&*conn) - // .expect("Error saving post."); - } -} diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..0d38f29 --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1,3 @@ +pub mod post; +pub use self::post::Post; +pub use self::post::NewPost; diff --git a/src/models/post.rs b/src/models/post.rs new file mode 100644 index 0000000..f7c35ac --- /dev/null +++ b/src/models/post.rs @@ -0,0 +1,51 @@ +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."); + } +} -- cgit v1.2.3