From 70cc60c809bf4c190206bfe5b8670e6518b2e347 Mon Sep 17 00:00:00 2001 From: ikka0426 Date: Mon, 4 Mar 2024 01:27:44 +0800 Subject: [PATCH 1/2] bug fix --- examples/actix_example/api/src/lib.rs | 30 ++++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/actix_example/api/src/lib.rs b/examples/actix_example/api/src/lib.rs index f57e3e769f..48207e2aa2 100644 --- a/examples/actix_example/api/src/lib.rs +++ b/examples/actix_example/api/src/lib.rs @@ -89,24 +89,34 @@ async fn create( .finish()) } -#[get("/{id}")] +#[get(r#"/{id:\d+}"#)] async fn edit(data: web::Data, id: web::Path) -> Result { let conn = &data.conn; let template = &data.templates; let id = id.into_inner(); - let post: post::Model = Query::find_post_by_id(conn, id) + let post: Option = Query::find_post_by_id(conn, id) .await - .expect("could not find post") - .unwrap_or_else(|| panic!("could not find post with id {id}")); + .expect("could not find post"); let mut ctx = tera::Context::new(); - ctx.insert("post", &post); - - let body = template - .render("edit.html.tera", &ctx) - .map_err(|_| error::ErrorInternalServerError("Template error"))?; - Ok(HttpResponse::Ok().content_type("text/html").body(body)) + let body = match post { + Some(post) => { + ctx.insert("post", &post); + + template + .render("edit.html.tera", &ctx) + .map_err(|_| error::ErrorInternalServerError("Template error")) + } + None => { + ctx.insert("uri", &format!("/{}", id)); + + template + .render("error/404.html.tera", &ctx) + .map_err(|_| error::ErrorInternalServerError("Template error")) + } + }; + Ok(HttpResponse::Ok().content_type("text/html").body(body?)) } #[post("/{id}")] From e65d34ba819bddc2755fee72ba99c775b677c349 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 10 Mar 2024 23:36:04 +0000 Subject: [PATCH 2/2] fmt --- examples/actix_example/api/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/actix_example/api/src/lib.rs b/examples/actix_example/api/src/lib.rs index 48207e2aa2..cd7450c485 100644 --- a/examples/actix_example/api/src/lib.rs +++ b/examples/actix_example/api/src/lib.rs @@ -103,14 +103,14 @@ async fn edit(data: web::Data, id: web::Path) -> Result { ctx.insert("post", &post); - + template .render("edit.html.tera", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error")) } None => { ctx.insert("uri", &format!("/{}", id)); - + template .render("error/404.html.tera", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error"))