Add API call for adding products to shoppinglist

This commit is contained in:
Marc Ole Bulling 2019-08-30 09:21:11 +02:00
parent afa4165d1c
commit d4eb767f1b
4 changed files with 114 additions and 0 deletions

View File

@ -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) public function RemoveItemFromShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {

View File

@ -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": { "/stock/shoppinglist/remove-product": {
"post": { "post": {
"summary": "Removes the given product from the given shopping list, if it is on it", "summary": "Removes the given product from the given shopping list, if it is on it",

View File

@ -169,6 +169,7 @@ $app->group('/api', function()
{ {
$this->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); $this->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList');
$this->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList'); $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'); $this->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveItemFromShoppingList');
} }

View File

@ -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) private function ProductExists($productId)
{ {
$productRow = $this->Database->products()->where('id = :1', $productId)->fetch(); $productRow = $this->Database->products()->where('id = :1', $productId)->fetch();