From 8eabb71db46792e1a726bd52be03be094ee3f910 Mon Sep 17 00:00:00 2001 From: colesm Date: Fri, 4 Apr 2025 08:50:54 +0200 Subject: [PATCH] Incident commit --- composer.lock | 12 +++---- src/Controller/IncidentController.php | 49 ++++++++++++++++++++++++--- src/Entity/Incident.php | 5 +-- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/composer.lock b/composer.lock index 465f63c..2c74e28 100644 --- a/composer.lock +++ b/composer.lock @@ -6671,16 +6671,16 @@ }, { "name": "symfony/twig-bundle", - "version": "v7.1.1", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/twig-bundle.git", - "reference": "d48c2f08c2f315e749f0e18fc4945b7be8afe1e5" + "reference": "af902314a71fb412ae412094f7e1d7e49594507b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/twig-bundle/zipball/d48c2f08c2f315e749f0e18fc4945b7be8afe1e5", - "reference": "d48c2f08c2f315e749f0e18fc4945b7be8afe1e5", + "url": "https://api.github.com/repos/symfony/twig-bundle/zipball/af902314a71fb412ae412094f7e1d7e49594507b", + "reference": "af902314a71fb412ae412094f7e1d7e49594507b", "shasum": "" }, "require": { @@ -6735,7 +6735,7 @@ "description": "Provides a tight integration of Twig into the Symfony full-stack framework", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/twig-bundle/tree/v7.1.1" + "source": "https://github.com/symfony/twig-bundle/tree/v7.1.6" }, "funding": [ { @@ -6751,7 +6751,7 @@ "type": "tidelift" } ], - "time": "2024-05-31T14:57:53+00:00" + "time": "2024-09-25T14:20:29+00:00" }, { "name": "symfony/type-info", diff --git a/src/Controller/IncidentController.php b/src/Controller/IncidentController.php index 534c9bc..4277597 100644 --- a/src/Controller/IncidentController.php +++ b/src/Controller/IncidentController.php @@ -2,17 +2,58 @@ namespace App\Controller; +use App\Entity\Incident; +use App\Form\IncidentType; +use App\Repository\IncidentRepository; +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; +use Symfony\Component\Routing\Annotation\Route; +#[Route('/incident', name: 'incident_')] class IncidentController extends AbstractController { - #[Route('/incident', name: 'app_incident')] - public function index(): Response + public function __construct( + private readonly EntityManagerInterface $entityManager + ) { + } + + #[Route('', name: 'index', methods: ['GET'])] + public function index(IncidentRepository $incidentRepository): Response { return $this->render('incident/index.html.twig', [ - 'controller_name' => 'IncidentController', + 'incidents' => $incidentRepository->findAll(), ]); } + + #[Route('/new', name: 'new', methods: ['GET', 'POST'])] + public function new(Request $request): Response + { + $incident = new Incident(); + $form = $this->createForm(IncidentType::class, $incident); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->entityManager->persist($incident); + $this->entityManager->flush(); + + return $this->redirectToRoute('incident_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('incident/new.html.twig', [ + 'form' => $form->createView(), + ]); + } + + #[Route('/{id}', name: 'delete', methods: ['POST'])] + public function delete(Request $request, Incident $incident): Response + { + if ($this->isCsrfTokenValid('delete'.$incident->getId(), $request->request->get('_token'))) { + $this->entityManager->remove($incident); + $this->entityManager->flush(); + } + + return $this->redirectToRoute('incident_index', [], Response::HTTP_SEE_OTHER); + } } diff --git a/src/Entity/Incident.php b/src/Entity/Incident.php index 4654c92..7d1a30e 100644 --- a/src/Entity/Incident.php +++ b/src/Entity/Incident.php @@ -2,11 +2,12 @@ namespace App\Entity; +use AllowDynamicProperties; use App\Repository\IncidentRepository; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -#[ORM\Entity(repositoryClass: IncidentRepository::class)] +#[AllowDynamicProperties] #[ORM\Entity(repositoryClass: IncidentRepository::class)] class Incident { #[ORM\Id] @@ -15,7 +16,7 @@ class Incident private ?int $id = null; #[ORM\Column(length: 255)] - private ?string $description = null; + public ?string $description = null; #[ORM\OneToMany(targetEntity: Employee::class, mappedBy: 'incident')] private ?Employee $employee;