diff --git a/src/Controller/WhishlistController.php b/src/Controller/WhishlistController.php new file mode 100644 index 0000000000000000000000000000000000000000..21237de6e38e3f599532390bd1ff41eb1b18f474 --- /dev/null +++ b/src/Controller/WhishlistController.php @@ -0,0 +1,81 @@ +<?php + +namespace App\Controller; + +use App\Entity\Wishlist; +use App\Form\WishlistType; +use App\Repository\WishlistRepository; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Attribute\Route; + +#[Route('/whishlist')] +final class WhishlistController extends AbstractController +{ + #[Route(name: 'app_whishlist_index', methods: ['GET'])] + public function index(WishlistRepository $wishlistRepository): Response + { + return $this->render('whishlist/index.html.twig', [ + 'wishlists' => $wishlistRepository->findAll(), + ]); + } + + #[Route('/new', name: 'app_whishlist_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $wishlist = new Wishlist(); + $form = $this->createForm(WishlistType::class, $wishlist); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($wishlist); + $entityManager->flush(); + + return $this->redirectToRoute('app_whishlist_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('whishlist/new.html.twig', [ + 'wishlist' => $wishlist, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: 'app_whishlist_show', methods: ['GET'])] + public function show(Wishlist $wishlist): Response + { + return $this->render('whishlist/show.html.twig', [ + 'wishlist' => $wishlist, + ]); + } + + #[Route('/{id}/edit', name: 'app_whishlist_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(WishlistType::class, $wishlist); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_whishlist_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('whishlist/edit.html.twig', [ + 'wishlist' => $wishlist, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: 'app_whishlist_delete', methods: ['POST'])] + public function delete(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$wishlist->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($wishlist); + $entityManager->flush(); + } + + return $this->redirectToRoute('app_whishlist_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Form/WishlistType.php b/src/Form/WishlistType.php new file mode 100644 index 0000000000000000000000000000000000000000..645c6caad33a792ba181e08b8d04e73a8ec8c542 --- /dev/null +++ b/src/Form/WishlistType.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Form; + +use App\Entity\Wishlist; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class WishlistType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('name') + ->add('deadline', null, [ + 'widget' => 'single_text', + ]) + ->add('isDisabled') + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Wishlist::class, + ]); + } +} diff --git a/templates/whishlist/_delete_form.html.twig b/templates/whishlist/_delete_form.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..f5360a00849311f21e0093af86cbfad9f4bfff9a --- /dev/null +++ b/templates/whishlist/_delete_form.html.twig @@ -0,0 +1,4 @@ +<form method="post" action="{{ path('app_whishlist_delete', {'id': wishlist.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');"> + <input type="hidden" name="_token" value="{{ csrf_token('delete' ~ wishlist.id) }}"> + <button class="btn">Delete</button> +</form> diff --git a/templates/whishlist/_form.html.twig b/templates/whishlist/_form.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..bf20b98fb01ed38c82b670ff3fe5d7e207e80f16 --- /dev/null +++ b/templates/whishlist/_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/whishlist/edit.html.twig b/templates/whishlist/edit.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..9544c2b23cb7db38587d4b626db9410c06bb43df --- /dev/null +++ b/templates/whishlist/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Wishlist{% endblock %} + +{% block body %} + <h1>Edit Wishlist</h1> + + {{ include('whishlist/_form.html.twig', {'button_label': 'Update'}) }} + + <a href="{{ path('app_whishlist_index') }}">back to list</a> + + {{ include('whishlist/_delete_form.html.twig') }} +{% endblock %} diff --git a/templates/whishlist/index.html.twig b/templates/whishlist/index.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..60cac94a7ddcae14f1ee92ede25311b067951021 --- /dev/null +++ b/templates/whishlist/index.html.twig @@ -0,0 +1,39 @@ +{% extends 'base.html.twig' %} + +{% block title %}Wishlist index{% endblock %} + +{% block body %} + <h1>Wishlist index</h1> + + <table class="table"> + <thead> + <tr> + <th>Id</th> + <th>Name</th> + <th>Deadline</th> + <th>IsDisabled</th> + <th>actions</th> + </tr> + </thead> + <tbody> + {% for wishlist in wishlists %} + <tr> + <td>{{ wishlist.id }}</td> + <td>{{ wishlist.name }}</td> + <td>{{ wishlist.deadline ? wishlist.deadline|date('Y-m-d H:i:s') : '' }}</td> + <td>{{ wishlist.isDisabled ? 'Yes' : 'No' }}</td> + <td> + <a href="{{ path('app_whishlist_show', {'id': wishlist.id}) }}">show</a> + <a href="{{ path('app_whishlist_edit', {'id': wishlist.id}) }}">edit</a> + </td> + </tr> + {% else %} + <tr> + <td colspan="5">no records found</td> + </tr> + {% endfor %} + </tbody> + </table> + + <a href="{{ path('app_whishlist_new') }}">Create new</a> +{% endblock %} diff --git a/templates/whishlist/new.html.twig b/templates/whishlist/new.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..3bbb2c1f5c2d1402b504d911fe76214ed28a7fc2 --- /dev/null +++ b/templates/whishlist/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Wishlist{% endblock %} + +{% block body %} + <h1>Create new Wishlist</h1> + + {{ include('whishlist/_form.html.twig') }} + + <a href="{{ path('app_whishlist_index') }}">back to list</a> +{% endblock %} diff --git a/templates/whishlist/show.html.twig b/templates/whishlist/show.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..3d74ac9f07c6d0a58451eead09297463bc44e376 --- /dev/null +++ b/templates/whishlist/show.html.twig @@ -0,0 +1,34 @@ +{% extends 'base.html.twig' %} + +{% block title %}Wishlist{% endblock %} + +{% block body %} + <h1>Wishlist</h1> + + <table class="table"> + <tbody> + <tr> + <th>Id</th> + <td>{{ wishlist.id }}</td> + </tr> + <tr> + <th>Name</th> + <td>{{ wishlist.name }}</td> + </tr> + <tr> + <th>Deadline</th> + <td>{{ wishlist.deadline ? wishlist.deadline|date('Y-m-d H:i:s') : '' }}</td> + </tr> + <tr> + <th>IsDisabled</th> + <td>{{ wishlist.isDisabled ? 'Yes' : 'No' }}</td> + </tr> + </tbody> + </table> + + <a href="{{ path('app_whishlist_index') }}">back to list</a> + + <a href="{{ path('app_whishlist_edit', {'id': wishlist.id}) }}">edit</a> + + {{ include('whishlist/_delete_form.html.twig') }} +{% endblock %}