aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-11-21 23:21:01 +0100
committerHarald Eilertsen <haraldei@anduin.net>2017-11-22 10:57:35 +0100
commit2e98f140e810d5271258df6905e744737734c666 (patch)
tree46f703681acfe4692dbbcd1d35a76c3856009ef3
parentbeede75cefd5ae52c2d81b7f183cfa41953b09a4 (diff)
downloadrocket-blog-2e98f140e810d5271258df6905e744737734c666.tar.gz
rocket-blog-2e98f140e810d5271258df6905e744737734c666.tar.bz2
rocket-blog-2e98f140e810d5271258df6905e744737734c666.zip
Add handlers for creating new Posts.
-rw-r--r--src/main.rs40
-rw-r--r--src/models.rs12
-rw-r--r--templates/index.html1
-rw-r--r--templates/new_post.html29
4 files changed, 81 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 1d20cb0..a709cd9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -32,6 +32,15 @@ impl<'a> Responder<'a> for IndexTemplate<'a> {
}
}
+impl<'a> Responder<'a> for posts::NewPostTemplate<'a> {
+ fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
+ Response::build()
+ .header(ContentType::HTML)
+ .sized_body(Cursor::new(format!("{}", &self)))
+ .ok()
+ }
+}
+
fn get_posts(conn: rocket_blog::DbConn) -> Vec<models::Post> {
use schema::posts::dsl::*;
posts.filter(published.eq(false))
@@ -50,9 +59,40 @@ fn index_json(conn: rocket_blog::DbConn) -> Json<Vec<models::Post>> {
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="<post>")]
+ 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())
.mount("/", routes![index, index_json])
+ .mount("/posts", routes![posts::new, posts::create])
.launch();
}
diff --git a/src/models.rs b/src/models.rs
index d8aa0aa..67038f1 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -1,7 +1,17 @@
-#[derive(Debug, Default, FromForm, Serialize, Queryable)]
+use super::schema::posts;
+
+#[derive(Serialize, Queryable)]
pub struct Post {
pub id: i32,
pub title: String,
pub body: String,
pub published: bool,
}
+
+#[derive(Default, FromForm, Insertable)]
+#[table_name="posts"]
+pub struct NewPost {
+ pub title: String,
+ pub body: String,
+ pub published: bool,
+}
diff --git a/templates/index.html b/templates/index.html
index 2a3a95b..674b867 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -4,6 +4,7 @@
<title>{{ title }}</title>
</head>
<body>
+ <p><a href="/posts/new">Create new post</a></p>
{{# posts }}{{> post_teaser.html }}{{/ posts }}
</body>
</html>
diff --git a/templates/new_post.html b/templates/new_post.html
new file mode 100644
index 0000000..93de696
--- /dev/null
+++ b/templates/new_post.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>{{ title }}</title>
+ </head>
+ <body>
+ <h1>Create a new post</h1>
+ <form id="new_post" name="post" method="post" action="/posts/create">
+ <div class="field">
+ <label>Title:</label>
+ <input type="text" name="title" value="{{ post.title }}">
+ </div>
+
+ <div class="field">
+ <label>Contents:</label>
+ <textarea name="body">{{ post.body }}</textarea>
+ </div>
+
+ <div class="field">
+ <input type="checkbox" checked="{{ post.published }}">
+ <label>Published</label>
+ </div>
+
+ <div class="actions">
+ <input type="submit" value="Create post">
+ </div>
+ </form>
+ </body>
+</html>