Improve API related changes regarding multiple shopping lists (references #190)

This commit is contained in:
Bernd Bestel 2019-04-22 08:21:57 +02:00
parent 49d16b458d
commit 162adeb359
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
3 changed files with 66 additions and 16 deletions

View File

@ -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)

View File

@ -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"
}
}
}
}
}
}

View File

@ -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 : '';