From 6b17cc50bc57d57354564192757c96f6b25d49d3 Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Fri, 3 Sep 2021 22:00:28 +1000 Subject: [PATCH] Can edit a post --- examples/rocket_example/src/main.rs | 40 +++++++++++++++--- examples/rocket_example/static/css/style.css | 17 ++++++++ .../rocket_example/templates/edit.html.tera | 42 +++++++++++++++++++ .../rocket_example/templates/index.html.tera | 9 +--- 4 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 examples/rocket_example/templates/edit.html.tera diff --git a/examples/rocket_example/src/main.rs b/examples/rocket_example/src/main.rs index 7048e4d4..9a55deac 100644 --- a/examples/rocket_example/src/main.rs +++ b/examples/rocket_example/src/main.rs @@ -46,6 +46,29 @@ async fn create(conn: Connection, post_form: Form) -> Flash", data = "")] +async fn update(conn: Connection, id: i64, post_form: Form) -> Flash { + 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, flash: Option>) -> Template { let posts = Post::find() @@ -66,16 +89,18 @@ async fn list(conn: Connection, flash: Option>) -> Template } #[get("/")] -async fn read(conn: Connection, id: i64) -> Option> { +async fn edit(conn: Connection, id: i64) -> Template { let post: Option = 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("/")] @@ -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()) } diff --git a/examples/rocket_example/static/css/style.css b/examples/rocket_example/static/css/style.css index 1b6a1720..62c2f904 100644 --- a/examples/rocket_example/static/css/style.css +++ b/examples/rocket_example/static/css/style.css @@ -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; +} diff --git a/examples/rocket_example/templates/edit.html.tera b/examples/rocket_example/templates/edit.html.tera new file mode 100644 index 00000000..a59b3093 --- /dev/null +++ b/examples/rocket_example/templates/edit.html.tera @@ -0,0 +1,42 @@ +{% extends "base" %} {% block content %} +
+

Edit Post

+
+
+
+
+ + +
+
+ +
+
+
+
+
+
+ + +
+
+
+
+
+{% endblock content %} diff --git a/examples/rocket_example/templates/index.html.tera b/examples/rocket_example/templates/index.html.tera index 077f7ff1..6ccbff4b 100644 --- a/examples/rocket_example/templates/index.html.tera +++ b/examples/rocket_example/templates/index.html.tera @@ -12,20 +12,13 @@ ID Title Text - {% for post in posts %} - + {{ post.id }} {{ post.title }} {{ post.text }} - - - {% endfor %}