aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-01-12 12:16:54 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-01-12 12:16:54 +0100
commit35112f2357d6510a889c8e20604b11a2c0acf342 (patch)
tree633df7fa174d885925e4445b3e68b164047161ef
parent49a06ccbf087c4ed0db3d61f3ac4892f45858ce1 (diff)
downloadrocket-blog-35112f2357d6510a889c8e20604b11a2c0acf342.tar.gz
rocket-blog-35112f2357d6510a889c8e20604b11a2c0acf342.tar.bz2
rocket-blog-35112f2357d6510a889c8e20604b11a2c0acf342.zip
Implement deleting posts.
-rw-r--r--src/controllers/posts_controller.rs6
-rw-r--r--src/main.rs3
-rw-r--r--src/models/post.rs7
-rw-r--r--templates/post_teaser.html4
4 files changed, 18 insertions, 2 deletions
diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs
index fde08a8..a8c5e1c 100644
--- a/src/controllers/posts_controller.rs
+++ b/src/controllers/posts_controller.rs
@@ -55,3 +55,9 @@ fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Redirect {
::models::Post::update(post.get(), conn);
Redirect::to("/")
}
+
+#[get("/<id>/delete", format = "text/html")]
+fn delete(id: i32, conn: utils::DbConn) -> Redirect {
+ ::models::Post::delete(id, conn);
+ Redirect::to("/")
+}
diff --git a/src/main.rs b/src/main.rs
index 4c05523..bdb8524 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -39,6 +39,7 @@ fn main() {
posts_controller::create,
posts_controller::show,
posts_controller::edit,
- posts_controller::update])
+ posts_controller::update,
+ posts_controller::delete])
.launch();
}
diff --git a/src/models/post.rs b/src/models/post.rs
index 922dbca..3dd0879 100644
--- a/src/models/post.rs
+++ b/src/models/post.rs
@@ -53,4 +53,11 @@ impl Post {
.execute(&*conn)
.expect("Error saving post.");
}
+
+ pub fn delete(post_id: i32, conn: utils::DbConn) {
+ use ::schema::posts::dsl::*;
+ diesel::delete(posts.filter(id.eq(post_id)))
+ .execute(&*conn)
+ .expect(&format!("Could not delete post with id {}", post_id));
+ }
}
diff --git a/templates/post_teaser.html b/templates/post_teaser.html
index 751f4ea..482cf73 100644
--- a/templates/post_teaser.html
+++ b/templates/post_teaser.html
@@ -2,7 +2,9 @@
<header>
<h1><a href="/posts/{{ .id }}">{{ .title }}</a></h1>
<div class="post-actions">
- <a href="/posts/{{ .id }}/edit">Edit</a> | Publish | Delete
+ <a href="/posts/{{ .id }}/edit">Edit</a> |
+ Publish |
+ <a href="/posts/{{ .id }}/delete">Delete</a>
</div>
</header>
<section class="teaser">