Merge pull request #338 from Forceu/master

Add API call to remove an item from the shopping list by productid
This commit is contained in:
Bernd Bestel 2019-08-10 08:04:00 +02:00 committed by GitHub
commit c757ee3874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 124 additions and 1 deletions

View File

@ -67,7 +67,7 @@ class StockApiController extends BaseApiController
throw new \Exception('An amount is required');
}
$bestBeforeDate = date('Y-m-d');
$bestBeforeDate = null;
if (array_key_exists('best_before_date', $requestBody) && IsIsoDate($requestBody['best_before_date']))
{
$bestBeforeDate = $requestBody['best_before_date'];
@ -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)
{
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": {
"post": {
"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/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList');
$this->post('/stock/shoppinglist/remove-from-list', '\Grocy\Controllers\StockApiController:RemoveFromShoppingList');
}
// Recipes

View File

@ -167,6 +167,16 @@ class StockService extends BaseService
$amount = $amount - $productDetails->stock_amount - $productDetails->product->tare_weight;
}
//Sets the default best before date, if none is supplied
if ($bestBeforeDate == null)
{
if ($productDetails->product->default_best_before_days == -1) {
$bestBeforeDate = date('2999-12-31');
} else {
$bestBeforeDate = date('Y-m-d', strtotime(date('Y-m-d') . ' + '.$productDetails->product->default_best_before_days.' days'));
}
}
if ($transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
{
$stockId = uniqid();
@ -496,6 +506,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();