|
@@ -19,7 +19,7 @@ pub async fn index(req: Request<State>) -> Result {
|
19
|
19
|
let posts: Vec<Post> = fs::get_all_posts().await?;
|
20
|
20
|
let mut context = Context::new();
|
21
|
21
|
context.insert("posts", &posts);
|
22
|
|
- render_response("index.html", &context, &req)
|
|
22
|
+ render_response("index.html", &context, req)
|
23
|
23
|
}
|
24
|
24
|
|
25
|
25
|
pub async fn single_post(req: Request<State>) -> Result {
|
|
@@ -27,7 +27,7 @@ pub async fn single_post(req: Request<State>) -> Result {
|
27
|
27
|
let post_id = req.param("id")?;
|
28
|
28
|
let post = fs::get_one_post(post_id).await?;
|
29
|
29
|
context.insert("post", &post);
|
30
|
|
- render_response("single.html", &context, &req)
|
|
30
|
+ render_response("single.html", &context, req)
|
31
|
31
|
}
|
32
|
32
|
|
33
|
33
|
pub async fn edit_post(req: Request<State>) -> Result {
|
|
@@ -36,7 +36,7 @@ pub async fn edit_post(req: Request<State>) -> Result {
|
36
|
36
|
let mut post = fs::get_one_post(post_id).await?;
|
37
|
37
|
post.body = post.body.replace("<br>", "\n");
|
38
|
38
|
context.insert("post", &post);
|
39
|
|
- render_response("edit.html", &context, &req)
|
|
39
|
+ render_response("edit.html", &context, req)
|
40
|
40
|
}
|
41
|
41
|
|
42
|
42
|
pub async fn create_post(mut req: Request<State>) -> Result {
|
|
@@ -51,28 +51,22 @@ pub async fn create_post(mut req: Request<State>) -> Result {
|
51
|
51
|
pub async fn update_post(mut req: Request<State>) -> Result {
|
52
|
52
|
let mut post: Post = req.body_form().await?;
|
53
|
53
|
post.save().await?;
|
54
|
|
- Ok(Redirect::new(format!("/posts/{}", post.id)).into())
|
|
54
|
+ req.session_mut().insert("flash_success", String::from("Post updated successfully"))?;
|
|
55
|
+ redirect(&format!("/posts/{}", post.id))
|
55
|
56
|
}
|
56
|
57
|
|
57
|
|
-pub async fn delete_post(req: Request<State>) -> Result {
|
|
58
|
+pub async fn delete_post(mut req: Request<State>) -> Result {
|
58
|
59
|
let id: String = req.param("id")?;
|
59
|
60
|
fs::delete_post(id)?;
|
|
61
|
+ req.session_mut().insert("flash_success", String::from("Post deleted successfully"))?;
|
60
|
62
|
let mut res = Response::new(StatusCode::Ok);
|
61
|
63
|
res.set_body("{\"success\": \"true\"}");
|
62
|
64
|
res.set_content_type(mime::JSON);
|
63
|
65
|
Ok(res)
|
64
|
66
|
}
|
65
|
67
|
|
66
|
|
-pub async fn login_page(mut req: Request<State>) -> Result {
|
67
|
|
- let mut context = Context::new();
|
68
|
|
- match req.session_mut().get::<String>("flash_error") {
|
69
|
|
- Some(error) => {
|
70
|
|
- req.session_mut().remove("flash_error");
|
71
|
|
- &context.insert("error", &error);
|
72
|
|
- }
|
73
|
|
- None => {}
|
74
|
|
- }
|
75
|
|
- render_response("login.html", &context, &req)
|
|
68
|
+pub async fn login_page(req: Request<State>) -> Result {
|
|
69
|
+ render_response("login.html", &Context::new(), req)
|
76
|
70
|
}
|
77
|
71
|
|
78
|
72
|
pub async fn login(mut req: Request<State>) -> Result {
|
|
@@ -82,12 +76,13 @@ pub async fn login(mut req: Request<State>) -> Result {
|
82
|
76
|
if user.username == username && user.password == password {
|
83
|
77
|
req.session_mut().remove("logged_in");
|
84
|
78
|
req.session_mut().insert("logged_in", true)?;
|
85
|
|
- Ok(Redirect::new("/").into())
|
|
79
|
+ redirect("/")
|
86
|
80
|
} else {
|
87
|
81
|
req.session_mut().remove("logged_in");
|
88
|
82
|
req.session_mut()
|
89
|
83
|
.insert("flash_error", "Invalid credentials")?;
|
90
|
|
- Ok(Redirect::new(&req.state().login_path).into())
|
|
84
|
+ let login_path = req.state().login_path.clone();
|
|
85
|
+ redirect(&login_path)
|
91
|
86
|
}
|
92
|
87
|
}
|
93
|
88
|
|
|
@@ -100,17 +95,21 @@ pub async fn logout(mut req: Request<State>) -> Result {
|
100
|
95
|
pub fn render_response(
|
101
|
96
|
template: &str,
|
102
|
97
|
context: &Context,
|
103
|
|
- req: &Request<State>,
|
|
98
|
+ req: Request<State>,
|
104
|
99
|
) -> Result<Response> {
|
105
|
100
|
let mut context = context.clone();
|
106
|
101
|
let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
|
107
|
|
- context.insert("logged_in", &logged_in);
|
108
|
102
|
let login_path = &req.state().login_path;
|
|
103
|
+ context.insert("logged_in", &logged_in);
|
109
|
104
|
context.insert("login_path", login_path);
|
|
105
|
+ context.extend(prepare_flash_messages(req));
|
|
106
|
+
|
110
|
107
|
let html = render_template(template, &context)?;
|
|
108
|
+
|
111
|
109
|
let mut res = Response::new(StatusCode::Ok);
|
112
|
110
|
res.set_body(html);
|
113
|
111
|
res.set_content_type(mime::HTML);
|
|
112
|
+
|
114
|
113
|
Ok(res)
|
115
|
114
|
}
|
116
|
115
|
|
|
@@ -119,3 +118,25 @@ pub fn render_template(template: &str, context: &Context) -> Result<String> {
|
119
|
118
|
let html = tera.render(template, &context)?;
|
120
|
119
|
Ok(html)
|
121
|
120
|
}
|
|
121
|
+
|
|
122
|
+fn redirect(path: &str) -> Result<Response> {
|
|
123
|
+ Ok(Redirect::new(path).into())
|
|
124
|
+}
|
|
125
|
+
|
|
126
|
+fn prepare_flash_messages(mut req: Request<State>) -> Context {
|
|
127
|
+ let mut context = Context::new();
|
|
128
|
+ context.insert("flash_error", &false);
|
|
129
|
+ context.insert("flash_success", &false);
|
|
130
|
+
|
|
131
|
+ for key in vec!["flash_error", "flash_success"] {
|
|
132
|
+ match req.session_mut().get::<String>(key) {
|
|
133
|
+ Some(value) => {
|
|
134
|
+ req.session_mut().remove(key);
|
|
135
|
+ &context.insert(key, &value);
|
|
136
|
+ }
|
|
137
|
+ None => {}
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ context
|
|
142
|
+}
|