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.
This commit is contained in:
Forceu 2019-08-04 20:31:47 +02:00 committed by GitHub
parent 3dbce7547f
commit 1a23eaabf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 113 additions and 0 deletions

View File

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

View File

@ -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": { "/stock/bookings/{bookingId}/undo": {
"post": { "post": {
"summary": "Undoes a booking", "summary": "Undoes a booking",

View File

@ -167,6 +167,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/remove-from-list', '\Grocy\Controllers\StockApiController:RemoveFromShoppingList');
} }
// Recipes // Recipes

View File

@ -496,6 +496,28 @@ class StockService extends BaseService
$this->Database->shopping_list()->where('shopping_list_id = :1', $listId)->delete(); $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) private function ProductExists($productId)
{ {
$productRow = $this->Database->products()->where('id = :1', $productId)->fetch(); $productRow = $this->Database->products()->where('id = :1', $productId)->fetch();