Skip to content

Commit

Permalink
Add fetch by barcode API method
Browse files Browse the repository at this point in the history
  • Loading branch information
matejdro committed Mar 10, 2019
1 parent e693460 commit 3f4a5cc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
13 changes: 13 additions & 0 deletions controllers/StockApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public function ProductDetails(\Slim\Http\Request $request, \Slim\Http\Response
}
}

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
Expand Down
41 changes: 41 additions & 0 deletions grocy.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
1 change: 1 addition & 0 deletions routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
$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');
Expand Down
18 changes: 18 additions & 0 deletions services/StockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public function GetMissingProducts()
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);
Expand Down

0 comments on commit 3f4a5cc

Please sign in to comment.