瀏覽代碼

Implement post update route

master
Dylan Baker 3 年之前
父節點
當前提交
aec0d0bcdb
共有 5 個文件被更改,包括 27 次插入19 次删除
  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 查看文件

20
     app.at("/posts")
20
     app.at("/posts")
21
         .with(middleware::require_auth)
21
         .with(middleware::require_auth)
22
         .post(routes::create_post);
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
     app.at("/posts/:id/edit")
26
     app.at("/posts/:id/edit")
25
         .with(middleware::require_auth)
27
         .with(middleware::require_auth)
26
         .get(routes::edit_post);
28
         .get(routes::edit_post);

+ 20
- 14
src/routes.rs 查看文件

2
 use serde::{Deserialize, Serialize};
2
 use serde::{Deserialize, Serialize};
3
 use tera::{Context, Tera};
3
 use tera::{Context, Tera};
4
 use tide::http::mime;
4
 use tide::http::mime;
5
-use tide::{Request, Response, Result, StatusCode};
5
+use tide::{Redirect, Request, Response, Result, StatusCode};
6
 use uuid::Uuid;
6
 use uuid::Uuid;
7
 
7
 
8
 use std::env;
8
 use std::env;
24
     render_response("index.html", &context)
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
 pub async fn single_post(req: Request<()>) -> Result {
27
 pub async fn single_post(req: Request<()>) -> Result {
37
     let mut context = Context::new();
28
     let mut context = Context::new();
38
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
29
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
54
     render_response("edit.html", &context)
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
 pub async fn login_page(mut req: Request<()>) -> Result {
63
 pub async fn login_page(mut req: Request<()>) -> Result {
58
     let mut context = Context::new();
64
     let mut context = Context::new();
59
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
65
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
71
 pub async fn login(mut req: Request<()>) -> Result {
77
 pub async fn login(mut req: Request<()>) -> Result {
72
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
78
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
73
     if logged_in {
79
     if logged_in {
74
-        return Ok(tide::Redirect::new("/").into());
80
+        return Ok(Redirect::new("/").into());
75
     }
81
     }
76
 
82
 
77
     let username = env::var("ADMIN_USERNAME")?;
83
     let username = env::var("ADMIN_USERNAME")?;
80
     if user.username == username && user.password == password {
86
     if user.username == username && user.password == password {
81
         req.session_mut().remove("logged_in");
87
         req.session_mut().remove("logged_in");
82
         req.session_mut().insert("logged_in", true)?;
88
         req.session_mut().insert("logged_in", true)?;
83
-        Ok(tide::Redirect::new("/").into())
89
+        Ok(Redirect::new("/").into())
84
     } else {
90
     } else {
85
         req.session_mut().remove("logged_in");
91
         req.session_mut().remove("logged_in");
86
         req.session_mut()
92
         req.session_mut()
87
             .insert("flash_error", "Invalid credentials")?;
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
 pub async fn logout(mut req: Request<()>) -> Result {
98
 pub async fn logout(mut req: Request<()>) -> Result {
93
     req.session_mut().remove("logged_in");
99
     req.session_mut().remove("logged_in");
94
     req.session_mut().insert("logged_in", false)?;
100
     req.session_mut().insert("logged_in", false)?;
95
-    Ok(tide::Redirect::new("/").into())
101
+    Ok(Redirect::new("/").into())
96
 }
102
 }
97
 
103
 
98
 pub fn render_response(template: &str, context: &Context) -> Result<Response> {
104
 pub fn render_response(template: &str, context: &Context) -> Result<Response> {

+ 2
- 2
templates/components.html 查看文件

16
     {{ post.body | safe }}
16
     {{ post.body | safe }}
17
   </div>
17
   </div>
18
 </div>
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
   <input
21
   <input
22
     type="hidden"
22
     type="hidden"
23
     name="id"
23
     name="id"

+ 1
- 1
templates/edit.html 查看文件

1
 {% extends "layout.html" %} {% block content %}
1
 {% extends "layout.html" %} {% block content %}
2
 <div class="posts">
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
 </div>
4
 </div>
5
 {% endblock %}
5
 {% endblock %}

+ 1
- 1
templates/index.html 查看文件

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

Loading…
取消
儲存