From ea41c3cccc01690aeae75cb6b3c901d99334b60e Mon Sep 17 00:00:00 2001 From: pressatl Date: Fri, 20 Dec 2024 09:08:48 +0100 Subject: [PATCH 1/2] Finished --- src/Repository/MissionRepository.php | 46 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Repository/MissionRepository.php b/src/Repository/MissionRepository.php index 782a0a9..f80af84 100644 --- a/src/Repository/MissionRepository.php +++ b/src/Repository/MissionRepository.php @@ -16,28 +16,28 @@ class MissionRepository extends ServiceEntityRepository parent::__construct($registry, Mission::class); } - // /** - // * @return Mission[] Returns an array of Mission objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('m') - // ->andWhere('m.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('m.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } + /** + * @return Mission[] Returns an array of Mission objects + */ + public function findByExampleField($value): array + { + return $this->createQueryBuilder('m') + ->andWhere('m.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('m.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } - // public function findOneBySomeField($value): ?Mission - // { - // return $this->createQueryBuilder('m') - // ->andWhere('m.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } + public function findOneBySomeField($value): ?Mission + { + return $this->createQueryBuilder('m') + ->andWhere('m.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } } From ebeafaeeed27225f41bba2af8f74da080b2cbce1 Mon Sep 17 00:00:00 2001 From: pressatl Date: Fri, 20 Dec 2024 09:09:10 +0100 Subject: [PATCH 2/2] Finished --- src/Controller/MissionController.php | 81 ++++++++++++++++++++++++ src/Form/MissionType.php | 25 ++++++++ templates/mission/_delete_form.html.twig | 4 ++ templates/mission/_form.html.twig | 4 ++ templates/mission/edit.html.twig | 13 ++++ templates/mission/index.html.twig | 35 ++++++++++ templates/mission/new.html.twig | 11 ++++ templates/mission/show.html.twig | 26 ++++++++ 8 files changed, 199 insertions(+) create mode 100644 src/Controller/MissionController.php create mode 100644 src/Form/MissionType.php create mode 100644 templates/mission/_delete_form.html.twig create mode 100644 templates/mission/_form.html.twig create mode 100644 templates/mission/edit.html.twig create mode 100644 templates/mission/index.html.twig create mode 100644 templates/mission/new.html.twig create mode 100644 templates/mission/show.html.twig diff --git a/src/Controller/MissionController.php b/src/Controller/MissionController.php new file mode 100644 index 0000000..123a7e2 --- /dev/null +++ b/src/Controller/MissionController.php @@ -0,0 +1,81 @@ +render('mission/index.html.twig', [ + 'missions' => $missionRepository->findAll(), + ]); + } + + #[Route('/new', name: '_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $mission = new Mission(); + $form = $this->createForm(MissionType::class, $mission); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($mission); + $entityManager->flush(); + + return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('mission/new.html.twig', [ + 'mission' => $mission, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_show', methods: ['GET'])] + public function show(Mission $mission): Response + { + return $this->render('mission/show.html.twig', [ + 'mission' => $mission, + ]); + } + + #[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Mission $mission, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(MissionType::class, $mission); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('mission/edit.html.twig', [ + 'mission' => $mission, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_delete', methods: ['POST'])] + public function delete(Request $request, Mission $mission, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$mission->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($mission); + $entityManager->flush(); + } + + return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Form/MissionType.php b/src/Form/MissionType.php new file mode 100644 index 0000000..6afd47c --- /dev/null +++ b/src/Form/MissionType.php @@ -0,0 +1,25 @@ +add('label') + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Mission::class, + ]); + } +} diff --git a/templates/mission/_delete_form.html.twig b/templates/mission/_delete_form.html.twig new file mode 100644 index 0000000..6f852cd --- /dev/null +++ b/templates/mission/_delete_form.html.twig @@ -0,0 +1,4 @@ +
+ + +
diff --git a/templates/mission/_form.html.twig b/templates/mission/_form.html.twig new file mode 100644 index 0000000..bf20b98 --- /dev/null +++ b/templates/mission/_form.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} + {{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/templates/mission/edit.html.twig b/templates/mission/edit.html.twig new file mode 100644 index 0000000..b1348b1 --- /dev/null +++ b/templates/mission/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Mission{% endblock %} + +{% block body %} +

Edit Mission

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

Mission index

+ + + + + + + + + + + {% for mission in missions %} + + + + + + {% else %} + + + + {% endfor %} + +
IdLabelactions
{{ mission.id }}{{ mission.label }} + show + edit +
no records found
+ + Create new +{% endblock %} diff --git a/templates/mission/new.html.twig b/templates/mission/new.html.twig new file mode 100644 index 0000000..d497584 --- /dev/null +++ b/templates/mission/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Mission{% endblock %} + +{% block body %} +

Create new Mission

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

Mission

+ + + + + + + + + + + + +
Id{{ mission.id }}
Label{{ mission.label }}
+ + back to list + + edit + + {{ include('mission/_delete_form.html.twig') }} +{% endblock %}