|
@@ -1,6 +1,7 @@
|
1
|
1
|
use chrono::prelude::Local;
|
2
|
2
|
use serde::{Deserialize, Serialize};
|
3
|
3
|
use tera::{Context, Tera};
|
|
4
|
+use tide::convert::json;
|
4
|
5
|
use tide::http::mime;
|
5
|
6
|
use tide::{Redirect, Request, Response, Result, StatusCode};
|
6
|
7
|
use uuid::Uuid;
|
|
@@ -19,7 +20,7 @@ pub async fn index(req: Request<State>) -> Result {
|
19
|
20
|
let posts: Vec<Post> = fs::get_all_posts().await?;
|
20
|
21
|
let mut context = Context::new();
|
21
|
22
|
context.insert("posts", &posts);
|
22
|
|
- render_response("index.html", &context, req)
|
|
23
|
+ render_html_response("index.html", &context, req)
|
23
|
24
|
}
|
24
|
25
|
|
25
|
26
|
pub async fn single_post(req: Request<State>) -> Result {
|
|
@@ -27,7 +28,7 @@ pub async fn single_post(req: Request<State>) -> Result {
|
27
|
28
|
let post_id = req.param("id")?;
|
28
|
29
|
let post = fs::get_one_post(post_id).await?;
|
29
|
30
|
context.insert("post", &post);
|
30
|
|
- render_response("single.html", &context, req)
|
|
31
|
+ render_html_response("single.html", &context, req)
|
31
|
32
|
}
|
32
|
33
|
|
33
|
34
|
pub async fn edit_post(req: Request<State>) -> Result {
|
|
@@ -36,7 +37,7 @@ pub async fn edit_post(req: Request<State>) -> Result {
|
36
|
37
|
let mut post = fs::get_one_post(post_id).await?;
|
37
|
38
|
post.body = post.body.replace("<br>", "\n");
|
38
|
39
|
context.insert("post", &post);
|
39
|
|
- render_response("edit.html", &context, req)
|
|
40
|
+ render_html_response("edit.html", &context, req)
|
40
|
41
|
}
|
41
|
42
|
|
42
|
43
|
pub async fn create_post(mut req: Request<State>) -> Result {
|
|
@@ -59,14 +60,11 @@ pub async fn delete_post(mut req: Request<State>) -> Result {
|
59
|
60
|
let id: String = req.param("id")?;
|
60
|
61
|
fs::delete_post(id)?;
|
61
|
62
|
req.session_mut().insert("flash_success", String::from("Post deleted successfully"))?;
|
62
|
|
- let mut res = Response::new(StatusCode::Ok);
|
63
|
|
- res.set_body("{\"success\": \"true\"}");
|
64
|
|
- res.set_content_type(mime::JSON);
|
65
|
|
- Ok(res)
|
|
63
|
+ render_json_response(json!({ "success": "true" }))
|
66
|
64
|
}
|
67
|
65
|
|
68
|
66
|
pub async fn login_page(req: Request<State>) -> Result {
|
69
|
|
- render_response("login.html", &Context::new(), req)
|
|
67
|
+ render_html_response("login.html", &Context::new(), req)
|
70
|
68
|
}
|
71
|
69
|
|
72
|
70
|
pub async fn login(mut req: Request<State>) -> Result {
|
|
@@ -92,7 +90,7 @@ pub async fn logout(mut req: Request<State>) -> Result {
|
92
|
90
|
Ok(Redirect::new("/").into())
|
93
|
91
|
}
|
94
|
92
|
|
95
|
|
-pub fn render_response(
|
|
93
|
+pub fn render_html_response(
|
96
|
94
|
template: &str,
|
97
|
95
|
context: &Context,
|
98
|
96
|
req: Request<State>,
|
|
@@ -114,15 +112,25 @@ pub fn render_response(
|
114
|
112
|
Ok(res)
|
115
|
113
|
}
|
116
|
114
|
|
|
115
|
+pub fn render_json_response(json: serde_json::Value) -> Result<Response> {
|
|
116
|
+ let res = Response::builder(StatusCode::Ok)
|
|
117
|
+ .body(json)
|
|
118
|
+ .content_type(mime::JSON)
|
|
119
|
+ .build();
|
|
120
|
+
|
|
121
|
+ Ok(res)
|
|
122
|
+}
|
|
123
|
+
|
|
124
|
+fn redirect(path: &str) -> Result<Response> {
|
|
125
|
+ Ok(Redirect::new(path).into())
|
|
126
|
+}
|
|
127
|
+
|
117
|
128
|
pub fn render_template(template: &str, context: &Context) -> Result<String> {
|
118
|
129
|
let tera = Tera::new("templates/**/*.html")?;
|
119
|
130
|
let html = tera.render(template, &context)?;
|
120
|
131
|
Ok(html)
|
121
|
132
|
}
|
122
|
133
|
|
123
|
|
-fn redirect(path: &str) -> Result<Response> {
|
124
|
|
- Ok(Redirect::new(path).into())
|
125
|
|
-}
|
126
|
134
|
|
127
|
135
|
fn prepare_flash_messages(mut req: Request<State>) -> Context {
|
128
|
136
|
let mut context = Context::new();
|