Add fetch by barcode API method

This commit is contained in:
Matej Drobnič 2019-03-10 12:20:31 +01:00
parent e693460894
commit 3f4a5cc0d6
4 changed files with 73 additions and 0 deletions

View File

@ -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) public function ProductPriceHistory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try

View File

@ -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": { "/stock/products/{productId}/entries": {
"get": { "get": {
"summary": "Returns all stock entries of the given product in order of next use (first expiring first, then first in first out)", "summary": "Returns all stock entries of the given product in order of next use (first expiring first, then first in first out)",

View File

@ -135,6 +135,7 @@ $app->group('/api', function()
$this->get('/stock', '\Grocy\Controllers\StockApiController:CurrentStock'); $this->get('/stock', '\Grocy\Controllers\StockApiController:CurrentStock');
$this->get('/stock/volatile', '\Grocy\Controllers\StockApiController:CurrentVolatilStock'); $this->get('/stock/volatile', '\Grocy\Controllers\StockApiController:CurrentVolatilStock');
$this->get('/stock/products/{productId}', '\Grocy\Controllers\StockApiController:ProductDetails'); $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}/entries', '\Grocy\Controllers\StockApiController:ProductStockEntries');
$this->get('/stock/products/{productId}/price-history', '\Grocy\Controllers\StockApiController:ProductPriceHistory'); $this->get('/stock/products/{productId}/price-history', '\Grocy\Controllers\StockApiController:ProductPriceHistory');
$this->post('/stock/products/{productId}/add', '\Grocy\Controllers\StockApiController:AddProduct'); $this->post('/stock/products/{productId}/add', '\Grocy\Controllers\StockApiController:AddProduct');

View File

@ -38,6 +38,24 @@ class StockService extends BaseService
return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); 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) public function GetExpiringProducts(int $days = 5, bool $excludeExpired = false)
{ {
$currentStock = $this->GetCurrentStock(true); $currentStock = $this->GetCurrentStock(true);