From 0fdd876bc4b9214019f025e471f291117cd43da1 Mon Sep 17 00:00:00 2001 From: Tchama Date: Thu, 18 Feb 2021 20:13:14 +0100 Subject: [PATCH] adding Beer entity, migration and crud --- app/migrations/Version20210218191150.php | 34 ++++++++ app/src/Controller/BeerController.php | 94 +++++++++++++++++++++++ app/src/Entity/Beer.php | 58 ++++++++++++++ app/src/Form/BeerType.php | 26 +++++++ app/src/Repository/BeerRepository.php | 50 ++++++++++++ app/templates/beer/_delete_form.html.twig | 5 ++ app/templates/beer/_form.html.twig | 4 + app/templates/beer/edit.html.twig | 13 ++++ app/templates/beer/index.html.twig | 37 +++++++++ app/templates/beer/new.html.twig | 11 +++ app/templates/beer/show.html.twig | 30 ++++++++ 11 files changed, 362 insertions(+) create mode 100644 app/migrations/Version20210218191150.php create mode 100644 app/src/Controller/BeerController.php create mode 100644 app/src/Entity/Beer.php create mode 100644 app/src/Form/BeerType.php create mode 100644 app/src/Repository/BeerRepository.php create mode 100644 app/templates/beer/_delete_form.html.twig create mode 100644 app/templates/beer/_form.html.twig create mode 100644 app/templates/beer/edit.html.twig create mode 100644 app/templates/beer/index.html.twig create mode 100644 app/templates/beer/new.html.twig create mode 100644 app/templates/beer/show.html.twig diff --git a/app/migrations/Version20210218191150.php b/app/migrations/Version20210218191150.php new file mode 100644 index 0000000..5cba1bc --- /dev/null +++ b/app/migrations/Version20210218191150.php @@ -0,0 +1,34 @@ +addSql('CREATE SEQUENCE beer_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE beer (id INT NOT NULL, name VARCHAR(255) NOT NULL, alcool DOUBLE PRECISION DEFAULT NULL, PRIMARY KEY(id))'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('DROP SEQUENCE beer_id_seq CASCADE'); + $this->addSql('DROP TABLE beer'); + } +} diff --git a/app/src/Controller/BeerController.php b/app/src/Controller/BeerController.php new file mode 100644 index 0000000..0b93c57 --- /dev/null +++ b/app/src/Controller/BeerController.php @@ -0,0 +1,94 @@ +render('beer/index.html.twig', [ + 'beers' => $beerRepository->findAll(), + ]); + } + + /** + * @Route("/new", name="beer_new", methods={"GET","POST"}) + */ + public function new(Request $request): Response + { + $beer = new Beer(); + $form = $this->createForm(BeerType::class, $beer); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($beer); + $entityManager->flush(); + + return $this->redirectToRoute('beer_index'); + } + + return $this->render('beer/new.html.twig', [ + 'beer' => $beer, + 'form' => $form->createView(), + ]); + } + + /** + * @Route("/{id}", name="beer_show", methods={"GET"}) + */ + public function show(Beer $beer): Response + { + return $this->render('beer/show.html.twig', [ + 'beer' => $beer, + ]); + } + + /** + * @Route("/{id}/edit", name="beer_edit", methods={"GET","POST"}) + */ + public function edit(Request $request, Beer $beer): Response + { + $form = $this->createForm(BeerType::class, $beer); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('beer_index'); + } + + return $this->render('beer/edit.html.twig', [ + 'beer' => $beer, + 'form' => $form->createView(), + ]); + } + + /** + * @Route("/{id}", name="beer_delete", methods={"DELETE"}) + */ + public function delete(Request $request, Beer $beer): Response + { + if ($this->isCsrfTokenValid('delete'.$beer->getId(), $request->request->get('_token'))) { + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->remove($beer); + $entityManager->flush(); + } + + return $this->redirectToRoute('beer_index'); + } +} diff --git a/app/src/Entity/Beer.php b/app/src/Entity/Beer.php new file mode 100644 index 0000000..2515ee1 --- /dev/null +++ b/app/src/Entity/Beer.php @@ -0,0 +1,58 @@ +id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getAlcool(): ?float + { + return $this->alcool; + } + + public function setAlcool(?float $alcool): self + { + $this->alcool = $alcool; + + return $this; + } +} diff --git a/app/src/Form/BeerType.php b/app/src/Form/BeerType.php new file mode 100644 index 0000000..b6b8d5b --- /dev/null +++ b/app/src/Form/BeerType.php @@ -0,0 +1,26 @@ +add('name') + ->add('alcool') + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Beer::class, + ]); + } +} diff --git a/app/src/Repository/BeerRepository.php b/app/src/Repository/BeerRepository.php new file mode 100644 index 0000000..23f7b15 --- /dev/null +++ b/app/src/Repository/BeerRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('b') + ->andWhere('b.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('b.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Beer + { + return $this->createQueryBuilder('b') + ->andWhere('b.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/app/templates/beer/_delete_form.html.twig b/app/templates/beer/_delete_form.html.twig new file mode 100644 index 0000000..6da3c31 --- /dev/null +++ b/app/templates/beer/_delete_form.html.twig @@ -0,0 +1,5 @@ +
+ + + +
diff --git a/app/templates/beer/_form.html.twig b/app/templates/beer/_form.html.twig new file mode 100644 index 0000000..bf20b98 --- /dev/null +++ b/app/templates/beer/_form.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} + {{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/app/templates/beer/edit.html.twig b/app/templates/beer/edit.html.twig new file mode 100644 index 0000000..36dd1bc --- /dev/null +++ b/app/templates/beer/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Beer{% endblock %} + +{% block body %} +

Edit Beer

+ + {{ include('beer/_form.html.twig', {'button_label': 'Update'}) }} + + back to list + + {{ include('beer/_delete_form.html.twig') }} +{% endblock %} diff --git a/app/templates/beer/index.html.twig b/app/templates/beer/index.html.twig new file mode 100644 index 0000000..b32782b --- /dev/null +++ b/app/templates/beer/index.html.twig @@ -0,0 +1,37 @@ +{% extends 'base.html.twig' %} + +{% block title %}Beer index{% endblock %} + +{% block body %} +

Beer index

+ + + + + + + + + + + + {% for beer in beers %} + + + + + + + {% else %} + + + + {% endfor %} + +
IdNameAlcoolactions
{{ beer.id }}{{ beer.name }}{{ beer.alcool }} + show + edit +
no records found
+ + Create new +{% endblock %} diff --git a/app/templates/beer/new.html.twig b/app/templates/beer/new.html.twig new file mode 100644 index 0000000..9c37bd5 --- /dev/null +++ b/app/templates/beer/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Beer{% endblock %} + +{% block body %} +

Create new Beer

+ + {{ include('beer/_form.html.twig') }} + + back to list +{% endblock %} diff --git a/app/templates/beer/show.html.twig b/app/templates/beer/show.html.twig new file mode 100644 index 0000000..b8c053d --- /dev/null +++ b/app/templates/beer/show.html.twig @@ -0,0 +1,30 @@ +{% extends 'base.html.twig' %} + +{% block title %}Beer{% endblock %} + +{% block body %} +

Beer

+ + + + + + + + + + + + + + + + +
Id{{ beer.id }}
Name{{ beer.name }}
Alcool{{ beer.alcool }}
+ + back to list + + edit + + {{ include('beer/_delete_form.html.twig') }} +{% endblock %}