diff --git a/src/AppBundle/Controller/Api/BookController.php b/src/AppBundle/Controller/Api/BookController.php new file mode 100644 index 0000000..d00ed6a --- /dev/null +++ b/src/AppBundle/Controller/Api/BookController.php @@ -0,0 +1,37 @@ + + */ +class BookController extends FOSRestController +{ + /** + * Get books. + * + * @Rest\View + * + * @ApiDoc( + * description="Get books.", + * ) + * + * @param Request $request + * + * @return array + */ + public function getBooksAction(Request $request) + { + return $this->getDoctrine() + ->getRepository(Book::class) + ->findAll(); + } +} diff --git a/src/AppBundle/Controller/Api/CategoryController.php b/src/AppBundle/Controller/Api/CategoryController.php new file mode 100644 index 0000000..690a56f --- /dev/null +++ b/src/AppBundle/Controller/Api/CategoryController.php @@ -0,0 +1,90 @@ + + */ +class CategoryController extends FOSRestController +{ + /** + * Get categories. + * + * @Rest\View + * + * @ApiDoc( + * description="Get categories.", + * ) + * + * @param Request $request + * + * @return array + */ + public function getCategoriesAction(Request $request) + { + return $this->getDoctrine() + ->getRepository(Category::class) + ->findAll(); + } + + /** + * Get a category. + * + * @Rest\View + * + * @ApiDoc( + * description="Get a category.", + * ) + * + * @param Request $request + * + * @return mixed + */ + public function getCategoryction(Request $request, Category $category) + { + return $category; + } + + /** + * Create a category. + * + * @Rest\View + * @Rest\Post("/categories/new") + * @ApiDoc( + * description="Create a category.", + * input={ + * "class"="AppBundle\Form\CategoryType" + * } + * ) + * + * @param Request $request + * + * @return mixed + */ + public function postCategoryNewAction(Request $request) + { + $object = new Category(); + $form = $this->createForm(CategoryType::class, $object); + $form->handleRequest($request); + + if (!$form->isSubmitted() || !$form->isValid()) { + return $this->view($form->getErrors(true), 400); + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($form->getData()); + $em->flush(); + + return ['status' => true]; + } +} diff --git a/src/AppBundle/Controller/CategoryController.php b/src/AppBundle/Controller/CategoryController.php new file mode 100644 index 0000000..be2e46c --- /dev/null +++ b/src/AppBundle/Controller/CategoryController.php @@ -0,0 +1,36 @@ + + */ +class CategoryController extends Controller +{ + /** + * Categories page. + * + * @Template() + * @Route("/", name="example") + * + * @param Request $request + * + * @return \Symfony\Bundle\FrameworkBundle\Controller\Response + */ + public function indexAction(Request $request) + { + return [ + 'form' => $this->createForm(CategoryType::class, new Category())->createView(), + ]; + } +} diff --git a/src/AppBundle/Form/CategoryType.php b/src/AppBundle/Form/CategoryType.php new file mode 100644 index 0000000..56e4d7c --- /dev/null +++ b/src/AppBundle/Form/CategoryType.php @@ -0,0 +1,50 @@ +add( + 'name', + TextType::class, + [ + 'label' => 'Nom', + 'required' => false, // html5 validation disable for the example + 'constraints' => [ + new NotBlank(), + ], + ] + ); + } + + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => 'AppBundle\Entity\Category', + 'csrf_protection' => false, + 'allow_extra_fields' => true, + ]); + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return ''; + } +} diff --git a/src/AppBundle/Resources/views/Category/index.html.twig b/src/AppBundle/Resources/views/Category/index.html.twig new file mode 100644 index 0000000..eea43d6 --- /dev/null +++ b/src/AppBundle/Resources/views/Category/index.html.twig @@ -0,0 +1,61 @@ + + + + + + + + +{{ form_row(form.name) }} + +

+
+
+
+
+
+
+
+
+
+
+
+
+
+