diff --git a/src/Controller/AffectationController.php b/src/Controller/AffectationController.php index db70093..c6c0047 100644 --- a/src/Controller/AffectationController.php +++ b/src/Controller/AffectationController.php @@ -17,4 +17,42 @@ class AffectationController extends AbstractController 'controller_name' => 'AffectationController', ]); } + + public function add(Request $request) + { + $validated = $request->validate([ + 'name' => 'required|string|max:255', + 'description' => 'nullable|string', + ]); + + $affectation = Affectation::create($validated); + + return response()->json([ + 'message' => 'Affectation réussie', + 'data' => $affectation + ], 201); + } + + public function list() + { + $affectations = Affectation::all(); + + return response()->json([ + 'data' => $affectations + ], 200); + } + + public function delete($id) + { + $affectation = Affectation::find($id); + + if (!$affectation) { + return response()->json(['message' => ''], 404); + } + + $affectation->delete(); + + return response()->json(['message' => ''], 200); + } + } diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php new file mode 100644 index 0000000..af71d09 --- /dev/null +++ b/src/Controller/CategoryController.php @@ -0,0 +1,74 @@ +render('category/index.html.twig', [ + 'categories' => $categoryRepository->findAll(), + ]); + } + + #[Route('/new', name: 'app_category_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $category = new Category(); + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($category); + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/new.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + + #[Route('/{id}/edit', name: 'app_category_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/edit.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: 'app_category_delete', methods: ['POST'])] + public function delete(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$category->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($category); + $entityManager->flush(); + } + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Form/CategoryType.php b/src/Form/CategoryType.php new file mode 100644 index 0000000..4d68103 --- /dev/null +++ b/src/Form/CategoryType.php @@ -0,0 +1,28 @@ +add('label', TextType::class, [ + 'label' => 'Category label', + ]); + + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Category::class, + ]); + } +} diff --git a/templates/affectation/index.html.twig b/templates/affectation/index.html.twig new file mode 100644 index 0000000..2db6701 --- /dev/null +++ b/templates/affectation/index.html.twig @@ -0,0 +1,21 @@ +{% extends 'base.html.twig' %} + +{% block title %}Affectation{% endblock %} + + +{% block body %} + +
Id | +Label | +actions | +
---|---|---|
{{ category.id }} | +{{ category.label }} | ++ edit + | +
no records found | +