From 3f4a5cc0d66bb2f235d09c15ddf5e24a027e913b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Drobni=C4=8D?= Date: Sun, 10 Mar 2019 12:20:31 +0100 Subject: [PATCH] Add fetch by barcode API method --- controllers/StockApiController.php | 13 ++++++++++ grocy.openapi.json | 41 ++++++++++++++++++++++++++++++ routes.php | 1 + services/StockService.php | 18 +++++++++++++ 4 files changed, 73 insertions(+) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 2469b06a..fb54640a 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -26,6 +26,19 @@ class StockApiController extends BaseApiController } } + public function ProductDetailsByBarcode(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + { + try + { + $productId = $this->StockService->GetProductIdFromBarcode($args['barcode']); + return $this->ApiResponse($this->StockService->GetProductDetails($productId)); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } + } + public function ProductPriceHistory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { try diff --git a/grocy.openapi.json b/grocy.openapi.json index ae358859..80dd05d3 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -908,6 +908,47 @@ } } }, + "/stock/products/by-barcode/{barcode}": { + "get": { + "summary": "Returns details of the product from its barcode", + "tags": [ + "Stock" + ], + "parameters": [ + { + "in": "path", + "name": "barcode", + "required": true, + "description": "Barcode", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "A ProductDetailsResponse object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProductDetailsResponse" + } + } + } + }, + "400": { + "description": "The operation was not successful (possible errors are: Unknown barcode)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } + } + } + } + }, "/stock/products/{productId}/entries": { "get": { "summary": "Returns all stock entries of the given product in order of next use (first expiring first, then first in first out)", diff --git a/routes.php b/routes.php index 4cbce34b..d22b1dd6 100644 --- a/routes.php +++ b/routes.php @@ -135,6 +135,7 @@ $app->group('/api', function() $this->get('/stock', '\Grocy\Controllers\StockApiController:CurrentStock'); $this->get('/stock/volatile', '\Grocy\Controllers\StockApiController:CurrentVolatilStock'); $this->get('/stock/products/{productId}', '\Grocy\Controllers\StockApiController:ProductDetails'); + $this->get('/stock/products/by-barcode/{barcode}', '\Grocy\Controllers\StockApiController:ProductDetailsByBarcode'); $this->get('/stock/products/{productId}/entries', '\Grocy\Controllers\StockApiController:ProductStockEntries'); $this->get('/stock/products/{productId}/price-history', '\Grocy\Controllers\StockApiController:ProductPriceHistory'); $this->post('/stock/products/{productId}/add', '\Grocy\Controllers\StockApiController:AddProduct'); diff --git a/services/StockService.php b/services/StockService.php index 8a4600f8..3ce4591a 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -38,6 +38,24 @@ class StockService extends BaseService return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); } + public function GetProductIdFromBarcode(string $barcode) + { + $sql = "SELECT id FROM products WHERE (',' || barcode || ',') LIKE '%,' || :barcode || ',%'"; + $query = $this->DatabaseService->ExecuteDbQuery($sql); + + $query->bindParam("barcode", $barcode); + + $query->execute(); + + $productId = $query->fetchColumn(0); + + if ($productId == null) + { + throw new \Exception("Product with barcode $barcode does not exist"); + } + return $productId; + } + public function GetExpiringProducts(int $days = 5, bool $excludeExpired = false) { $currentStock = $this->GetCurrentStock(true);