now can edit the posts directly from admin panel
parent
0da7e92c09
commit
e983cc1b9d
58
src/admin.rs
58
src/admin.rs
|
@ -90,7 +90,7 @@ fn add_post(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
|
|||
}
|
||||
|
||||
#[post("/add_post", data = "<post>")]
|
||||
fn write_post(mut cookies: Cookies<'_>, post: Form<NewPost>) -> Redirect {
|
||||
fn write_add_post(mut cookies: Cookies<'_>, post: Form<NewPost>) -> 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<NewPost>) -> Redirect {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_routes() -> Vec<Route> {
|
||||
routes![index, login, panel, add_post, write_post]
|
||||
#[get("/edit_post/<id>")]
|
||||
fn edit_post(mut cookies: Cookies<'_>, id: i32) -> Result<Template, Redirect> {
|
||||
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/<id>", data = "<post>")]
|
||||
fn write_edit_post(mut cookies: Cookies<'_>, id: i32, post: Form<NewPost>) -> 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<Route> {
|
||||
routes![
|
||||
index,
|
||||
login,
|
||||
panel,
|
||||
add_post,
|
||||
write_add_post,
|
||||
edit_post,
|
||||
write_edit_post
|
||||
]
|
||||
}
|
||||
|
|
|
@ -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::<Post>(&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<Post, Error> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
{% extends "base" %}
|
||||
|
||||
{% block extracss %}
|
||||
<style>
|
||||
div.container {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
}
|
||||
|
||||
div.editable form {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
div.editable input#title {
|
||||
width: 76%;
|
||||
margin: 10px 10px 10px 0;
|
||||
}
|
||||
|
||||
div.editable textarea {
|
||||
width: 95%;
|
||||
height: 85%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
</style>
|
||||
{% endblock extracss %}
|
||||
|
||||
{% block content %}
|
||||
<div class="editable">
|
||||
<form action="/admin/edit_post/{{ id }}" method="post">
|
||||
<input id="title" name="title" type="text" value={{ title }}/>
|
||||
<label for="">Visible</label>
|
||||
<input name="published" type="checkbox" value="true"{% if published %}checked{% endif %}/>
|
||||
<textarea id="content" name="content">{{ content }}</textarea>
|
||||
<button>Publicar</button>
|
||||
</form>
|
||||
</div>
|
||||
<div id="preview" class="preview">
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
{% block extrajs %}
|
||||
<script src="https://cdn.jsdelivr.net/npm/showdown@1.9.0/dist/showdown.min.js"></script>
|
||||
<script>
|
||||
function delay(fn, ms) {
|
||||
let timer = 0
|
||||
return function(...args) {
|
||||
clearTimeout(timer)
|
||||
timer = setTimeout(fn.bind(this, ...args), ms || 0)
|
||||
}
|
||||
}
|
||||
|
||||
let converter = new showdown.Converter({
|
||||
parseImgDimensions: true,
|
||||
strikethrough: true,
|
||||
tables: true,
|
||||
tasklists: true,
|
||||
emoji: true
|
||||
});
|
||||
|
||||
|
||||
text = "# " + document.getElementById("title").value.trim() + "\n" + document.getElementById("content").value;
|
||||
document.getElementById("preview").innerHTML = converter.makeHtml(text);
|
||||
|
||||
document.getElementById("content").addEventListener("keyup", delay(() => {
|
||||
let text = "# " + document.getElementById("title").value.trim() + "\n" + document.getElementById("content").value;
|
||||
document.getElementById("preview").innerHTML = converter.makeHtml(text);
|
||||
}, 500));
|
||||
|
||||
document.getElementById("title").addEventListener("keyup", delay(() => {
|
||||
let text = "# " + document.getElementById("title").value.trim() + "\n" + document.getElementById("content").value;
|
||||
document.getElementById("preview").innerHTML = converter.makeHtml(text);
|
||||
}, 500));
|
||||
</script>
|
||||
{% endblock extrajs %}
|
|
@ -45,15 +45,14 @@
|
|||
<p class="colored"><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
|
||||
<p class="colored center">0</p>
|
||||
<p class="colored center">{{ post.published }}</p>
|
||||
<p class="colored"><a href="#">Editar</a></p>
|
||||
<p class="colored"><a href="/admin/edit_post/{{ post.id }}">Editar</a></p>
|
||||
{% else %}
|
||||
<p>{{ post.id }}</p>
|
||||
<p><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
|
||||
<p class="center">0</p>
|
||||
<p class="center">{{ post.published }}</p>
|
||||
<p><a href="#">Editar</a></p>
|
||||
<p><a href="/admin/edit_post/{{ post.id }}">Editar</a></p>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</h3>
|
||||
{% endblock content %}
|
||||
|
|
Loading…
Reference in New Issue