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
4 changed files with 113 additions and 0 deletions

View File

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