...

/

Read and Search Endpoint

Read and Search Endpoint

Learn to create an endpoint, show the product to the user, and search for it.

We'll cover the following...

Read endpoint

The code to access one product is similar to the one we used in the previous lesson.

Press + to interact
// Our product_show endpoint needs to pattern match show_product and
// give us a product as a json object, otherwise just return an internal server error
async fn product_show(id: web::Path<i32>, conn: web::Data<SqliteConnection>)
-> Result<impl Responder> {
match product::show_product(*id, &conn) {
Ok(product) => Ok(web::Json(product)),
Err(error) => Err(actix_web::error::ErrorNotFound(error))
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.data(establish_connection())
.route("products/{id}", web::get().to(product_show))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}

We ask for the ...