91 lines
1.9 KiB
PHP
91 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace AppBundle\Controller\Api;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
|
use FOS\RestBundle\Controller\Annotations as Rest;
|
|
use FOS\RestBundle\View\View;
|
|
use FOS\RestBundle\Controller\FOSRestController;
|
|
use AppBundle\Entity\Category;
|
|
use AppBundle\Form\CategoryType;
|
|
|
|
/**
|
|
* class CategoryController.
|
|
*
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
*/
|
|
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];
|
|
}
|
|
}
|