From b17b0fc926cf19040b07c5e6c436acd09b3771e6 Mon Sep 17 00:00:00 2001 From: Gregory SACRE Date: Sun, 19 Jan 2020 00:22:16 +0100 Subject: [PATCH] FR #496: Removed extra GET entry to include product by default The '/api/stock' now returns an extra attribute ('product') with the associated product details. --- controllers/StockApiController.php | 5 -- grocy.openapi.json | 117 +---------------------------- routes.php | 1 - services/StockService.php | 22 ++++-- 4 files changed, 18 insertions(+), 127 deletions(-) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 5199c5e1..64e4b716 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -388,11 +388,6 @@ class StockApiController extends BaseApiController return $this->ApiResponse($this->StockService->GetCurrentStock()); } - public function CurrentStockFull(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) - { - return $this->ApiResponse($this->StockService->GetCurrentStockFull()); - } - public function CurrentVolatilStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { $nextXDays = 5; diff --git a/grocy.openapi.json b/grocy.openapi.json index bf226b85..87ebb3fa 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1124,29 +1124,6 @@ } } }, - "/stock/full": { - "get": { - "summary": "Returns all products which are currently in stock incl. product details", - "tags": [ - "Stock" - ], - "responses": { - "200": { - "description": "An array of CurrentStockResponse objects with product details", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/CurrentStockResponseFull" - } - } - } - } - } - } - } - }, "/stock/volatile": { "get": { "summary": "Returns all products which are expiring soon, are already expired or currently missing", @@ -4048,99 +4025,9 @@ "is_aggregated_amount": { "type": "boolean", "description": "Indicates wheter this product has sub-products or not / if the fields `amount_aggregated` and `amount_opened_aggregated` are filled" - } - } - }, - "CurrentStockResponseFull": { - "type": "object", - "properties": { - "product_id": { - "type": "integer" }, - "amount": { - "type": "number" - }, - "amount_aggregated": { - "type": "number" - }, - "amount_opened": { - "type": "number" - }, - "amount_opened_aggregated": { - "type": "number" - }, - "best_before_date": { - "type": "string", - "format": "date", - "description": "The next best before date for this product" - }, - "is_aggregated_amount": { - "type": "boolean", - "description": "Indicates wheter this product has sub-products or not / if the fields `amount_aggregated` and `amount_opened_aggregated` are filled" - }, - "id": { - "type": "integer" - }, - "name": { - "type": "string" - }, - "description": { - "type": "string" - }, - "location_id": { - "type": "integer" - }, - "qu_id_purchase": { - "type": "integer" - }, - "qu_id_stock": { - "type": "integer" - }, - "enable_tare_weight_handling": { - "type": "integer" - }, - "not_check_stock_fulfillment_for_recipes": { - "type": "integer" - }, - "product_group_id": { - "type": "integer" - }, - "qu_factor_purchase_to_stock": { - "type": "number", - "format": "number" - }, - "tare_weight": { - "type": "number", - "format": "number" - }, - "barcode": { - "type": "string", - "description": "Can contain multiple barcodes separated by comma" - }, - "min_stock_amount": { - "type": "integer", - "minimum": 0, - "default": 0 - }, - "default_best_before_days": { - "type": "integer", - "minimum": 0, - "default": 0 - }, - "default_best_before_days_after_open": { - "type": "integer", - "minimum": 0, - "default": 0 - }, - "picture_file_name": { - "type": "string" - }, - "allow_partial_units_in_stock": { - "type": "boolean" - }, - "row_created_timestamp": { - "type": "string", - "format": "date-time" + "product": { + "$ref": "#/components/schemas/Product" } } }, diff --git a/routes.php b/routes.php index 857d831b..30e93fc5 100644 --- a/routes.php +++ b/routes.php @@ -162,7 +162,6 @@ $app->group('/api', function() { $this->get('/stock', '\Grocy\Controllers\StockApiController:CurrentStock'); $this->put('/stock', '\Grocy\Controllers\StockApiController:EditStock'); - $this->get('/stock/full', '\Grocy\Controllers\StockApiController:CurrentStockFull'); $this->get('/stock/volatile', '\Grocy\Controllers\StockApiController:CurrentVolatilStock'); $this->get('/stock/products/{productId}', '\Grocy\Controllers\StockApiController:ProductDetails'); $this->get('/stock/products/{productId}/entries', '\Grocy\Controllers\StockApiController:ProductStockEntries'); diff --git a/services/StockService.php b/services/StockService.php index 8d99fe3f..1ae9536d 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -27,14 +27,24 @@ class StockService extends BaseService $sql = 'SELECT * FROM stock_current WHERE best_before_date IS NOT NULL UNION SELECT id, 0, 0, null, 0, 0, 0 FROM ' . $missingProductsView . ' WHERE id NOT IN (SELECT product_id FROM stock_current)'; } - return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); - } + $current_stock = $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); + $stock_array = []; - public function GetCurrentStockFull() - { - $sql = 'select * from stock_current join products on products.id = stock_current.product_id'; + foreach ($current_stock as $cur) { + $stock_array[$cur->product_id] = $cur; + } - return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); + $list_of_product_ids = implode(",", array_keys($stock_array)); + + $sql = 'SELECT * FROM products WHERE id in (' . $list_of_product_ids . ')'; + + $products = $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); + + foreach ($products as $product) { + $stock_array[$product->id]->product = $product; + } + + return $stock_array; } public function GetCurrentStockLocationContent()