aboutsummaryrefslogtreecommitdiffstats
path: root/crates/rocie-server/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crates/rocie-server/src/api/get/mod.rs2
-rw-r--r--crates/rocie-server/src/api/get/product.rs55
-rw-r--r--crates/rocie-server/src/main.rs2
-rw-r--r--crates/rocie-server/src/storage/sql/get/product/mod.rs25
-rw-r--r--crates/rocie-server/src/storage/sql/product.rs2
5 files changed, 84 insertions, 2 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(
diff --git a/crates/rocie-server/src/main.rs b/crates/rocie-server/src/main.rs
index 8e10763..2329b0b 100644
--- a/crates/rocie-server/src/main.rs
+++ b/crates/rocie-server/src/main.rs
@@ -20,6 +20,8 @@ async fn main() -> Result<(), std::io::Error> {
#[openapi(
paths(
api::get::product::product_by_id,
+ api::get::product::product_by_name,
+ api::get::product::product_suggestion_by_name,
api::get::product::products,
api::get::unit::units,
api::get::unit::unit_by_id,
diff --git a/crates/rocie-server/src/storage/sql/get/product/mod.rs b/crates/rocie-server/src/storage/sql/get/product/mod.rs
index 01047b2..0df51d8 100644
--- a/crates/rocie-server/src/storage/sql/get/product/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/product/mod.rs
@@ -36,6 +36,31 @@ impl Product {
}
}
+ pub(crate) async fn from_name(app: &App, name: String) -> Result<Option<Self>, from_id::Error> {
+ let record = query!(
+ "
+ SELECT name, id, unit_property, description, parent
+ FROM products
+ WHERE name = ?
+",
+ name
+ )
+ .fetch_optional(&app.db)
+ .await?;
+
+ if let Some(record) = record {
+ Ok(Some(Self {
+ id: ProductId::from_db(&record.id),
+ unit_property: UnitPropertyId::from_db(&record.unit_property),
+ name,
+ description: record.description,
+ associated_bar_codes: vec![], // todo
+ }))
+ } else {
+ Ok(None)
+ }
+ }
+
pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
let records = query!(
"
diff --git a/crates/rocie-server/src/storage/sql/product.rs b/crates/rocie-server/src/storage/sql/product.rs
index 2575b59..1c5a7d8 100644
--- a/crates/rocie-server/src/storage/sql/product.rs
+++ b/crates/rocie-server/src/storage/sql/product.rs
@@ -20,7 +20,7 @@ pub(crate) struct Product {
/// The name of the product.
///
/// This should be globally unique, to make searching easier for the user.
- pub(super) name: String,
+ pub(crate) name: String,
/// An optional description of this product.
pub(super) description: Option<String>,