Browse Source

Implement post update route

master
Dylan Baker 3 years ago
parent
commit
aec0d0bcdb
5 changed files with 27 additions and 19 deletions
  1. 3
    1
      src/main.rs
  2. 20
    14
      src/routes.rs
  3. 2
    2
      templates/components.html
  4. 1
    1
      templates/edit.html
  5. 1
    1
      templates/index.html

+ 3
- 1
src/main.rs View File

@@ -20,7 +20,9 @@ async fn main() -> std::io::Result<()> {
20 20
     app.at("/posts")
21 21
         .with(middleware::require_auth)
22 22
         .post(routes::create_post);
23
-    app.at("/posts/:id").get(routes::single_post);
23
+    app.at("/posts/:id")
24
+        .get(routes::single_post)
25
+        .post(routes::update_post);
24 26
     app.at("/posts/:id/edit")
25 27
         .with(middleware::require_auth)
26 28
         .get(routes::edit_post);

+ 20
- 14
src/routes.rs View File

@@ -2,7 +2,7 @@ use chrono::prelude::Local;
2 2
 use serde::{Deserialize, Serialize};
3 3
 use tera::{Context, Tera};
4 4
 use tide::http::mime;
5
-use tide::{Request, Response, Result, StatusCode};
5
+use tide::{Redirect, Request, Response, Result, StatusCode};
6 6
 use uuid::Uuid;
7 7
 
8 8
 use std::env;
@@ -24,15 +24,6 @@ pub async fn index(req: Request<()>) -> Result {
24 24
     render_response("index.html", &context)
25 25
 }
26 26
 
27
-pub async fn create_post(mut req: Request<()>) -> Result {
28
-    let mut post: Post = req.body_form().await?;
29
-    post.id = Uuid::new_v4().to_string();
30
-    post.date = Local::now().date().naive_local().to_string();
31
-    post.body = post.body.trim().to_owned();
32
-    post.save().await?;
33
-    Ok(tide::Redirect::new("/").into())
34
-}
35
-
36 27
 pub async fn single_post(req: Request<()>) -> Result {
37 28
     let mut context = Context::new();
38 29
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
@@ -54,6 +45,21 @@ pub async fn edit_post(req: Request<()>) -> Result {
54 45
     render_response("edit.html", &context)
55 46
 }
56 47
 
48
+pub async fn create_post(mut req: Request<()>) -> Result {
49
+    let mut post: Post = req.body_form().await?;
50
+    post.id = Uuid::new_v4().to_string();
51
+    post.date = Local::now().date().naive_local().to_string();
52
+    post.body = post.body.trim().to_owned();
53
+    post.save().await?;
54
+    Ok(Redirect::new("/").into())
55
+}
56
+
57
+pub async fn update_post(mut req: Request<()>) -> Result {
58
+    let mut post: Post = req.body_form().await?;
59
+    post.save().await?;
60
+    Ok(Redirect::new(format!("/posts/{}", post.id)).into())
61
+}
62
+
57 63
 pub async fn login_page(mut req: Request<()>) -> Result {
58 64
     let mut context = Context::new();
59 65
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
@@ -71,7 +77,7 @@ pub async fn login_page(mut req: Request<()>) -> Result {
71 77
 pub async fn login(mut req: Request<()>) -> Result {
72 78
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
73 79
     if logged_in {
74
-        return Ok(tide::Redirect::new("/").into());
80
+        return Ok(Redirect::new("/").into());
75 81
     }
76 82
 
77 83
     let username = env::var("ADMIN_USERNAME")?;
@@ -80,19 +86,19 @@ pub async fn login(mut req: Request<()>) -> Result {
80 86
     if user.username == username && user.password == password {
81 87
         req.session_mut().remove("logged_in");
82 88
         req.session_mut().insert("logged_in", true)?;
83
-        Ok(tide::Redirect::new("/").into())
89
+        Ok(Redirect::new("/").into())
84 90
     } else {
85 91
         req.session_mut().remove("logged_in");
86 92
         req.session_mut()
87 93
             .insert("flash_error", "Invalid credentials")?;
88
-        Ok(tide::Redirect::new("/login").into())
94
+        Ok(Redirect::new("/login").into())
89 95
     }
90 96
 }
91 97
 
92 98
 pub async fn logout(mut req: Request<()>) -> Result {
93 99
     req.session_mut().remove("logged_in");
94 100
     req.session_mut().insert("logged_in", false)?;
95
-    Ok(tide::Redirect::new("/").into())
101
+    Ok(Redirect::new("/").into())
96 102
 }
97 103
 
98 104
 pub fn render_response(template: &str, context: &Context) -> Result<Response> {

+ 2
- 2
templates/components.html View File

@@ -16,8 +16,8 @@
16 16
     {{ post.body | safe }}
17 17
   </div>
18 18
 </div>
19
-{% endmacro %} {% macro form(post=false, action, method) %}
20
-<form class="form" method="{{ method }}" action="{{ action }}">
19
+{% endmacro %} {% macro form(post=false, action) %}
20
+<form class="form" method="POST" action="{{ action }}">
21 21
   <input
22 22
     type="hidden"
23 23
     name="id"

+ 1
- 1
templates/edit.html View File

@@ -1,5 +1,5 @@
1 1
 {% extends "layout.html" %} {% block content %}
2 2
 <div class="posts">
3
-  {{ components::form(post=post, action="/posts/{{ post.id }}", method="PUT") }}
3
+  {{ components::form(post=post, action="/posts/{{ post.id }}") }}
4 4
 </div>
5 5
 {% endblock %}

+ 1
- 1
templates/index.html View File

@@ -1,6 +1,6 @@
1 1
 {% extends "layout.html" %} {% block content %} {% if logged_in %}
2 2
 <h1 class="heading">New Post</h1>
3
-{{ components::form(action="/posts", method="POST") }} {% endif %}
3
+{{ components::form(action="/posts") }} {% endif %}
4 4
 
5 5
 <div class="posts">
6 6
   {% for post in posts %} {{ components::post(post=post, type="index") }} {%

Loading…
Cancel
Save