API: changed date-format, camelCase instead of snake_case, null values, update and create for customer and project (#718)

This commit is contained in:
Kevin Papst
2019-04-24 18:13:33 +02:00
committed by GitHub
parent 215d4fc8bf
commit 460391136f
61 changed files with 2304 additions and 505 deletions

View File

@@ -11,6 +11,8 @@ declare(strict_types=1);
namespace App\API;
use App\Entity\Customer;
use App\Form\CustomerEditForm;
use App\Repository\CustomerRepository;
use App\Repository\Query\CustomerQuery;
use FOS\RestBundle\Controller\Annotations as Rest;
@@ -20,7 +22,9 @@ use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* @RouteResource("Customer")
@@ -50,17 +54,19 @@ class CustomerController extends BaseApiController
}
/**
* Returns a collection of customers
*
* @SWG\Response(
* response=200,
* description="Returns the collection of all existing customer",
* description="Returns a collection of customer entities",
* @SWG\Schema(
* type="array",
* @SWG\Items(ref="#/definitions/CustomerEntity")
* @SWG\Items(ref="#/definitions/CustomerCollection")
* )
* )
* @Rest\QueryParam(name="visible", requirements="\d+", strict=true, nullable=true, description="Visibility status to filter activities (1=visible, 2=hidden, 3=both)")
* @Rest\QueryParam(name="order", requirements="ASC|DESC", strict=true, nullable=true, description="The result order (allowed values: 'ASC', 'DESC')")
* @Rest\QueryParam(name="orderBy", requirements="id|name", strict=true, nullable=true, description="The field by which results will be ordered (allowed values: 'id', 'name')")
* @Rest\QueryParam(name="order", requirements="ASC|DESC", strict=true, nullable=true, description="The result order. Allowed values: ASC, DESC (default: ASC)")
* @Rest\QueryParam(name="orderBy", requirements="id|name", strict=true, nullable=true, description="The field by which results will be ordered. Allowed values: id, name (default: name)")
*
* @return Response
*/
@@ -92,6 +98,8 @@ class CustomerController extends BaseApiController
}
/**
* Returns one customer
*
* @SWG\Response(
* response=200,
* description="Returns one customer entity",
@@ -104,12 +112,134 @@ class CustomerController extends BaseApiController
public function getAction($id)
{
$data = $this->repository->find($id);
if (null === $data) {
throw new NotFoundException();
}
$view = new View($data, 200);
$view->getContext()->setGroups(['Default', 'Entity', 'Customer']);
return $this->viewHandler->handle($view);
}
/**
* Creates a new customer
*
* @SWG\Post(
* description="Creates a new customer and returns it afterwards",
* @SWG\Response(
* response=200,
* description="Returns the new created customer",
* @SWG\Schema(ref="#/definitions/CustomerEntity"),
* )
* )
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(ref="#/definitions/CustomerEditForm")
* )
*
* @param Request $request
* @return Response
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function postAction(Request $request)
{
if (!$this->isGranted('create_customer')) {
throw new AccessDeniedHttpException('User cannot create customers');
}
$customer = new Customer();
$form = $this->createForm(CustomerEditForm::class, $customer, [
'csrf_protection' => false,
]);
$form->submit($request->request->all());
if ($form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($customer);
$entityManager->flush();
$view = new View($customer, 200);
$view->getContext()->setGroups(['Default', 'Entity', 'Customer']);
return $this->viewHandler->handle($view);
}
$view = new View($form);
$view->getContext()->setGroups(['Default', 'Entity', 'Customer']);
return $this->viewHandler->handle($view);
}
/**
* Update an existing customer
*
* @SWG\Patch(
* description="Update an existing customer, you can pass all or just a subset of all attributes",
* @SWG\Response(
* response=200,
* description="Returns the updated customer",
* @SWG\Schema(ref="#/definitions/CustomerEntity")
* )
* )
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(ref="#/definitions/CustomerEditForm")
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="Customer ID to update",
* required=true,
* )
*
* @param Request $request
* @param string $id
* @return Response
*/
public function patchAction(Request $request, string $id)
{
$customer = $this->repository->find($id);
if (null === $customer) {
throw new NotFoundException();
}
if (!$this->isGranted('edit', $customer)) {
throw new AccessDeniedHttpException('User cannot update customer');
}
$form = $this->createForm(CustomerEditForm::class, $customer, [
'csrf_protection' => false,
]);
$form->setData($customer);
$form->submit($request->request->all(), false);
if (false === $form->isValid()) {
$view = new View($form, Response::HTTP_OK);
$view->getContext()->setGroups(['Default', 'Entity', 'Customer']);
return $this->viewHandler->handle($view);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($customer);
$entityManager->flush();
$view = new View($customer, Response::HTTP_OK);
$view->getContext()->setGroups(['Default', 'Entity', 'Customer']);
return $this->viewHandler->handle($view);
}
}