From de2ab661b524865a68f8875120e818940e8ad36c Mon Sep 17 00:00:00 2001 From: ASTIER Yann Date: Thu, 21 Nov 2024 17:55:00 +0100 Subject: [PATCH] Working on employee backend --- src/Controller/EmployeeController.php | 71 +++++++++++++++++++++-- src/Form/EmployeeType.php | 42 ++++++++++++++ templates/base.html.twig | 7 +++ templates/employee/_delete_form.html.twig | 4 ++ templates/employee/_form.html.twig | 4 ++ templates/employee/edit.html.twig | 13 +++++ templates/employee/index.html.twig | 49 +++++++++++----- templates/employee/new.html.twig | 11 ++++ templates/employee/show.html.twig | 42 ++++++++++++++ 9 files changed, 226 insertions(+), 17 deletions(-) create mode 100644 src/Form/EmployeeType.php create mode 100644 templates/employee/_delete_form.html.twig create mode 100644 templates/employee/_form.html.twig create mode 100644 templates/employee/edit.html.twig create mode 100644 templates/employee/new.html.twig create mode 100644 templates/employee/show.html.twig diff --git a/src/Controller/EmployeeController.php b/src/Controller/EmployeeController.php index 36a4fdb..11f2de5 100644 --- a/src/Controller/EmployeeController.php +++ b/src/Controller/EmployeeController.php @@ -2,17 +2,80 @@ namespace App\Controller; +use App\Entity\Employee; +use App\Form\EmployeeType; +use App\Repository\EmployeeRepository; +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; -class EmployeeController extends AbstractController +#[Route('/employee', name: 'employee')] +final class EmployeeController extends AbstractController { - #[Route('/employee', name: 'app_employee')] - public function index(): Response + #[Route(name: '_index', methods: ['GET'])] + public function index(EmployeeRepository $employeeRepository): Response { return $this->render('employee/index.html.twig', [ - 'controller_name' => 'EmployeeController', + 'employees' => $employeeRepository->findAll(), ]); } + + #[Route('/new', name: '_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $employee = new Employee(); + $form = $this->createForm(EmployeeType::class, $employee); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($employee); + $entityManager->flush(); + + return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('employee/new.html.twig', [ + 'employee' => $employee, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_show', methods: ['GET'])] + public function show(Employee $employee): Response + { + return $this->render('employee/show.html.twig', [ + 'employee' => $employee, + ]); + } + + #[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(EmployeeType::class, $employee); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('employee/edit.html.twig', [ + 'employee' => $employee, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_delete', methods: ['POST'])] + public function delete(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$employee->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($employee); + $entityManager->flush(); + } + + return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER); + } } diff --git a/src/Form/EmployeeType.php b/src/Form/EmployeeType.php new file mode 100644 index 0000000..7868b3b --- /dev/null +++ b/src/Form/EmployeeType.php @@ -0,0 +1,42 @@ +add('email', EmailType::class, ['label' => 'Email Address']) + ->add('firstName', TextType::class, ['label' => 'First Name']) + ->add('lastName', TextType::class, ['label' => 'Last Name']) + ->add('password', PasswordType::class, ['label' => 'Password']) + ->add('roles', ChoiceType::class, [ + 'label' => 'Roles (comma-separated)', + 'required' => false, + 'choices' => [ + 'User' => 'ROLE_USER', + 'Admin' => 'ROLE_ADMIN', + ], + 'multiple' => true, // Allow multiple selections + 'expanded' => true, // Render as checkboxes + ]) + ->add('save', SubmitType::class, ['label' => 'Add Employee']); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Employee::class, + ]); + } +} \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 4bb4a26..ebef434 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -13,6 +13,13 @@ {% endblock %} + + {% for label, messages in app.flashes %} + {% for message in messages %} +
{{ message }}
+ {% endfor %} + {% endfor %} +