diff options
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/models.rs | 9 |
2 files changed, 11 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 0cf1acf..84fe239 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ extern crate rocket_contrib; #[macro_use] extern crate bart_derive; #[macro_use] extern crate serde_derive; -use self::diesel::prelude::*; use self::rocket_blog::{schema, models}; use rocket_contrib::Json; @@ -23,22 +22,14 @@ struct IndexTemplate<'a> { implement_responder_for!(IndexTemplate<'a>); -fn get_posts(conn: rocket_blog::DbConn) -> Vec<models::Post> { - use schema::posts::dsl::*; - posts.filter(published.eq(false)) - .limit(5) - .load::<models::Post>(&*conn) - .expect("Error loading posts") -} - #[get("/", format = "text/html")] fn index<'a>(conn: rocket_blog::DbConn) -> IndexTemplate<'a> { - IndexTemplate { title: "Bloggen", posts: get_posts(conn) } + IndexTemplate { title: "Bloggen", posts: models::get_posts(conn) } } #[get("/", format = "application/json")] fn index_json(conn: rocket_blog::DbConn) -> Json<Vec<models::Post>> { - Json(get_posts(conn)) + Json(models::get_posts(conn)) } fn main() { diff --git a/src/models.rs b/src/models.rs index 67038f1..8bf933e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,4 +1,5 @@ use super::schema::posts; +use diesel::prelude::*; #[derive(Serialize, Queryable)] pub struct Post { @@ -8,6 +9,14 @@ pub struct Post { pub published: bool, } +pub fn get_posts(conn: ::DbConn) -> Vec<Post> { + use super::schema::posts::dsl::*; + posts.filter(published.eq(false)) + .limit(5) + .load::<Post>(&*conn) + .expect("Error loading posts") +} + #[derive(Default, FromForm, Insertable)] #[table_name="posts"] pub struct NewPost { |