From d5ef71f734c77a954a67ece771a4f5d0bc61b4b6 Mon Sep 17 00:00:00 2001 From: Kayn Ty Date: Thu, 5 Apr 2018 17:09:06 +0200 Subject: [PATCH] cruds for products, categories, festivals --- app/Resources/views/product/index.html.twig | 112 +++++---------- app/config/routing.yml | 12 ++ .../Controller/DefaultController.php | 5 +- .../Controller/FestivalController.php | 136 ++++++++++++++++++ .../Controller/ProductCategoryController.php | 136 ++++++++++++++++++ .../Controller/ProductController.php | 136 ++++++++++++++++++ src/AppBundle/Entity/Product.php | 20 +++ src/AppBundle/Form/FestivalType.php | 36 +++++ src/AppBundle/Form/ProductCategoryType.php | 36 +++++ src/AppBundle/Form/ProductType.php | 36 +++++ .../Controller/FestivalControllerTest.php | 55 +++++++ .../ProductCategoryControllerTest.php | 55 +++++++ .../Controller/ProductControllerTest.php | 55 +++++++ 13 files changed, 755 insertions(+), 75 deletions(-) create mode 100644 src/AppBundle/Controller/FestivalController.php create mode 100644 src/AppBundle/Controller/ProductCategoryController.php create mode 100644 src/AppBundle/Controller/ProductController.php create mode 100644 src/AppBundle/Form/FestivalType.php create mode 100644 src/AppBundle/Form/ProductCategoryType.php create mode 100644 src/AppBundle/Form/ProductType.php create mode 100644 src/AppBundle/Tests/Controller/FestivalControllerTest.php create mode 100644 src/AppBundle/Tests/Controller/ProductCategoryControllerTest.php create mode 100644 src/AppBundle/Tests/Controller/ProductControllerTest.php diff --git a/app/Resources/views/product/index.html.twig b/app/Resources/views/product/index.html.twig index e26dba07..46ed6c06 100755 --- a/app/Resources/views/product/index.html.twig +++ b/app/Resources/views/product/index.html.twig @@ -1,79 +1,45 @@ {% extends 'base.html.twig' %} -{% block title %}Hello !{% endblock %} - {% block body %} -
-
-

Caisse de convention Qzine

-
+

Products list

-
-
-

- Catégories -

- {% for c in categories %} -

{{ c.name }}

-
- {% for p in c.products %} -
+ + + + + + + + + + + + + {% for product in products %} + + + + + + + + + {% endfor %} + +
IdNameImagePriceCommentActions
{{ product.id }}{{ product.name }}{{ product.image }}{{ product.price }}{{ product.comment }} + +
- - -
- {% endfor %} -
- - {% endfor %} -
-
-
-
- évènement: - -
-
-

Commentaire

- - -
-

Prix

- - - € - -
-
-

Historique

-
    -
  • - 12€ - commentaire - -
  • -
-
-
-
- - -
- -
-
+ {% endblock %} diff --git a/app/config/routing.yml b/app/config/routing.yml index a8992110..0b3bcb6d 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -1,3 +1,15 @@ +app_festival: + resource: "@AppBundle/Controller/FestivalController.php" + type: annotation + +app_product_category: + resource: "@AppBundle/Controller/ProductCategoryController.php" + type: annotation + +app_product: + resource: "@AppBundle/Controller/ProductController.php" + type: annotation + app_sell_record: resource: "@AppBundle/Controller/SellRecordController.php" type: annotation diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index 0b81d66f..7d359b51 100644 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -118,11 +118,12 @@ class DefaultController extends Controller { * @return JsonResponse */ public function addSellingAction( Request $request ) { - var_dump( $request->request ); +// var_dump( $request->request ); +// die(); // link selling record with user, festival // setup dates // persist all // fetch back history of selling - return new JsonResponse( [ "message" => "ok" ] ); + return new JsonResponse( [ "message" => "ok", "dump" => $request->request ] ); } } diff --git a/src/AppBundle/Controller/FestivalController.php b/src/AppBundle/Controller/FestivalController.php new file mode 100644 index 00000000..1c228b4b --- /dev/null +++ b/src/AppBundle/Controller/FestivalController.php @@ -0,0 +1,136 @@ +getDoctrine()->getManager(); + + $festivals = $em->getRepository('AppBundle:Festival')->findAll(); + + return $this->render('festival/index.html.twig', array( + 'festivals' => $festivals, + )); + } + + /** + * Creates a new festival entity. + * + * @Route("/new", name="festival_new") + * @Method({"GET", "POST"}) + */ + public function newAction(Request $request) + { + $festival = new Festival(); + $form = $this->createForm('AppBundle\Form\FestivalType', $festival); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($festival); + $em->flush(); + + return $this->redirectToRoute('festival_show', array('id' => $festival->getId())); + } + + return $this->render('festival/new.html.twig', array( + 'festival' => $festival, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a festival entity. + * + * @Route("/{id}", name="festival_show") + * @Method("GET") + */ + public function showAction(Festival $festival) + { + $deleteForm = $this->createDeleteForm($festival); + + return $this->render('festival/show.html.twig', array( + 'festival' => $festival, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing festival entity. + * + * @Route("/{id}/edit", name="festival_edit") + * @Method({"GET", "POST"}) + */ + public function editAction(Request $request, Festival $festival) + { + $deleteForm = $this->createDeleteForm($festival); + $editForm = $this->createForm('AppBundle\Form\FestivalType', $festival); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('festival_edit', array('id' => $festival->getId())); + } + + return $this->render('festival/edit.html.twig', array( + 'festival' => $festival, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Deletes a festival entity. + * + * @Route("/{id}", name="festival_delete") + * @Method("DELETE") + */ + public function deleteAction(Request $request, Festival $festival) + { + $form = $this->createDeleteForm($festival); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->remove($festival); + $em->flush(); + } + + return $this->redirectToRoute('festival_index'); + } + + /** + * Creates a form to delete a festival entity. + * + * @param Festival $festival The festival entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createDeleteForm(Festival $festival) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('festival_delete', array('id' => $festival->getId()))) + ->setMethod('DELETE') + ->getForm() + ; + } +} diff --git a/src/AppBundle/Controller/ProductCategoryController.php b/src/AppBundle/Controller/ProductCategoryController.php new file mode 100644 index 00000000..774da07b --- /dev/null +++ b/src/AppBundle/Controller/ProductCategoryController.php @@ -0,0 +1,136 @@ +getDoctrine()->getManager(); + + $productCategories = $em->getRepository('AppBundle:ProductCategory')->findAll(); + + return $this->render('productcategory/index.html.twig', array( + 'productCategories' => $productCategories, + )); + } + + /** + * Creates a new productCategory entity. + * + * @Route("/new", name="productcategory_new") + * @Method({"GET", "POST"}) + */ + public function newAction(Request $request) + { + $productCategory = new Productcategory(); + $form = $this->createForm('AppBundle\Form\ProductCategoryType', $productCategory); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($productCategory); + $em->flush(); + + return $this->redirectToRoute('productcategory_show', array('id' => $productCategory->getId())); + } + + return $this->render('productcategory/new.html.twig', array( + 'productCategory' => $productCategory, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a productCategory entity. + * + * @Route("/{id}", name="productcategory_show") + * @Method("GET") + */ + public function showAction(ProductCategory $productCategory) + { + $deleteForm = $this->createDeleteForm($productCategory); + + return $this->render('productcategory/show.html.twig', array( + 'productCategory' => $productCategory, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing productCategory entity. + * + * @Route("/{id}/edit", name="productcategory_edit") + * @Method({"GET", "POST"}) + */ + public function editAction(Request $request, ProductCategory $productCategory) + { + $deleteForm = $this->createDeleteForm($productCategory); + $editForm = $this->createForm('AppBundle\Form\ProductCategoryType', $productCategory); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('productcategory_edit', array('id' => $productCategory->getId())); + } + + return $this->render('productcategory/edit.html.twig', array( + 'productCategory' => $productCategory, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Deletes a productCategory entity. + * + * @Route("/{id}", name="productcategory_delete") + * @Method("DELETE") + */ + public function deleteAction(Request $request, ProductCategory $productCategory) + { + $form = $this->createDeleteForm($productCategory); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->remove($productCategory); + $em->flush(); + } + + return $this->redirectToRoute('productcategory_index'); + } + + /** + * Creates a form to delete a productCategory entity. + * + * @param ProductCategory $productCategory The productCategory entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createDeleteForm(ProductCategory $productCategory) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('productcategory_delete', array('id' => $productCategory->getId()))) + ->setMethod('DELETE') + ->getForm() + ; + } +} diff --git a/src/AppBundle/Controller/ProductController.php b/src/AppBundle/Controller/ProductController.php new file mode 100644 index 00000000..9714c8d3 --- /dev/null +++ b/src/AppBundle/Controller/ProductController.php @@ -0,0 +1,136 @@ +getDoctrine()->getManager(); + + $products = $em->getRepository('AppBundle:Product')->findAll(); + + return $this->render('product/index.html.twig', array( + 'products' => $products, + )); + } + + /** + * Creates a new product entity. + * + * @Route("/new", name="product_new") + * @Method({"GET", "POST"}) + */ + public function newAction(Request $request) + { + $product = new Product(); + $form = $this->createForm('AppBundle\Form\ProductType', $product); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($product); + $em->flush(); + + return $this->redirectToRoute('product_show', array('id' => $product->getId())); + } + + return $this->render('product/new.html.twig', array( + 'product' => $product, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a product entity. + * + * @Route("/{id}", name="product_show") + * @Method("GET") + */ + public function showAction(Product $product) + { + $deleteForm = $this->createDeleteForm($product); + + return $this->render('product/show.html.twig', array( + 'product' => $product, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing product entity. + * + * @Route("/{id}/edit", name="product_edit") + * @Method({"GET", "POST"}) + */ + public function editAction(Request $request, Product $product) + { + $deleteForm = $this->createDeleteForm($product); + $editForm = $this->createForm('AppBundle\Form\ProductType', $product); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('product_edit', array('id' => $product->getId())); + } + + return $this->render('product/edit.html.twig', array( + 'product' => $product, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Deletes a product entity. + * + * @Route("/{id}", name="product_delete") + * @Method("DELETE") + */ + public function deleteAction(Request $request, Product $product) + { + $form = $this->createDeleteForm($product); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->remove($product); + $em->flush(); + } + + return $this->redirectToRoute('product_index'); + } + + /** + * Creates a form to delete a product entity. + * + * @param Product $product The product entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createDeleteForm(Product $product) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('product_delete', array('id' => $product->getId()))) + ->setMethod('DELETE') + ->getForm() + ; + } +} diff --git a/src/AppBundle/Entity/Product.php b/src/AppBundle/Entity/Product.php index 0ccc8413..652ea05e 100644 --- a/src/AppBundle/Entity/Product.php +++ b/src/AppBundle/Entity/Product.php @@ -22,6 +22,12 @@ class Product { */ private $name; + + /** + * @ORM\Column(type="string", length=256) + */ + private $image; + /** * @ORM\ManyToOne(targetEntity="ProductCategory", inversedBy="products") */ @@ -34,6 +40,20 @@ class Product { use Sellable; use Commentable; + /** + * @return mixed + */ + public function getImage() { + return $this->image; + } + + /** + * @param mixed $image + */ + public function setImage( $image ) { + $this->image = $image; + } + /** * @return mixed */ diff --git a/src/AppBundle/Form/FestivalType.php b/src/AppBundle/Form/FestivalType.php new file mode 100644 index 00000000..c9cf2a7a --- /dev/null +++ b/src/AppBundle/Form/FestivalType.php @@ -0,0 +1,36 @@ +add('name')->add('dateCreation'); + }/** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'AppBundle\Entity\Festival' + )); + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'appbundle_festival'; + } + + +} diff --git a/src/AppBundle/Form/ProductCategoryType.php b/src/AppBundle/Form/ProductCategoryType.php new file mode 100644 index 00000000..212f8b79 --- /dev/null +++ b/src/AppBundle/Form/ProductCategoryType.php @@ -0,0 +1,36 @@ +add('name')->add('users'); + }/** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'AppBundle\Entity\ProductCategory' + )); + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'appbundle_productcategory'; + } + + +} diff --git a/src/AppBundle/Form/ProductType.php b/src/AppBundle/Form/ProductType.php new file mode 100644 index 00000000..defdeff4 --- /dev/null +++ b/src/AppBundle/Form/ProductType.php @@ -0,0 +1,36 @@ +add('name')->add('image')->add('price')->add('comment')->add('category')->add('user'); + }/** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'AppBundle\Entity\Product' + )); + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'appbundle_product'; + } + + +} diff --git a/src/AppBundle/Tests/Controller/FestivalControllerTest.php b/src/AppBundle/Tests/Controller/FestivalControllerTest.php new file mode 100644 index 00000000..e404453a --- /dev/null +++ b/src/AppBundle/Tests/Controller/FestivalControllerTest.php @@ -0,0 +1,55 @@ +request('GET', '/festival/'); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /festival/"); + $crawler = $client->click($crawler->selectLink('Create a new entry')->link()); + + // Fill in the form and submit it + $form = $crawler->selectButton('Create')->form(array( + 'appbundle_festival[field_name]' => 'Test', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check data in the show view + $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")'); + + // Edit the entity + $crawler = $client->click($crawler->selectLink('Edit')->link()); + + $form = $crawler->selectButton('Update')->form(array( + 'appbundle_festival[field_name]' => 'Foo', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check the element contains an attribute with value equals "Foo" + $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); + + // Delete the entity + $client->submit($crawler->selectButton('Delete')->form()); + $crawler = $client->followRedirect(); + + // Check the entity has been delete on the list + $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent()); + } + + */ +} diff --git a/src/AppBundle/Tests/Controller/ProductCategoryControllerTest.php b/src/AppBundle/Tests/Controller/ProductCategoryControllerTest.php new file mode 100644 index 00000000..3217defc --- /dev/null +++ b/src/AppBundle/Tests/Controller/ProductCategoryControllerTest.php @@ -0,0 +1,55 @@ +request('GET', '/productcategory/'); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /productcategory/"); + $crawler = $client->click($crawler->selectLink('Create a new entry')->link()); + + // Fill in the form and submit it + $form = $crawler->selectButton('Create')->form(array( + 'appbundle_productcategory[field_name]' => 'Test', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check data in the show view + $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")'); + + // Edit the entity + $crawler = $client->click($crawler->selectLink('Edit')->link()); + + $form = $crawler->selectButton('Update')->form(array( + 'appbundle_productcategory[field_name]' => 'Foo', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check the element contains an attribute with value equals "Foo" + $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); + + // Delete the entity + $client->submit($crawler->selectButton('Delete')->form()); + $crawler = $client->followRedirect(); + + // Check the entity has been delete on the list + $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent()); + } + + */ +} diff --git a/src/AppBundle/Tests/Controller/ProductControllerTest.php b/src/AppBundle/Tests/Controller/ProductControllerTest.php new file mode 100644 index 00000000..b75120ee --- /dev/null +++ b/src/AppBundle/Tests/Controller/ProductControllerTest.php @@ -0,0 +1,55 @@ +request('GET', '/product/'); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /product/"); + $crawler = $client->click($crawler->selectLink('Create a new entry')->link()); + + // Fill in the form and submit it + $form = $crawler->selectButton('Create')->form(array( + 'appbundle_product[field_name]' => 'Test', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check data in the show view + $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")'); + + // Edit the entity + $crawler = $client->click($crawler->selectLink('Edit')->link()); + + $form = $crawler->selectButton('Update')->form(array( + 'appbundle_product[field_name]' => 'Foo', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check the element contains an attribute with value equals "Foo" + $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); + + // Delete the entity + $client->submit($crawler->selectButton('Delete')->form()); + $crawler = $client->followRedirect(); + + // Check the entity has been delete on the list + $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent()); + } + + */ +}