diff --git a/src/admin.rs b/src/admin.rs index 0214779..29f0287 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -90,7 +90,7 @@ fn add_post(mut cookies: Cookies<'_>) -> Result { } #[post("/add_post", data = "")] -fn write_post(mut cookies: Cookies<'_>, post: Form) -> Redirect { +fn write_add_post(mut cookies: Cookies<'_>, post: Form) -> Redirect { let password = env::var("admin_pass").expect("admin_pass not setted"); match cookies.get_private("user") { @@ -108,6 +108,58 @@ fn write_post(mut cookies: Cookies<'_>, post: Form) -> Redirect { } } -pub fn get_routes() -> Vec { - routes![index, login, panel, add_post, write_post] +#[get("/edit_post/")] +fn edit_post(mut cookies: Cookies<'_>, id: i32) -> Result { + let password = env::var("admin_pass").expect("admin_pass not setted"); + + match cookies.get_private("user") { + Some(cookie) => { + if cookie.value() == password { + let mut context = get_context(); + + let post = posts::get_post(id).unwrap(); + + context.insert("id", &post.id); + context.insert("title", &post.title); + context.insert("content", &post.content); + context.insert("published", &post.published); + + Ok(Template::render("admin/edit_post", context)) + } else { + Err(Redirect::to("/admin")) + } + } + None => Err(Redirect::to("/admin")), + } +} + +#[post("/edit_post/", data = "")] +fn write_edit_post(mut cookies: Cookies<'_>, id: i32, post: Form) -> Redirect { + let password = env::var("admin_pass").expect("admin_pass not setted"); + + match cookies.get_private("user") { + Some(cookie) => { + if cookie.value() == password { + match posts::edit_post(id, &post) { + Ok(post) => Redirect::to(format!("/post/{}", post.id)), + Err(_) => Redirect::to("admin/add_post"), + } + } else { + Redirect::to("/admin") + } + } + None => Redirect::to("/admin"), + } +} + +pub fn get_routes() -> Vec { + routes![ + index, + login, + panel, + add_post, + write_add_post, + edit_post, + write_edit_post + ] } diff --git a/src/controllers.rs b/src/controllers.rs index f39105e..a86976f 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -23,9 +23,10 @@ pub mod posts { use crate::schema::posts::dsl::*; let connection = establish_connection(); - let visible_posts = posts.filter(published.eq(true)).order(created_at.desc()); + let visible_posts = posts.order(created_at.desc()); let visible_posts = match page { Some(number_page) => visible_posts + .filter(published.eq(true)) .limit(MAX_POSTS_PER_PAGE as i64) .offset((MAX_POSTS_PER_PAGE * (number_page - 1)) as i64) .load::(&connection) @@ -61,4 +62,17 @@ pub mod posts { .values(new_post) .get_result(&connection) } + + pub fn edit_post(updated_id: i32, new_post: &NewPost) -> Result { + use crate::schema::posts::dsl::*; + let connection = establish_connection(); + + diesel::update(posts.filter(id.eq(updated_id))) + .set(( + title.eq(&new_post.title), + content.eq(&new_post.content), + published.eq(&new_post.published), + )) + .get_result(&connection) + } } diff --git a/templates/admin/edit_post.html.tera b/templates/admin/edit_post.html.tera new file mode 100644 index 0000000..280303e --- /dev/null +++ b/templates/admin/edit_post.html.tera @@ -0,0 +1,74 @@ +{% extends "base" %} + +{% block extracss %} + +{% endblock extracss %} + +{% block content %} +
+
+ + + + + +
+
+
+
+{% endblock content %} + +{% block extrajs %} + + +{% endblock extrajs %} diff --git a/templates/admin/panel.html.tera b/templates/admin/panel.html.tera index 95b8cbb..84c35bb 100644 --- a/templates/admin/panel.html.tera +++ b/templates/admin/panel.html.tera @@ -45,15 +45,14 @@

{{ post.title }}

0

{{ post.published }}

-

Editar

+

Editar

{% else %}

{{ post.id }}

{{ post.title }}

0

{{ post.published }}

-

Editar

+

Editar

{% endif %} {% endfor %} - {% endblock content %}