From a92260270084eca48bee948c962dab943fd2a30a Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Thu, 26 Aug 2021 22:07:38 +1000 Subject: [PATCH] Show post #[get("/")] --- examples/rocket_example/src/sqlx/mod.rs | 28 ++++++++----------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/examples/rocket_example/src/sqlx/mod.rs b/examples/rocket_example/src/sqlx/mod.rs index 39f11241..fe089ba4 100644 --- a/examples/rocket_example/src/sqlx/mod.rs +++ b/examples/rocket_example/src/sqlx/mod.rs @@ -61,26 +61,16 @@ async fn list(mut con: Connection) -> Result>> { } #[get("/")] -async fn read(mut db: Connection, id: i64) -> Option> { - // let post: Option = Post::find_by_id(id) - // .one(db) - // .await - // .expect("could not find baker"); - // println!("post: {:#?}", post); +async fn read(mut con: Connection, id: i64) -> Option> { + let post: Option = Post::find_by_id(id) + .one(&con) + .await + .expect("could not find post"); - // sqlx::query!("SELECT id, title, text FROM posts WHERE id = ?", id) - // .fetch_one(&mut *db) - // .map_ok(|r| { - // Json(Post { - // id: Some(r.id), - // title: r.title, - // text: r.text, - // }) - // }) - // .await - // .ok() - - None + match post { + None => None, + Some(post) => Some(Json(post)), + } } // #[delete("/")]