diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-01-11 22:41:09 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-01-11 22:41:09 +0100 |
commit | 22bac37fcaa1b1cdf8755754184ec8b3ace313b1 (patch) | |
tree | 41e6b4ec69b173313c257d9c74b8e62fce6028b1 | |
parent | 757770635c4c2537b03f9b1fb1576a8bf4609a72 (diff) | |
download | rocket-blog-22bac37fcaa1b1cdf8755754184ec8b3ace313b1.tar.gz rocket-blog-22bac37fcaa1b1cdf8755754184ec8b3ace313b1.tar.bz2 rocket-blog-22bac37fcaa1b1cdf8755754184ec8b3ace313b1.zip |
Drop the lib.
The misc stuff from the lib root was moved to the utils module (terrible
name, it's meant to be temporary.) The modules under the lib has been
moved directly under the app.
-rw-r--r-- | src/main.rs | 19 | ||||
-rw-r--r-- | src/models/post.rs | 13 | ||||
-rw-r--r-- | src/posts.rs | 11 | ||||
-rw-r--r-- | src/utils/mod.rs (renamed from src/lib.rs) | 17 |
4 files changed, 26 insertions, 34 deletions
diff --git a/src/main.rs b/src/main.rs index 5084fbc..109d7d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,22 @@ -#![feature(plugin)] +#![feature(plugin, custom_derive)] #![plugin(rocket_codegen)] -#[macro_use] extern crate rocket_blog; #[macro_use] extern crate bart_derive; +#[macro_use] extern crate diesel_codegen; +#[macro_use] extern crate diesel; #[macro_use] extern crate serde_derive; -extern crate diesel; +extern crate dotenv; +extern crate r2d2; +extern crate r2d2_diesel; extern crate rocket; extern crate rocket_contrib; -use rocket_blog::models; use rocket_contrib::Json; +#[macro_use] mod utils; +mod models; +mod schema; mod posts; #[derive(BartDisplay, Serialize)] @@ -24,18 +29,18 @@ struct IndexTemplate<'a> { implement_responder_for!(IndexTemplate<'a>); #[get("/", format = "text/html")] -fn index<'a>(conn: rocket_blog::DbConn) -> IndexTemplate<'a> { +fn index<'a>(conn: utils::DbConn) -> IndexTemplate<'a> { IndexTemplate { title: "Bloggen", posts: models::Post::get_all(conn) } } #[get("/", format = "application/json")] -fn index_json(conn: rocket_blog::DbConn) -> Json<Vec<models::Post>> { +fn index_json(conn: utils::DbConn) -> Json<Vec<models::Post>> { Json(models::Post::get_all(conn)) } fn main() { rocket::ignite() - .manage(rocket_blog::init_db_pool()) + .manage(utils::init_db_pool()) .mount("/", routes![index, index_json]) .mount("/posts", routes![posts::new, posts::create, posts::show, posts::edit, posts::update]) .launch(); diff --git a/src/models/post.rs b/src/models/post.rs index b8453ce..922dbca 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -1,6 +1,7 @@ -use ::schema::posts; +use schema::posts; use diesel::prelude::*; use diesel::{self, ExecuteDsl}; +use utils; #[derive(AsChangeset, FromForm, Identifiable, Serialize, Queryable)] pub struct Post { @@ -19,7 +20,7 @@ pub struct NewPost { } impl Post { - pub fn get_all(conn: ::DbConn) -> Vec<Post> { + pub fn get_all(conn: utils::DbConn) -> Vec<Post> { use ::schema::posts::dsl::*; posts.filter(published.eq(false)) .limit(5) @@ -27,25 +28,25 @@ impl Post { .expect("Error loading posts") } - fn get_internal(post_id: i32, conn: &::DbConn) -> Post { + fn get_internal(post_id: i32, conn: &utils::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 get(post_id: i32, conn: ::DbConn) -> Post { + pub fn get(post_id: i32, conn: utils::DbConn) -> Post { Post::get_internal(post_id, &conn) } - pub fn create(new_post: &NewPost, conn: ::DbConn) { + pub fn create(new_post: &NewPost, conn: utils::DbConn) { diesel::insert(new_post) .into(posts::table) .execute(&*conn) .expect("Error saving post."); } - pub fn update(updated_post: &Post, conn: ::DbConn) { + pub fn update(updated_post: &Post, conn: utils::DbConn) { let p = Post::get_internal(updated_post.id, &conn); diesel::update(&p) .set(updated_post) diff --git a/src/posts.rs b/src/posts.rs index 859cce5..fde08a8 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -1,5 +1,6 @@ use rocket::request::Form; use rocket::response::Redirect; +use utils; #[derive(BartDisplay)] #[template = "templates/new_post.html"] @@ -11,12 +12,12 @@ pub struct NewPostTemplate<'a> { implement_responder_for!(NewPostTemplate<'a>); #[get("/new", format = "text/html")] -fn new<'a>(_conn: ::rocket_blog::DbConn) -> NewPostTemplate<'a> { +fn new<'a>(_conn: utils::DbConn) -> NewPostTemplate<'a> { NewPostTemplate { title: "Bloggen", post: Default::default() } } #[post("/create", data="<post>")] -fn create(post: Form<::models::NewPost>, conn: ::rocket_blog::DbConn) -> Redirect { +fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Redirect { ::models::Post::create(post.get(), conn); Redirect::to("/") } @@ -30,7 +31,7 @@ pub struct ShowPostTemplate { implement_responder_for!(ShowPostTemplate); #[get("/<id>", format = "text/html")] -fn show<'a>(id: i32, conn: ::rocket_blog::DbConn) -> ShowPostTemplate { +fn show<'a>(id: i32, conn: utils::DbConn) -> ShowPostTemplate { let p = ::models::Post::get(id, conn); ShowPostTemplate { post: p } } @@ -44,13 +45,13 @@ pub struct EditPostTemplate { implement_responder_for!(EditPostTemplate); #[get("/<id>/edit", format = "text/html")] -fn edit(id: i32, conn: ::rocket_blog::DbConn) -> EditPostTemplate { +fn edit(id: i32, conn: utils::DbConn) -> EditPostTemplate { let p = ::models::Post::get(id, conn); EditPostTemplate { post: p } } #[post("/update", data="<post>")] -fn update(post: Form<::models::Post>, conn: ::rocket_blog::DbConn) -> Redirect { +fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Redirect { ::models::Post::update(post.get(), conn); Redirect::to("/") } diff --git a/src/lib.rs b/src/utils/mod.rs index ec821e6..1d697fe 100644 --- a/src/lib.rs +++ b/src/utils/mod.rs @@ -1,19 +1,5 @@ -#![feature(plugin, custom_derive)] -#![plugin(rocket_codegen)] - -#[macro_use] extern crate diesel_codegen; -#[macro_use] extern crate diesel; -#[macro_use] extern crate serde_derive; - -extern crate dotenv; -extern crate r2d2_diesel; -extern crate r2d2; -extern crate rocket; - -pub mod models; -pub mod schema; - use diesel::pg::PgConnection; +use r2d2; use r2d2_diesel::ConnectionManager; use dotenv::dotenv; use std::env; @@ -65,7 +51,6 @@ impl Deref for DbConn { } } -#[macro_export] macro_rules! implement_responder_for { // Implement a responder for the given template type // |