|
@@ -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> {
|