diff options
Diffstat (limited to 'crates/rocie-server/src/api')
| -rw-r--r-- | crates/rocie-server/src/api/get/mod.rs | 2 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/get/product.rs | 55 |
2 files changed, 56 insertions, 1 deletions
diff --git a/crates/rocie-server/src/api/get/mod.rs b/crates/rocie-server/src/api/get/mod.rs index e21a120..1c32105 100644 --- a/crates/rocie-server/src/api/get/mod.rs +++ b/crates/rocie-server/src/api/get/mod.rs @@ -7,6 +7,8 @@ pub(crate) mod unit_property; pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) { cfg.service(product::product_by_id) + .service(product::product_by_name) + .service(product::product_suggestion_by_name) .service(product::products) .service(unit::units) .service(unit::unit_by_id) diff --git a/crates/rocie-server/src/api/get/product.rs b/crates/rocie-server/src/api/get/product.rs index 90cb8c8..9356a68 100644 --- a/crates/rocie-server/src/api/get/product.rs +++ b/crates/rocie-server/src/api/get/product.rs @@ -16,7 +16,7 @@ use crate::{ ("id" = ProductId, description = "Product id" ), ) )] -#[get("/product/{id}")] +#[get("/product/by-id/{id}")] pub(crate) async fn product_by_id( app: web::Data<App>, id: web::Path<ProductIdStub>, @@ -29,6 +29,59 @@ pub(crate) async fn product_by_id( } } +/// Get Product by name +#[utoipa::path( + responses( + (status = OK, description = "Product found from database", body = Product), + (status = NOT_FOUND, description = "Product not found in database"), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), + params( + ("name" = String, description = "Name of the product" ), + ) +)] +// TODO: Html decode the name, before use. Otherwise `Milk 2` will not work, as it is send as +// `Milk+2` <2025-10-21> +#[get("/product/by-name/{name}")] +pub(crate) async fn product_by_name( + app: web::Data<App>, + name: web::Path<String>, +) -> Result<impl Responder> { + let name = name.into_inner(); + + match Product::from_name(&app, name).await? { + Some(product) => Ok(HttpResponse::Ok().json(product)), + None => Ok(HttpResponse::NotFound().finish()), + } +} + +/// Get Product suggestion by name +#[utoipa::path( + responses( + (status = OK, description = "Product suggestions found from database", body = Vec<Product>), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), + params( + ("name" = String, description = "Partial name of a product" ), + ) +)] +#[get("/product/by-part-name/{name}")] +pub(crate) async fn product_suggestion_by_name( + app: web::Data<App>, + name: web::Path<String>, +) -> Result<impl Responder> { + let name = name.into_inner(); + + let all = Product::get_all(&app).await?; + + let matching = all + .into_iter() + .filter(|product| product.name.starts_with(name.as_str())) + .collect::<Vec<_>>(); + + Ok(HttpResponse::Ok().json(matching)) +} + /// Return all registered products #[utoipa::path( responses( |
