about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-15T21·23+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-15T21·23+0200
commite130e15b79c0a77c7759ec24e44310eebc013417 (patch)
tree4d60d5d073c72912465f7d02014b7fbee2939b0e
parent705097dab91c57524d2311dd839615840044437c (diff)
feat(db): Support UpdatePost message
Simple message intended to be used for post editing.
-rw-r--r--src/db.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs
index 4ac658a5ce64..3641bddb5aa4 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -87,6 +87,29 @@ impl Handler<GetPost> for DbExecutor {
     }
 }
 
+/// Message used to update the content of a post.
+#[derive(Deserialize)]
+pub struct UpdatePost {
+    post_id: i32,
+    post: String,
+}
+
+message!(UpdatePost, Result<Post>);
+
+impl Handler<UpdatePost> for DbExecutor {
+    type Result = Result<Post>;
+
+    fn handle(&mut self, msg: UpdatePost, _: &mut Self::Context) -> Self::Result {
+        use schema::posts::dsl::*;
+        let conn = self.0.get()?;
+        let updated = diesel::update(posts.find(msg.post_id))
+            .set(body.eq(msg.post))
+            .get_result(&conn)?;
+
+        Ok(updated)
+    }
+}
+
 /// Message used to create a new thread
 pub struct CreateThread {
     pub new_thread: NewThread,