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

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