From d4eb767f1b602e5279c23c991d1b5d52f6fa0c28 Mon Sep 17 00:00:00 2001 From: Marc Ole Bulling Date: Fri, 30 Aug 2019 09:21:11 +0200 Subject: [PATCH] Add API call for adding products to shoppinglist --- controllers/StockApiController.php | 35 ++++++++++++++++++++ grocy.openapi.json | 52 ++++++++++++++++++++++++++++++ routes.php | 1 + services/StockService.php | 26 +++++++++++++++ 4 files changed, 114 insertions(+) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index e290d123..918ae430 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -289,6 +289,41 @@ class StockApiController extends BaseApiController } + public function AddItemToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + { + try + { + $requestBody = $request->getParsedBody(); + + $listId = 1; + $amount = 1; + $productId = null; + if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) + { + $listId = intval($requestBody['list_id']); + } + if (array_key_exists('product_amount', $requestBody) && !empty($requestBody['product_amount']) && is_numeric($requestBody['product_amount'])) + { + $amount = intval($requestBody['product_amount']); + } + if (array_key_exists('product_id', $requestBody) && !empty($requestBody['product_id']) && is_numeric($requestBody['product_id'])) + { + $productId = intval($requestBody['product_id']); + } + + if ($productId == null) + { + throw new \Exception("No product id was supplied"); + } + + $this->StockService->AddProductToShoppingList($productId, $amount, $listId); + return $this->EmptyApiResponse($response); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } + } public function RemoveItemFromShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { diff --git a/grocy.openapi.json b/grocy.openapi.json index d7f51af3..be617bd3 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1609,6 +1609,58 @@ } } }, + "/stock/shoppinglist/add-product": { + "post": { + "summary": "Adds the given product to the given shopping list", + "tags": [ + "Stock" + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "product_id": { + "type": "integer", + "description": "A valid product id of the item on the shopping list" + }, + "list_id": { + "type": "integer", + "description": "A valid shopping list id, when omitted, the default shopping list (with id 1) is used" + }, + "product_amount": { + "type": "integer", + "description": "The amount of product units to add, when omitted, the default amount of 1 is used" + } + }, + "example": { + "product_id": 3, + "list_id": 2, + "product_amount": 5 + } + } + } + } + }, + "responses": { + "204": { + "description": "The operation was successful" + }, + "400": { + "description": "The operation was not successful (possible errors are: Not existing shopping list, Invalid product id supplied)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } + } + } + } + }, "/stock/shoppinglist/remove-product": { "post": { "summary": "Removes the given product from the given shopping list, if it is on it", diff --git a/routes.php b/routes.php index b99a5e39..577e50e6 100644 --- a/routes.php +++ b/routes.php @@ -169,6 +169,7 @@ $app->group('/api', function() { $this->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); $this->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList'); + $this->post('/stock/shoppinglist/add-product', '\Grocy\Controllers\StockApiController:AddItemToShoppingList'); $this->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveItemFromShoppingList'); } diff --git a/services/StockService.php b/services/StockService.php index 2dd33c4d..68dee04f 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -549,6 +549,32 @@ class StockService extends BaseService } } + public function AddProductToShoppingList($productId, $amount = 1, $listId = 1) + { + if (!$this->ShoppingListExists($listId)) + { + throw new \Exception('Shopping list does not exist'); + } + + $alreadyExistingEntry = $this->Database->shopping_list()->where('product_id', $productId->id)->fetch(); + if ($alreadyExistingEntry) // Update + { + $alreadyExistingEntry->update(array( + 'amount' => ($alreadyExistingEntry->amount + $amount), + 'shopping_list_id' => $listId + )); + } + else // Insert + { + $shoppinglistRow = $this->Database->shopping_list()->createRow(array( + 'product_id' => $productId->id, + 'amount' => $amount, + 'shopping_list_id' => $listId + )); + $shoppinglistRow->save(); + } + } + private function ProductExists($productId) { $productRow = $this->Database->products()->where('id = :1', $productId)->fetch();