From 1a23eaabf16ff3cc5eab8299525107c0b12d408e Mon Sep 17 00:00:00 2001 From: Forceu Date: Sun, 4 Aug 2019 20:31:47 +0200 Subject: [PATCH] Add API call to remove an item from the shopping list by productid This adds an API call, so a shopping list item can easily be removed Please note: This is UNTESTED, as I was unable to run the current grocy master commit on my server. --- controllers/StockApiController.php | 38 ++++++++++++++++++++++ grocy.openapi.json | 52 ++++++++++++++++++++++++++++++ routes.php | 1 + services/StockService.php | 22 +++++++++++++ 4 files changed, 113 insertions(+) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 78ba58e0..4021587a 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -288,6 +288,44 @@ class StockApiController extends BaseApiController } } + + + public function RemoveFromShoppingList(\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->RemoveProductFromShoppingList($productId, $amount, $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) { try diff --git a/grocy.openapi.json b/grocy.openapi.json index 15e009fd..e55c4b61 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -1534,6 +1534,58 @@ } } }, + "/stock/shoppinglist/remove-from-list": { + "post": { + "summary": "Removes a product from the shoppinglist, if it is on it", + "tags": [ + "Stock" + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "product_id": { + "type": "integer", + "description": "The product id from the item on the shopping list" + }, + "list_id": { + "type": "integer", + "description": "The shopping list id to use, when omitted, the default shopping list (with id 1) is used" + }, + "product_amount": { + "type": "integer", + "description": "The amount of prodcut units to remove, 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, No product id supplied)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } + } + } + } + }, "/stock/bookings/{bookingId}/undo": { "post": { "summary": "Undoes a booking", diff --git a/routes.php b/routes.php index f32726ef..65b378b3 100644 --- a/routes.php +++ b/routes.php @@ -167,6 +167,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/remove-from-list', '\Grocy\Controllers\StockApiController:RemoveFromShoppingList'); } // Recipes diff --git a/services/StockService.php b/services/StockService.php index 13101dec..81623b1e 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -496,6 +496,28 @@ class StockService extends BaseService $this->Database->shopping_list()->where('shopping_list_id = :1', $listId)->delete(); } + + public function RemoveProductFromShoppingList($productId, $amount = 1, $listId = 1) + { + if (!$this->ShoppingListExists($listId)) + { + throw new \Exception('Shopping list does not exist'); + } + $productRow = $this->Database->shopping_list()->where('product_id = :1', $productId)->fetch(); + //If no entry was found with for this product, we return gracefully + if ($productRow != null && !empty($productRow)) + { + $newAmount = $productRow->amount - $amount; + if ($newAmount < 1) + { + $productRow->delete(); + } else { + $productRow->update(array('amount' => $newAmount)); + } + + } + } + private function ProductExists($productId) { $productRow = $this->Database->products()->where('id = :1', $productId)->fetch();