Search⌘ K

Read and Search Endpoint

Explore how to implement read and search endpoints in Rust backend development. Understand how to handle query parameters to fetch and search products, and test your endpoint functionality effectively.

We'll cover the following...

Read endpoint

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

Rust 1.40.0
// 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 id query ...