| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Entity\Product; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Repository\ProductRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\Persistence\ManagerRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Routing\Attribute\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | final class ProductController extends AbstractController | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     #[Route('/product', name: 'app_product')] | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public function index(): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         return $this->render('product/index.html.twig', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             'controller_name' => 'ProductController', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     #[Route('/product/create', name: 'product_create', methods: ['POST'])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function createProduct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         ManagerRegistry $doctrine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $entityManager = $doctrine->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $product = new Product(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $product->setName('Keyboard_num_' . rand(1, 9)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $product->setValue(rand(100, 999)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         // tell Doctrine you want to (eventually) save the Product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         // (no queries yet) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $entityManager->persist($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // actually executes the queries (i.e. the INSERT query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $entityManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return new Response('Saved new product with id '.$product->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     #[Route('/product/show', name: 'product_show_all')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function showAllProduct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ProductRepository $productRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $products = $productRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         //return $this->json($products); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $response = $this->json($products); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $response->setEncodingOptions( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $response->getEncodingOptions() | JSON_PRETTY_PRINT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     #[Route('/product/show/{id}', name: 'product_by_id')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function showProductById( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ProductRepository $productRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $product = $productRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $this->json($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     #[Route('/product/delete/{id}', name: 'product_delete_by_id', methods: ['POST'])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function deleteProductById( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ManagerRegistry $doctrine, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $entityManager = $doctrine->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $product = $entityManager->getRepository(Product::class)->find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!$product) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             throw $this->createNotFoundException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 'No product found for id '.$id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $entityManager->remove($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $entityManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->redirectToRoute('product_show_all'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     #[Route('/product/update/{id}/{value}', name: 'product_update')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function updateProduct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ManagerRegistry $doctrine, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         int $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         int $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $entityManager = $doctrine->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $product = $entityManager->getRepository(Product::class)->find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (!$product) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             throw $this->createNotFoundException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 'No product found for id '.$id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $product->setValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $entityManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this->redirectToRoute('product_show_all'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     #[Route('/product/view', name: 'product_view_all')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function viewAllProduct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ProductRepository $productRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $products = $productRepository->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             'products' => $products | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $this->render('product/view.html.twig', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     #[Route('/product/view/{value}', name: 'product_view_minimum_value')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function viewProductWithMinimumValue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ProductRepository $productRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         int $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $products = $productRepository->findByMinimumValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             'products' => $products | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return $this->render('product/view.html.twig', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     #[Route('/product/show/min/{value}', name: 'product_by_min_value')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function showProductByMinimumValue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         ProductRepository $productRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         int $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $products = $productRepository->findByMinimumValue2($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $this->json($products); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  |