aboutsummaryrefslogtreecommitdiffstats
path: root/src/controllers/posts_controller.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/controllers/posts_controller.rs')
-rw-r--r--src/controllers/posts_controller.rs53
1 files changed, 31 insertions, 22 deletions
diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs
index f10f312..7cc3849 100644
--- a/src/controllers/posts_controller.rs
+++ b/src/controllers/posts_controller.rs
@@ -1,20 +1,29 @@
use comrak::{markdown_to_html, ComrakOptions};
-use rocket;
-use rocket::request::Form;
-use rocket::response::{Flash, Redirect};
-use utils;
+use rocket::{
+ response::{Flash, Redirect},
+ request::Form,
+ Route
+};
+use models::{
+ NewPost,
+ Post
+};
+use utils::{
+ DbConn,
+ Page
+};
#[derive(BartDisplay)]
#[template = "templates/new_post.html"]
pub struct NewPostTemplate {
- post: ::models::NewPost
+ post: NewPost
}
implement_responder_for!(NewPostTemplate);
#[get("/new", format = "text/html")]
-fn new(_conn: utils::DbConn) -> utils::Page<NewPostTemplate> {
- utils::Page {
+fn new(_conn: DbConn) -> Page<NewPostTemplate> {
+ Page {
title: String::from("New post"),
flash: None,
content: NewPostTemplate {
@@ -24,15 +33,15 @@ fn new(_conn: utils::DbConn) -> utils::Page<NewPostTemplate> {
}
#[post("/create", data="<post>")]
-fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Flash<Redirect> {
- ::models::Post::create(post.get(), conn);
+fn create(post: Form<NewPost>, conn: DbConn) -> Flash<Redirect> {
+ Post::create(post.get(), conn);
Flash::success(Redirect::to("/"), "Post successfully created!")
}
#[derive(BartDisplay, Serialize)]
#[template = "templates/show_post.html"]
pub struct ShowPostTemplate {
- pub post: ::models::Post
+ pub post: Post
}
implement_responder_for!(ShowPostTemplate);
@@ -44,9 +53,9 @@ impl ShowPostTemplate {
}
#[get("/<id>", format = "text/html")]
-fn show(id: i32, conn: utils::DbConn) -> utils::Page<ShowPostTemplate> {
- let p = ::models::Post::get(id, conn);
- utils::Page {
+fn show(id: i32, conn: DbConn) -> Page<ShowPostTemplate> {
+ let p = Post::get(id, conn);
+ Page {
title: p.title.clone(),
flash: None,
content: ShowPostTemplate {
@@ -58,15 +67,15 @@ fn show(id: i32, conn: utils::DbConn) -> utils::Page<ShowPostTemplate> {
#[derive(BartDisplay)]
#[template = "templates/edit_post.html"]
pub struct EditPostTemplate {
- post: ::models::Post
+ post: Post
}
implement_responder_for!(EditPostTemplate);
#[get("/<id>/edit", format = "text/html")]
-fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditPostTemplate> {
- let p = ::models::Post::get(id, conn);
- utils::Page {
+fn edit(id: i32, conn: DbConn) -> Page<EditPostTemplate> {
+ let p = Post::get(id, conn);
+ Page {
title: String::from("Edit post"),
flash: None,
content: EditPostTemplate { post: p }
@@ -74,18 +83,18 @@ fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditPostTemplate> {
}
#[post("/update", data="<post>")]
-fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Flash<Redirect> {
- ::models::Post::update(post.get(), conn);
+fn update(post: Form<Post>, conn: DbConn) -> Flash<Redirect> {
+ Post::update(post.get(), conn);
Flash::success(Redirect::to("/"), "Post updated successfully!")
}
#[get("/<id>/delete", format = "text/html")]
-fn delete(id: i32, conn: utils::DbConn) -> Flash<Redirect> {
- ::models::Post::delete(id, conn);
+fn delete(id: i32, conn: DbConn) -> Flash<Redirect> {
+ Post::delete(id, conn);
Flash::success(Redirect::to("/"), "Post deleted!")
}
-pub fn routes() -> Vec<rocket::Route> {
+pub fn routes() -> Vec<Route> {
routes![
new,
create,