src/Controller/higotrigo/ProductController.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Controller\higotrigo;
  3. use App\Entity\Product;
  4. use App\Form\ProductType;
  5. use App\Repository\ProductRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. /**
  12.  * @Route("/higotrigo/product")
  13.  */
  14. class ProductController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/", name="app_product_index", methods={"GET"})
  18.      */
  19.     public function index(ProductRepository $productRepository): Response
  20.     {
  21.         return $this->render('higotrigo/product/index.html.twig', [
  22.             'products' => $productRepository->findAll(),
  23.         ]);
  24.     }
  25.     /**
  26.      * @Route("/new", name="app_product_new", methods={"GET", "POST"})
  27.      */
  28.     public function new(Request $requestProductRepository $productRepository): Response
  29.     {
  30.         $product = new Product();
  31.         $form $this->createForm(ProductType::class, $product);
  32.         $form->handleRequest($request);
  33.         if ($form->isSubmitted() && $form->isValid()) {
  34.             $user $this->getUser();
  35.             $product->setCreatedId($user);
  36.             $product->setUpdatedId($user);
  37.             $productRepository->add($producttrue);
  38.             return $this->redirectToRoute('app_product_index', [], Response::HTTP_SEE_OTHER);
  39.         }
  40.         return $this->renderForm('higotrigo/product/new.html.twig', [
  41.             'product' => $product,
  42.             'form' => $form,
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/{id}", name="app_product_show", methods={"GET"})
  47.      */
  48.     public function show(Product $product): Response
  49.     {
  50.         return $this->render('higotrigo/product/show.html.twig', [
  51.             'product' => $product,
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/{id}/edit", name="app_product_edit", methods={"GET", "POST"})
  56.      */
  57.     public function edit(Request $requestProduct $productProductRepository $productRepository): Response
  58.     {
  59.         $form $this->createForm(ProductType::class, $product);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted() && $form->isValid()) {
  62.             $user $this->getUser();
  63.             $product->setUpdatedId($user);
  64.             $productRepository->add($producttrue);
  65.             return $this->redirectToRoute('app_product_index', [], Response::HTTP_SEE_OTHER);
  66.         }
  67.         return $this->renderForm('higotrigo/product/edit.html.twig', [
  68.             'product' => $product,
  69.             'form' => $form,
  70.         ]);
  71.     }
  72.     /**
  73.      * @Route("/{id}", name="app_product_delete", methods={"POST"})
  74.      */
  75.     public function delete(Request $requestProduct $productProductRepository $productRepository): Response
  76.     {
  77.         if ($this->isCsrfTokenValid('delete'.$product->getId(), $request->request->get('_token'))) {
  78.             $productRepository->remove($producttrue);
  79.         }
  80.         return $this->redirectToRoute('app_product_index', [], Response::HTTP_SEE_OTHER);
  81.     }
  82.     /**
  83.      * @Route("/{id}/price", name="app_product_get_price", methods={"GET"})
  84.      */
  85.     public function getPreice(Product $product): JsonResponse
  86.     {
  87.         $return = array( 'price' => $product->getPrecio(), );
  88.         $response = new JsonResponse($return);
  89.         return $response;
  90.     }
  91. }