From 162adeb359a97660adccc2952b2b184eec82da97 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 22 Apr 2019 08:21:57 +0200 Subject: [PATCH] Improve API related changes regarding multiple shopping lists (references #190) --- controllers/StockApiController.php | 46 +++++++++++++++++++----------- grocy.openapi.json | 20 +++++++++++++ services/StockService.php | 16 +++++++++++ 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 963d9f87..543b995a 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -242,30 +242,44 @@ class StockApiController extends BaseApiController public function AddMissingProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - $requestBody = $request->getParsedBody(); - - $listId = 1; - if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) + try { - $listId = intval($requestBody['list_id']); - } + $requestBody = $request->getParsedBody(); - $this->StockService->AddMissingProductsToShoppingList($listId); - return $this->EmptyApiResponse($response); + $listId = 1; + if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) + { + $listId = intval($requestBody['list_id']); + } + + $this->StockService->AddMissingProductsToShoppingList($listId); + return $this->EmptyApiResponse($response); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } } public function ClearShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - $requestBody = $request->getParsedBody(); - - $listId = 1; - if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) + try { - $listId = intval($requestBody['list_id']); - } + $requestBody = $request->getParsedBody(); - $this->StockService->ClearShoppingList($listId); - return $this->EmptyApiResponse($response); + $listId = 1; + if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) + { + $listId = intval($requestBody['list_id']); + } + + $this->StockService->ClearShoppingList($listId); + return $this->EmptyApiResponse($response); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } } public function ExternalBarcodeLookup(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) diff --git a/grocy.openapi.json b/grocy.openapi.json index 25fc2eb2..48973de6 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1356,6 +1356,16 @@ "responses": { "204": { "description": "The operation was successful" + }, + "400": { + "description": "The operation was not successful (possible errors are: Not existing shopping list)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } } } } @@ -1388,6 +1398,16 @@ "responses": { "204": { "description": "The operation was successful" + }, + "400": { + "description": "The operation was not successful (possible errors are: Not existing shopping list)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } } } } diff --git a/services/StockService.php b/services/StockService.php index 17e010ed..3d3b321c 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -432,6 +432,11 @@ class StockService extends BaseService public function AddMissingProductsToShoppingList($listId = 1) { + if (!$this->ShoppingListExists($listId)) + { + throw new \Exception('Shopping list does not exist'); + } + $missingProducts = $this->GetMissingProducts(); foreach ($missingProducts as $missingProduct) { @@ -463,6 +468,11 @@ class StockService extends BaseService public function ClearShoppingList($listId = 1) { + if (!$this->ShoppingListExists($listId)) + { + throw new \Exception('Shopping list does not exist'); + } + $this->Database->shopping_list()->where('shopping_list_id = :1', $listId)->delete(); } @@ -472,6 +482,12 @@ class StockService extends BaseService return $productRow !== null; } + private function ShoppingListExists($listId) + { + $shoppingListRow = $this->Database->shopping_lists()->where('id = :1', $listId)->fetch(); + return $shoppingListRow !== null; + } + private function LoadBarcodeLookupPlugin() { $pluginName = defined('GROCY_STOCK_BARCODE_LOOKUP_PLUGIN') ? GROCY_STOCK_BARCODE_LOOKUP_PLUGIN : '';