feat: Added recipes/requirements route

- feat: Added requirements route to allow clients to access the requirements fulfilments of recipes
This commit is contained in:
grocy 2019-06-20 23:10:30 -05:00
parent a9c0539305
commit 7a51fb77b0
3 changed files with 3181 additions and 2687 deletions

View File

@ -1,43 +1,63 @@
<?php <?php
namespace Grocy\Controllers; namespace Grocy\Controllers;
use \Grocy\Services\RecipesService; use \Grocy\Services\RecipesService;
class RecipesApiController extends BaseApiController class RecipesApiController extends BaseApiController
{ {
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->RecipesService = new RecipesService(); $this->RecipesService = new RecipesService();
} }
protected $RecipesService; protected $RecipesService;
public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
$requestBody = $request->getParsedBody(); $requestBody = $request->getParsedBody();
$excludedProductIds = null; $excludedProductIds = null;
if ($requestBody !== null && array_key_exists('excludedProductIds', $requestBody)) if ($requestBody !== null && array_key_exists('excludedProductIds', $requestBody))
{ {
$excludedProductIds = $requestBody['excludedProductIds']; $excludedProductIds = $requestBody['excludedProductIds'];
} }
$this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds); $this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
public function ConsumeRecipe(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function ConsumeRecipe(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try
{ {
$this->RecipesService->ConsumeRecipe($args['recipeId']); $this->RecipesService->ConsumeRecipe($args['recipeId']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
return $this->GenericErrorResponse($response, $ex->getMessage()); return $this->GenericErrorResponse($response, $ex->getMessage());
} }
} }
}
public function GetRecipeRequirements(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
try {
if(!$args['recipeId']){
return $this->ApiResponse($this->RecipesService->GetRecipesResolved());
}
$recipeResolved = FindObjectInArrayByPropertyValue($this->RecipesService->GetRecipesResolved(), 'recipe_id', $args['recipeId']);
if(!$recipeResolved) {
$errorMsg ='Recipe requirments do not exist for recipe_id ' . $args['recipe_id'];
$GenericError = $this->GenericErrorResponse($response, $errorMsg);
return $GenericError;
}
return $this->ApiResponse($recipeResolved);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -156,6 +156,8 @@ $app->group('/api', function()
if (GROCY_FEATURE_FLAG_RECIPES) if (GROCY_FEATURE_FLAG_RECIPES)
{ {
$this->post('/recipes/{recipeId}/add-not-fulfilled-products-to-shoppinglist', '\Grocy\Controllers\RecipesApiController:AddNotFulfilledProductsToShoppingList'); $this->post('/recipes/{recipeId}/add-not-fulfilled-products-to-shoppinglist', '\Grocy\Controllers\RecipesApiController:AddNotFulfilledProductsToShoppingList');
$this->get('/recipes/{recipeId}/requirements', '\Grocy\Controllers\RecipesApiController:GetRecipeRequirements');
$this->get('/recipes/requirements', '\Grocy\Controllers\RecipesApiController:GetRecipeRequirements');
$this->post('/recipes/{recipeId}/consume', '\Grocy\Controllers\RecipesApiController:ConsumeRecipe'); $this->post('/recipes/{recipeId}/consume', '\Grocy\Controllers\RecipesApiController:ConsumeRecipe');
} }