Files
grocy/controllers/Api/RecipesApiController.php

110 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2021-07-03 17:46:47 +02:00
<?php
namespace Grocy\Controllers\Api;
2021-07-03 17:46:47 +02:00
use Grocy\Controllers\Users\User;
use Grocy\Helpers\Grocycode;
2026-04-24 19:59:42 +02:00
use Grocy\Helpers\WebhookRunner;
use Grocy\Services\RecipesService;
2023-05-13 14:43:51 +02:00
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
2021-07-03 17:46:47 +02:00
class RecipesApiController extends BaseApiController
{
2023-05-13 14:43:51 +02:00
public function AddNotFulfilledProductsToShoppingList(Request $request, Response $response, array $args)
2021-07-03 17:46:47 +02:00
{
User::CheckPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD);
2021-07-03 17:46:47 +02:00
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
$excludedProductIds = null;
if ($requestBody !== null && array_key_exists('excludedProductIds', $requestBody))
{
$excludedProductIds = $requestBody['excludedProductIds'];
}
RecipesService::GetInstance()->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds);
2021-07-03 17:46:47 +02:00
return $this->EmptyApiResponse($response);
}
2023-05-13 14:43:51 +02:00
public function ConsumeRecipe(Request $request, Response $response, array $args)
2021-07-03 17:46:47 +02:00
{
User::CheckPermission($request, User::PERMISSION_STOCK_CONSUME);
2021-07-03 17:46:47 +02:00
try
{
RecipesService::GetInstance()->ConsumeRecipe($args['recipeId']);
2021-07-03 17:46:47 +02:00
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2023-05-13 14:43:51 +02:00
public function GetRecipeFulfillment(Request $request, Response $response, array $args)
2021-07-03 17:46:47 +02:00
{
try
{
if (!isset($args['recipeId']))
{
return $this->FilteredApiResponse($response, RecipesService::GetInstance()->GetRecipesResolved(), $request->getQueryParams());
2021-07-03 17:46:47 +02:00
}
$recipeResolved = FindObjectInArrayByPropertyValue(RecipesService::GetInstance()->GetRecipesResolved(), 'recipe_id', $args['recipeId']);
2021-07-03 17:46:47 +02:00
if (!$recipeResolved)
{
throw new \Exception('Recipe does not exist');
}
else
{
return $this->ApiResponse($response, $recipeResolved);
}
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2023-05-13 14:43:51 +02:00
public function CopyRecipe(Request $request, Response $response, array $args)
{
try
{
return $this->ApiResponse($response, [
'created_object_id' => RecipesService::GetInstance()->CopyRecipe($args['recipeId'])
]);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2023-05-13 14:43:51 +02:00
public function RecipePrintLabel(Request $request, Response $response, array $args)
{
try
{
$recipe = $this->DB->recipes()->where('id', $args['recipeId'])->fetch();
$webhookData = array_merge([
'recipe' => $recipe->name,
'grocycode' => (string)(new Grocycode(Grocycode::RECIPE, $args['recipeId'])),
'details' => $recipe
], GROCY_LABEL_PRINTER_PARAMS);
if (GROCY_LABEL_PRINTER_RUN_SERVER)
{
(new WebhookRunner())->run(GROCY_LABEL_PRINTER_WEBHOOK, $webhookData, GROCY_LABEL_PRINTER_HOOK_JSON);
}
return $this->ApiResponse($response, $webhookData);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2021-07-03 17:46:47 +02:00
}