Files
grocy/controllers/RecipesApiController.php
T

71 lines
2.1 KiB
PHP
Raw Normal View History

2019-06-20 23:10:30 -05:00
<?php
namespace Grocy\Controllers;
2020-08-29 12:05:32 +02:00
use Grocy\Controllers\Users\User;
2019-06-20 23:10:30 -05:00
class RecipesApiController extends BaseApiController
{
2020-02-11 17:42:03 +01:00
public function AddNotFulfilledProductsToShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
2019-06-20 23:10:30 -05:00
{
2020-08-29 16:41:27 +02:00
User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD);
2020-08-29 12:05:32 +02:00
2020-08-29 16:41:27 +02:00
$requestBody = $request->getParsedBody();
2019-06-20 23:10:30 -05:00
$excludedProductIds = null;
if ($requestBody !== null && array_key_exists('excludedProductIds', $requestBody))
{
$excludedProductIds = $requestBody['excludedProductIds'];
}
$this->getRecipesService()->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds);
2019-06-20 23:10:30 -05:00
return $this->EmptyApiResponse($response);
}
2020-02-11 17:42:03 +01:00
public function ConsumeRecipe(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
2019-06-20 23:10:30 -05:00
{
User::checkPermission($request, User::PERMISSION_STOCK_CONSUME);
2020-08-29 12:05:32 +02:00
2020-08-29 16:41:27 +02:00
try
2019-06-20 23:10:30 -05:00
{
$this->getRecipesService()->ConsumeRecipe($args['recipeId']);
2019-06-20 23:10:30 -05:00
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2020-02-11 17:42:03 +01:00
public function GetRecipeFulfillment(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
try
{
2020-08-31 20:40:31 +02:00
if (!isset($args['recipeId']))
{
2020-09-01 19:59:40 +02:00
return $this->FilteredApiResponse($response, $this->getRecipesService()->GetRecipesResolved(), $request->getQueryParams());
}
$recipeResolved = FindObjectInArrayByPropertyValue($this->getRecipesService()->GetRecipesResolved(), 'recipe_id', $args['recipeId']);
2020-08-31 20:40:31 +02:00
if (!$recipeResolved)
{
throw new \Exception('Recipe does not exist');
}
else
{
2020-02-11 17:42:03 +01:00
return $this->ApiResponse($response, $recipeResolved);
}
}
catch (\Exception $ex)
{
2019-06-20 23:10:30 -05:00
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2020-08-31 20:40:31 +02:00
public function __construct(\DI\Container $container)
{
parent::__construct($container);
}
2019-06-20 23:10:30 -05:00
}