From 6109a230ca65cd9a113df72d36247235fe934de4 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 23 Dec 2017 13:00:14 +0100 Subject: Move get_posts function to models. --- src/main.rs | 13 ++----------- 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 { - use schema::posts::dsl::*; - posts.filter(published.eq(false)) - .limit(5) - .load::(&*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> { - 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 { + use super::schema::posts::dsl::*; + posts.filter(published.eq(false)) + .limit(5) + .load::(&*conn) + .expect("Error loading posts") +} + #[derive(Default, FromForm, Insertable)] #[table_name="posts"] pub struct NewPost { -- cgit v1.2.3