actix example bug fix (#2140)
* bug fix * fmt --------- Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
This commit is contained in:
parent
d64b627ba2
commit
763a3f9aa1
@ -89,24 +89,34 @@ async fn create(
|
|||||||
.finish())
|
.finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/{id}")]
|
#[get(r#"/{id:\d+}"#)]
|
||||||
async fn edit(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
async fn edit(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
||||||
let conn = &data.conn;
|
let conn = &data.conn;
|
||||||
let template = &data.templates;
|
let template = &data.templates;
|
||||||
let id = id.into_inner();
|
let id = id.into_inner();
|
||||||
|
|
||||||
let post: post::Model = Query::find_post_by_id(conn, id)
|
let post: Option<post::Model> = Query::find_post_by_id(conn, id)
|
||||||
.await
|
.await
|
||||||
.expect("could not find post")
|
.expect("could not find post");
|
||||||
.unwrap_or_else(|| panic!("could not find post with id {id}"));
|
|
||||||
|
|
||||||
let mut ctx = tera::Context::new();
|
let mut ctx = tera::Context::new();
|
||||||
|
let body = match post {
|
||||||
|
Some(post) => {
|
||||||
ctx.insert("post", &post);
|
ctx.insert("post", &post);
|
||||||
|
|
||||||
let body = template
|
template
|
||||||
.render("edit.html.tera", &ctx)
|
.render("edit.html.tera", &ctx)
|
||||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
|
.map_err(|_| error::ErrorInternalServerError("Template error"))
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(body))
|
}
|
||||||
|
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}")]
|
#[post("/{id}")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user