src/Controller/Api/RecipesController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Helper\ConvertUnitMeasures;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use App\Controller\Response;
  7. use App\Entity\RecipeIngredients;
  8. use App\Entity\Recipes;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * @Route("/api/recipes", name="api_recipes_")
  13.  */
  14. class RecipesController extends AbstractController
  15. {
  16.     use Response;
  17.     use \App\Controller\Request;
  18.     /**
  19.      * @Route("/{id}", name="view", methods={"GET"})
  20.      */
  21.     public function viewRecipeAction($idRequest $requestEntityManagerInterface $em)
  22.     {
  23.         try {
  24.             $user $this->getUser(); // TOKEN
  25.             $unitMeasure $user->getUnitMeasure();
  26.             $lang $this->getQueryLang($request);
  27.             $repository $em->getRepository(Recipes::class);
  28.             $recipe $repository->findById($id$lang);
  29.             if ($recipe) {
  30.                 $ingredientsRepository $em->getRepository(RecipeIngredients::class);
  31.                 $recipe['ingredients'] = $ingredientsRepository->findByRecipe($id$lang);
  32.                 foreach ($recipe['ingredients'] as &$ingredient){
  33.                     if($unitMeasure == 2){
  34.                         if($ingredient['abbreviation'] === 'g'){
  35.                             $ingredient['converted_unit_measure'] = ConvertUnitMeasures::convertGrams($ingredient['quantity'], $lang);
  36.                         } else if($ingredient['abbreviation'] === 'ml'){
  37.                             $ingredient['converted_unit_measure'] = ConvertUnitMeasures::convertMl($ingredient['quantity'], $lang);
  38.                         } else {
  39.                             $ingredient['converted_unit_measure'] = $ingredient['quantity'] . ' ' $ingredient['abbreviation'];
  40.                         }
  41.                     } else {
  42.                         $ingredient['converted_unit_measure'] = $ingredient['quantity'] . ' ' $ingredient['abbreviation'];
  43.                     }
  44.                 }
  45.             }
  46.             return $this->jsonResponse(['data' => $recipe]);
  47.         } catch (\Exception $e) {
  48.             return $this->errorJsonResponse(['message' => $e->getMessage()]);
  49.         }
  50.     }
  51. }