aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-12-23 13:00:14 +0100
committerHarald Eilertsen <haraldei@anduin.net>2017-12-23 13:00:14 +0100
commit6109a230ca65cd9a113df72d36247235fe934de4 (patch)
tree4f8b7b6838eb2dd59ac8648afbd65d209d74941f /src
parentd4f1eb033376fab5074c71558ac796c20026ec19 (diff)
downloadrocket-blog-6109a230ca65cd9a113df72d36247235fe934de4.tar.gz
rocket-blog-6109a230ca65cd9a113df72d36247235fe934de4.tar.bz2
rocket-blog-6109a230ca65cd9a113df72d36247235fe934de4.zip
Move get_posts function to models.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs13
-rw-r--r--src/models.rs9
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 {