Commit 27862ca5 authored by Mat's avatar Mat

APIcontroller, la fonction update avec saroute, et la gestion des erreurs

parent 4fb6abe1
......@@ -13,11 +13,15 @@ abstract class MasterAPIController extends MasterController
* @return \Symfony\Component\HttpFoundation\JsonResponse
*
*/
protected function api($data)
protected function api($data, $status = 200)
{
$serializer = $this->get('jms_serializer');
$serialized = $serializer->serialize($data, 'json');
return new JsonResponse($serialized, 200, array(), true);
// if ($data instanceof Form) {
// $status = 400;
// }
return new JsonResponse($serialized, $status, array(), true);
}
}
......@@ -79,4 +79,32 @@ class OwnerController extends MasterAPIController
return $this->api($form);
}
/**
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$owner = $em->getRepository('TechnoBundle:Owner')->find($id);
if ($owner === null) {
throw $this->api('Not found', Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(OwnerAPIType::class, $owner);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($owner);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
}
......@@ -28,6 +28,13 @@ api_owner_create:
_controller: APIBundle:Owner:create
methods: [ POST ]
api_owner_update:
path: /owner/{id}
defaults:
_controller: APIBundle:Owner:update
requirements:
id: \d+
methods: [ POST ]
##
## Note:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment