Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actix example bug fix #2140

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions examples/actix_example/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,34 @@ async fn create(
.finish())
}

#[get("/{id}")]
#[get(r#"/{id:\d+}"#)]
async fn edit(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
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<post::Model> = 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}")]
Expand Down
Loading