From 3a6f04f7704ea31eaded388e1b3df4093a719bbd Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 6 Jan 2022 14:07:29 +0100 Subject: [PATCH] Optionally remove only done shopping list items (`/stock/shoppinglist/clear` API endpoint, closes #1730) --- changelog/66_UNRELEASED_xxxx-xx-xx.md | 1 + controllers/StockApiController.php | 9 +++++++-- grocy.openapi.json | 7 ++++++- services/StockService.php | 11 +++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/changelog/66_UNRELEASED_xxxx-xx-xx.md b/changelog/66_UNRELEASED_xxxx-xx-xx.md index 56dceae2..e8a3e90a 100644 --- a/changelog/66_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/66_UNRELEASED_xxxx-xx-xx.md @@ -7,3 +7,4 @@ - Fixed that the barcode lookup for the "Stock by-barcode" API endpoints was case sensitive - Fixed that the logout button/menu was missing when using external authentication (e.g. LDAP) - Fixed that the consume page/dialog wasn't properly initialized when opening it from the stock entries page +- The API endpoint `/stock/shoppinglist/clear` has now a new optional request body parameter `done_only` (to only remove done items from the given shopping list, defaults to `false`) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index e216f32f..540a565f 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -233,13 +233,18 @@ class StockApiController extends BaseApiController $requestBody = $this->GetParsedAndFilteredRequestBody($request); $listId = 1; - if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) { $listId = intval($requestBody['list_id']); } - $this->getStockService()->ClearShoppingList($listId); + $doneOnly = false; + if (array_key_exists('done_only', $requestBody) && filter_var($requestBody['done_only'], FILTER_VALIDATE_BOOLEAN) !== false) + { + $doneOnly = boolval($requestBody['done_only']); + } + + $this->getStockService()->ClearShoppingList($listId, $doneOnly); return $this->EmptyApiResponse($response); } catch (\Exception $ex) diff --git a/grocy.openapi.json b/grocy.openapi.json index 6a6a8c26..ba606dd7 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -2961,10 +2961,15 @@ "list_id": { "type": "integer", "description": "The shopping list id to clear, when omitted, the default shopping list (with id 1) is used" + }, + "done_only": { + "type": "boolean", + "description": "When `true`, only done items will be removed (defaults to `false` when ommited)" } }, "example": { - "list_id": 2 + "list_id": 2, + "done_only": false } } } diff --git a/services/StockService.php b/services/StockService.php index 49fe4b53..e6460c6c 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -336,14 +336,21 @@ class StockService extends BaseService } } - public function ClearShoppingList($listId = 1) + public function ClearShoppingList($listId = 1, $doneOnly = false) { if (!$this->ShoppingListExists($listId)) { throw new \Exception('Shopping list does not exist'); } - $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId)->delete(); + if ($doneOnly) + { + $this->getDatabase()->shopping_list()->where('shopping_list_id = :1 AND IFNULL(done, 0) = 1', $listId)->delete(); + } + else + { + $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId)->delete(); + } } public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null, &$transactionId = null, $allowSubproductSubstitution = false, $consumeExactAmount = false)