mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 09:39:57 +00:00
Improve API related changes regarding multiple shopping lists (references #190)
This commit is contained in:
parent
49d16b458d
commit
162adeb359
@ -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)
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 : '';
|
||||
|
Loading…
x
Reference in New Issue
Block a user