forked from SymfonyCasts/symfony-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal-make-crud-product.diff
304 lines (304 loc) · 9.17 KB
/
modal-make-crud-product.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
diff --git a/src/Controller/ProductAdminController.php b/src/Controller/ProductAdminController.php
new file mode 100644
index 0000000..174aa73
--- /dev/null
+++ b/src/Controller/ProductAdminController.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace App\Controller;
+
+use App\Entity\Product;
+use App\Form\ProductType;
+use App\Repository\ProductRepository;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Annotation\Route;
+
+/**
+ * @Route("/product/admin")
+ */
+class ProductAdminController extends AbstractController
+{
+ /**
+ * @Route("/", name="product_admin_index", methods={"GET"})
+ */
+ public function index(ProductRepository $productRepository): Response
+ {
+ return $this->render('product_admin/index.html.twig', [
+ 'products' => $productRepository->findAll(),
+ ]);
+ }
+
+ /**
+ * @Route("/new", name="product_admin_new", methods={"GET","POST"})
+ */
+ public function new(Request $request): Response
+ {
+ $product = new Product();
+ $form = $this->createForm(ProductType::class, $product);
+ $form->handleRequest($request);
+
+ if ($form->isSubmitted() && $form->isValid()) {
+ $entityManager = $this->getDoctrine()->getManager();
+ $entityManager->persist($product);
+ $entityManager->flush();
+
+ return $this->redirectToRoute('product_admin_index');
+ }
+
+ return $this->render('product_admin/new.html.twig', [
+ 'product' => $product,
+ 'form' => $form->createView(),
+ ]);
+ }
+
+ /**
+ * @Route("/{id}", name="product_admin_show", methods={"GET"})
+ */
+ public function show(Product $product): Response
+ {
+ return $this->render('product_admin/show.html.twig', [
+ 'product' => $product,
+ ]);
+ }
+
+ /**
+ * @Route("/{id}/edit", name="product_admin_edit", methods={"GET","POST"})
+ */
+ public function edit(Request $request, Product $product): Response
+ {
+ $form = $this->createForm(ProductType::class, $product);
+ $form->handleRequest($request);
+
+ if ($form->isSubmitted() && $form->isValid()) {
+ $this->getDoctrine()->getManager()->flush();
+
+ return $this->redirectToRoute('product_admin_index');
+ }
+
+ return $this->render('product_admin/edit.html.twig', [
+ 'product' => $product,
+ 'form' => $form->createView(),
+ ]);
+ }
+
+ /**
+ * @Route("/{id}", name="product_admin_delete", methods={"POST"})
+ */
+ public function delete(Request $request, Product $product): Response
+ {
+ if ($this->isCsrfTokenValid('delete'.$product->getId(), $request->request->get('_token'))) {
+ $entityManager = $this->getDoctrine()->getManager();
+ $entityManager->remove($product);
+ $entityManager->flush();
+ }
+
+ return $this->redirectToRoute('product_admin_index');
+ }
+}
diff --git a/src/Form/ProductType.php b/src/Form/ProductType.php
new file mode 100644
index 0000000..b0f2b51
--- /dev/null
+++ b/src/Form/ProductType.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Form;
+
+use App\Entity\Product;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class ProductType extends AbstractType
+{
+ public function buildForm(FormBuilderInterface $builder, array $options)
+ {
+ $builder
+ ->add('name')
+ ->add('description')
+ ->add('brand')
+ ->add('weight')
+ ->add('price')
+ ->add('stockQuantity')
+ ->add('imageFilename')
+ ->add('category')
+ ->add('colors')
+ ;
+ }
+
+ public function configureOptions(OptionsResolver $resolver)
+ {
+ $resolver->setDefaults([
+ 'data_class' => Product::class,
+ ]);
+ }
+}
diff --git a/templates/product_admin/_delete_form.html.twig b/templates/product_admin/_delete_form.html.twig
new file mode 100644
index 0000000..f1344bd
--- /dev/null
+++ b/templates/product_admin/_delete_form.html.twig
@@ -0,0 +1,4 @@
+<form method="post" action="{{ path('product_admin_delete', {'id': product.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
+ <input type="hidden" name="_token" value="{{ csrf_token('delete' ~ product.id) }}">
+ <button class="btn">Delete</button>
+</form>
diff --git a/templates/product_admin/_form.html.twig b/templates/product_admin/_form.html.twig
new file mode 100644
index 0000000..bf20b98
--- /dev/null
+++ b/templates/product_admin/_form.html.twig
@@ -0,0 +1,4 @@
+{{ form_start(form) }}
+ {{ form_widget(form) }}
+ <button class="btn">{{ button_label|default('Save') }}</button>
+{{ form_end(form) }}
diff --git a/templates/product_admin/edit.html.twig b/templates/product_admin/edit.html.twig
new file mode 100644
index 0000000..83b8757
--- /dev/null
+++ b/templates/product_admin/edit.html.twig
@@ -0,0 +1,13 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}Edit Product{% endblock %}
+
+{% block body %}
+ <h1>Edit Product</h1>
+
+ {{ include('product_admin/_form.html.twig', {'button_label': 'Update'}) }}
+
+ <a href="{{ path('product_admin_index') }}">back to list</a>
+
+ {{ include('product_admin/_delete_form.html.twig') }}
+{% endblock %}
diff --git a/templates/product_admin/index.html.twig b/templates/product_admin/index.html.twig
new file mode 100644
index 0000000..87a7a10
--- /dev/null
+++ b/templates/product_admin/index.html.twig
@@ -0,0 +1,47 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}Product index{% endblock %}
+
+{% block body %}
+ <h1>Product index</h1>
+
+ <table class="table">
+ <thead>
+ <tr>
+ <th>Id</th>
+ <th>Name</th>
+ <th>Description</th>
+ <th>Brand</th>
+ <th>Weight</th>
+ <th>Price</th>
+ <th>StockQuantity</th>
+ <th>ImageFilename</th>
+ <th>actions</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for product in products %}
+ <tr>
+ <td>{{ product.id }}</td>
+ <td>{{ product.name }}</td>
+ <td>{{ product.description }}</td>
+ <td>{{ product.brand }}</td>
+ <td>{{ product.weight }}</td>
+ <td>{{ product.price }}</td>
+ <td>{{ product.stockQuantity }}</td>
+ <td>{{ product.imageFilename }}</td>
+ <td>
+ <a href="{{ path('product_admin_show', {'id': product.id}) }}">show</a>
+ <a href="{{ path('product_admin_edit', {'id': product.id}) }}">edit</a>
+ </td>
+ </tr>
+ {% else %}
+ <tr>
+ <td colspan="9">no records found</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+
+ <a href="{{ path('product_admin_new') }}">Create new</a>
+{% endblock %}
diff --git a/templates/product_admin/new.html.twig b/templates/product_admin/new.html.twig
new file mode 100644
index 0000000..1560ef7
--- /dev/null
+++ b/templates/product_admin/new.html.twig
@@ -0,0 +1,11 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}New Product{% endblock %}
+
+{% block body %}
+ <h1>Create new Product</h1>
+
+ {{ include('product_admin/_form.html.twig') }}
+
+ <a href="{{ path('product_admin_index') }}">back to list</a>
+{% endblock %}
diff --git a/templates/product_admin/show.html.twig b/templates/product_admin/show.html.twig
new file mode 100644
index 0000000..4bb4073
--- /dev/null
+++ b/templates/product_admin/show.html.twig
@@ -0,0 +1,50 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}Product{% endblock %}
+
+{% block body %}
+ <h1>Product</h1>
+
+ <table class="table">
+ <tbody>
+ <tr>
+ <th>Id</th>
+ <td>{{ product.id }}</td>
+ </tr>
+ <tr>
+ <th>Name</th>
+ <td>{{ product.name }}</td>
+ </tr>
+ <tr>
+ <th>Description</th>
+ <td>{{ product.description }}</td>
+ </tr>
+ <tr>
+ <th>Brand</th>
+ <td>{{ product.brand }}</td>
+ </tr>
+ <tr>
+ <th>Weight</th>
+ <td>{{ product.weight }}</td>
+ </tr>
+ <tr>
+ <th>Price</th>
+ <td>{{ product.price }}</td>
+ </tr>
+ <tr>
+ <th>StockQuantity</th>
+ <td>{{ product.stockQuantity }}</td>
+ </tr>
+ <tr>
+ <th>ImageFilename</th>
+ <td>{{ product.imageFilename }}</td>
+ </tr>
+ </tbody>
+ </table>
+
+ <a href="{{ path('product_admin_index') }}">back to list</a>
+
+ <a href="{{ path('product_admin_edit', {'id': product.id}) }}">edit</a>
+
+ {{ include('product_admin/_delete_form.html.twig') }}
+{% endblock %}