From dce2fbff42b4a85b0f583104bb29c99fc62179b1 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 4 Apr 2023 00:36:35 +0200 Subject: [PATCH] Allow ceating and editing snips --- src/Controller/SnipController.php | 46 +++++++++++++++++++++++++++++-- src/Entity/Snip.php | 5 ++++ src/Entity/User.php | 5 ++++ src/Form/SnipType.php | 7 +++-- src/Repository/SnipRepository.php | 29 ++----------------- templates/snip/edit.html.twig | 12 ++++++++ templates/snip/index.html.twig | 3 +- 7 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 templates/snip/edit.html.twig diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 1d23559..8471cdd 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -3,19 +3,28 @@ namespace App\Controller; use App\Entity\Snip; +use App\Form\SnipType; use App\Repository\SnipRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/snip', name: 'snip')] class SnipController extends AbstractController { + public function __construct( + private readonly SnipRepository $repository, + ) + { + } + #[Route('/', name: '_index')] - public function index(SnipRepository $repository): Response + public function index(): Response { return $this->render('snip/index.html.twig', [ - 'snips' => $repository->findAll(), + 'snips' => $this->repository->findAll(), ]); } @@ -26,4 +35,37 @@ class SnipController extends AbstractController 'snip' => $snip, ]); } + + #[Route('/edit/{snip}', name: '_edit')] + public function edit(Snip $snip, Request $request): Response + { + $form = $this->createForm(SnipType::class, $snip); + $form->add('Save', SubmitType::class); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $snip->setCreatedAtTodayNoSeconds() + ->setCreatedBy($this->getUser()); + $this->repository->save($snip); + + $this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip)); + + return $this->redirectToRoute('snip_single', [ + 'snip' => $snip->getId(), + ]); + } + + return $this->render('snip/edit.html.twig', [ + 'snip' => $snip, + 'form' => $form->createView(), + ]); + } + + #[Route('/new', name: '_new')] + public function new(Request $request): Response + { + $snip = new Snip(); + + return $this->edit($snip, $request); + } } \ No newline at end of file diff --git a/src/Entity/Snip.php b/src/Entity/Snip.php index b3d8f9c..82805a4 100644 --- a/src/Entity/Snip.php +++ b/src/Entity/Snip.php @@ -19,6 +19,11 @@ class Snip #[ORM\Column(length: 255)] private ?string $name = null; + public function __toString(): string + { + return $this->name ?? ''; + } + public function getId(): ?int { return $this->id; diff --git a/src/Entity/User.php b/src/Entity/User.php index 5a2b350..1d8feaf 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -34,6 +34,11 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(length: 255)] private ?string $email = null; + public function __toString(): string + { + return $this->name ?? ''; + } + public function getId(): ?int { return $this->id; diff --git a/src/Form/SnipType.php b/src/Form/SnipType.php index 10494dc..7deb35d 100644 --- a/src/Form/SnipType.php +++ b/src/Form/SnipType.php @@ -4,6 +4,7 @@ namespace App\Form; use App\Entity\Snip; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -13,8 +14,10 @@ class SnipType extends AbstractType { $builder ->add('name') - ->add('createdAt') - ->add('createdBy') + ->add('content', TextareaType::class, [ + 'attr' => ['rows' => 20], + 'mapped' => false, + ]) ; } diff --git a/src/Repository/SnipRepository.php b/src/Repository/SnipRepository.php index 6255abe..988b6e2 100644 --- a/src/Repository/SnipRepository.php +++ b/src/Repository/SnipRepository.php @@ -21,7 +21,7 @@ class SnipRepository extends ServiceEntityRepository parent::__construct($registry, Snip::class); } - public function save(Snip $entity, bool $flush = false): void + public function save(Snip $entity, bool $flush = true): void { $this->getEntityManager()->persist($entity); @@ -30,7 +30,7 @@ class SnipRepository extends ServiceEntityRepository } } - public function remove(Snip $entity, bool $flush = false): void + public function remove(Snip $entity, bool $flush = true): void { $this->getEntityManager()->remove($entity); @@ -38,29 +38,4 @@ class SnipRepository extends ServiceEntityRepository $this->getEntityManager()->flush(); } } - -// /** -// * @return Snip[] Returns an array of Snip objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('s.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } - -// public function findOneBySomeField($value): ?Snip -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } } diff --git a/templates/snip/edit.html.twig b/templates/snip/edit.html.twig new file mode 100644 index 0000000..a48ba11 --- /dev/null +++ b/templates/snip/edit.html.twig @@ -0,0 +1,12 @@ +{% extends 'base/base.html.twig' %} + +{% block body %} +

{{ snip }}

+
+
+
+ {{ form(form) }} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/snip/index.html.twig b/templates/snip/index.html.twig index ffbfb3c..d870a5c 100644 --- a/templates/snip/index.html.twig +++ b/templates/snip/index.html.twig @@ -7,10 +7,11 @@

Snippets

+ Create a new Snip
{% for snip in snips %} - + {{ snip }} {% endfor %}