Can edit a post

This commit is contained in:
Sam Samai 2021-09-03 22:00:28 +10:00
parent 3f6b2e3516
commit 6b17cc50bc
4 changed files with 94 additions and 14 deletions

View File

@ -46,6 +46,29 @@ async fn create(conn: Connection<Db>, post_form: Form<post::Model>) -> Flash<Red
Flash::success(Redirect::to("/"), "Post successfully added.")
}
#[post("/<id>", data = "<post_form>")]
async fn update(conn: Connection<Db>, id: i64, post_form: Form<post::Model>) -> Flash<Redirect> {
let post: post::ActiveModel = Post::find_by_id(id)
.one(&conn)
.await
.unwrap()
.unwrap()
.into();
let post_data = post_form.into_inner();
let _edited_post = post::ActiveModel {
id: post.id,
title: Set(post_data.title.to_owned()),
text: Set(post_data.text.to_owned()),
}
.save(&conn)
.await
.expect("could not edit post");
Flash::success(Redirect::to("/"), "Post successfully edited.")
}
#[get("/")]
async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template {
let posts = Post::find()
@ -66,16 +89,18 @@ async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template
}
#[get("/<id>")]
async fn read(conn: Connection<Db>, id: i64) -> Option<Json<post::Model>> {
async fn edit(conn: Connection<Db>, id: i64) -> Template {
let post: Option<post::Model> = Post::find_by_id(id)
.one(&conn)
.await
.expect("could not find post");
match post {
None => None,
Some(post) => Some(Json(post)),
}
Template::render(
"edit",
context! {
post: post,
},
)
}
#[delete("/<id>")]
@ -120,7 +145,10 @@ fn rocket() -> _ {
.attach(Db::init())
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
.mount("/", FileServer::from(relative!("/static")))
.mount("/", routes![new, create, delete, destroy, list, read,])
.mount(
"/",
routes![new, create, delete, destroy, list, edit, update],
)
.register("/", catchers![not_found])
.attach(Template::fairing())
}

View File

@ -57,3 +57,20 @@ button.small {
line-height: 20px;
margin: 0 2.5px;
}
.post {
}
.post:hover {
background-color: #bce2ee;
}
.post td {
padding: 5px;
width: 150px;
}
#delete-button {
color: red;
border-color: red;
}

View File

@ -0,0 +1,42 @@
{% extends "base" %} {% block content %}
<div class="row">
<h4>Edit Post</h4>
<div class="twelve columns">
<div class="ten columns">
<form action="/{{ post.id }}" method="post">
<div class="twelve columns">
<input
type="text"
placeholder="title"
name="title"
id="title"
value="{{ post.title }}"
autofocus
class="u-full-width"
/>
<input
type="text"
placeholder="content"
name="text"
id="text"
value="{{ post.text }}"
autofocus
class="u-full-width"
/>
</div>
<div class="twelve columns">
<input type="submit" value="save post" />
</div>
</form>
</div>
<div class="two columns">
<form action="/{{ post.id }}" method="post">
<div class="two columns">
<input type="hidden" name="_method" value="delete" />
<input id="delete-button" type="submit" value="delete post" />
</div>
</form>
</div>
</div>
</div>
{% endblock content %}

View File

@ -12,20 +12,13 @@
<th>ID</th>
<th>Title</th>
<th>Text</th>
<th></th>
</tr>
</thead>
{% for post in posts %}
<tr>
<tr class="post" onclick="window.location='/{{ post.id }}';">
<td>{{ post.id }}</td>
<td>{{ post.title }}</td>
<td>{{ post.text }}</td>
<td>
<form class="link" action="/{{ post.id }}" method="post">
<input type="hidden" name="_method" value="delete" />
<button class="link" type="submit">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>