From b5a32ebbc86d6b17f4cb00673eb38096fe82defb Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 17 Dec 2017 21:20:14 +0100 Subject: Move posts module to it's own source file. --- src/main.rs | 32 ++------------------------------ src/posts.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 30 deletions(-) create mode 100644 src/posts.rs diff --git a/src/main.rs b/src/main.rs index c94dab5..f89a39f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,8 @@ use rocket::request::Request; use rocket::http::{ContentType, Status}; use std::io::Cursor; +mod posts; + #[derive(BartDisplay, Serialize)] #[template = "templates/index.html"] struct IndexTemplate<'a> { @@ -63,36 +65,6 @@ fn index_json(conn: rocket_blog::DbConn) -> Json> { Json(get_posts(conn)) } -mod posts { - use rocket::request::Form; - use rocket::response::Redirect; - use diesel::{self, ExecuteDsl}; - - #[derive(BartDisplay)] - #[template = "templates/new_post.html"] - pub struct NewPostTemplate<'a> { - title: &'a str, - post: ::models::NewPost - } - - #[get("/new", format = "text/html")] - fn new<'a>(_conn: ::rocket_blog::DbConn) -> NewPostTemplate<'a> { - NewPostTemplate { title: "Bloggen", post: Default::default() } - } - - #[post("/create", data="")] - fn create(post: Form<::models::NewPost>, conn: ::rocket_blog::DbConn) -> Redirect { - use ::schema::posts; - - diesel::insert(post.get()) - .into(posts::table) - .execute(&*conn) - .expect("Error saving post."); - - Redirect::to("/") - } -} - fn main() { rocket::ignite() .manage(rocket_blog::init_db_pool()) diff --git a/src/posts.rs b/src/posts.rs new file mode 100644 index 0000000..a98ea5c --- /dev/null +++ b/src/posts.rs @@ -0,0 +1,27 @@ +use rocket::request::Form; +use rocket::response::Redirect; +use diesel::{self, ExecuteDsl}; + +#[derive(BartDisplay)] +#[template = "templates/new_post.html"] +pub struct NewPostTemplate<'a> { + title: &'a str, + post: ::models::NewPost +} + +#[get("/new", format = "text/html")] +fn new<'a>(_conn: ::rocket_blog::DbConn) -> NewPostTemplate<'a> { + NewPostTemplate { title: "Bloggen", post: Default::default() } +} + +#[post("/create", data="")] +fn create(post: Form<::models::NewPost>, conn: ::rocket_blog::DbConn) -> Redirect { + use ::schema::posts; + + diesel::insert(post.get()) + .into(posts::table) + .execute(&*conn) + .expect("Error saving post."); + + Redirect::to("/") +} -- cgit v1.2.3