diff --git a/grocy.openapi.json b/grocy.openapi.json index 80dd05d3..2d9f6647 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -910,7 +910,7 @@ }, "/stock/products/by-barcode/{barcode}": { "get": { - "summary": "Returns details of the product from its barcode", + "summary": "Returns details of the given product by its barcode", "tags": [ "Stock" ], diff --git a/services/StockService.php b/services/StockService.php index 3ce4591a..9773e941 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -40,20 +40,14 @@ class StockService extends BaseService public function GetProductIdFromBarcode(string $barcode) { - $sql = "SELECT id FROM products WHERE (',' || barcode || ',') LIKE '%,' || :barcode || ',%'"; - $query = $this->DatabaseService->ExecuteDbQuery($sql); + $potentialProduct = $this->Database->products()->where('barcode LIKE :1', '%' . $barcode . '%')->limit(1)->fetch(); - $query->bindParam("barcode", $barcode); - - $query->execute(); - - $productId = $query->fetchColumn(0); - - if ($productId == null) + if ($potentialProduct === null) { - throw new \Exception("Product with barcode $barcode does not exist"); + throw new \Exception("No product with barcode $barcode found"); } - return $productId; + + return intval($potentialProduct->id); } public function GetExpiringProducts(int $days = 5, bool $excludeExpired = false)