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