Skip to content

Commit

Permalink
Merge pull request GoIntegro#27 from GoIntegro/feature/api-59-to-one-…
Browse files Browse the repository at this point in the history
…urls

[API-59] To one relationship URLs
  • Loading branch information
skqr committed Nov 22, 2014
2 parents 92a30bc + f155c6b commit 085e42a
Show file tree
Hide file tree
Showing 33 changed files with 591 additions and 309 deletions.
1 change: 1 addition & 0 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function getResourcesFromRepo(Params $params)

$resources = $this->get('hateoas.resource_manager')
->createCollectionFactory()
->setRequest($this->getRequest())
->setPaginator($entities->getPaginator())
->create();

Expand Down
109 changes: 87 additions & 22 deletions Controller/MagicAlterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
use GoIntegro\Bundle\HateoasBundle\JsonApi\Exception\DocumentTooLargeHttpException,
GoIntegro\Bundle\HateoasBundle\JsonApi\ResourceEntityInterface,
GoIntegro\Bundle\HateoasBundle\JsonApi\Request\Params,
GoIntegro\Bundle\HateoasBundle\JsonApi\Document;
GoIntegro\Bundle\HateoasBundle\JsonApi\Document,
GoIntegro\Bundle\HateoasBundle\JsonApi\Exception\NotFoundException;
// Utils.
use GoIntegro\Bundle\HateoasBundle\Util\Inflector;
// Security.
Expand All @@ -36,20 +37,13 @@
// Request.
use GoIntegro\Bundle\HateoasBundle\JsonApi\Request\ParseException,
GoIntegro\Bundle\HateoasBundle\JsonApi\Request\ActionNotAllowedException,
GoIntegro\Bundle\HateoasBundle\JsonApi\Request\EntityAccessDeniedException,
GoIntegro\Bundle\HateoasBundle\JsonApi\Request\EntityNotFoundException,
GoIntegro\Bundle\HateoasBundle\JsonApi\Request\ResourceNotFoundException;
GoIntegro\Bundle\HateoasBundle\JsonApi\Request\EntityAccessDeniedException;

/**
* Permite probar la flexibilidad de la biblioteca.
* @todo Refactor.
*/
class MagicAlterController extends SymfonyController
{
use CommonResponseTrait;

const RESOURCE_LIMIT = 50,
ERROR_ACCESS_DENIED = "Access to the resource was denied.",
const ERROR_ACCESS_DENIED = "Access to the resource was denied.",
ERROR_RESOURCE_NOT_FOUND = "The resource was not found.",
ERROR_RELATIONSHIP_NOT_FOUND = "No relationship by that name found.",
ERROR_FIELD_NOT_FOUND = "No field by that name found.";
Expand All @@ -67,8 +61,8 @@ class MagicAlterController extends SymfonyController
public function createAction($primaryType)
{
try {
$params = $this->get('hateoas.request_parser')->parse();
} catch (ResourceNotFoundException $e) {
$params = $this->get('hateoas.request_parser')->parse($this->getRequest());
} catch (NotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (ActionNotAllowedException $e) {
throw new MethodNotAllowedHttpException(
Expand All @@ -78,8 +72,6 @@ public function createAction($primaryType)
throw new BadRequestHttpException($e->getMessage(), $e);
} catch (EntityAccessDeniedException $e) {
throw new AccessDeniedHttpException($e->getMessage(), $e);
} catch (EntityNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (DocumentTooLargeException $e) {
throw new DocumentTooLargeHttpException($e->getMessage(), $e);
}
Expand Down Expand Up @@ -110,6 +102,7 @@ public function createAction($primaryType)
$resources = 1 < count($entities)
? $this->get('hateoas.resource_manager')
->createCollectionFactory()
->setParams($params)
->addEntities($entities)
->create()
: $this->get('hateoas.resource_manager')
Expand All @@ -118,6 +111,7 @@ public function createAction($primaryType)
->create();
$json = $this->get('hateoas.resource_manager')
->createSerializerFactory()
->setParams($params)
->setDocumentResources($resources)
->create()
->serialize();
Expand All @@ -138,8 +132,8 @@ public function createAction($primaryType)
public function updateAction($primaryType, $ids)
{
try {
$params = $this->get('hateoas.request_parser')->parse();
} catch (ResourceNotFoundException $e) {
$params = $this->get('hateoas.request_parser')->parse($this->getRequest());
} catch (NotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (ActionNotAllowedException $e) {
throw new MethodNotAllowedHttpException(
Expand All @@ -149,8 +143,6 @@ public function updateAction($primaryType, $ids)
throw new BadRequestHttpException($e->getMessage(), $e);
} catch (EntityAccessDeniedException $e) {
throw new AccessDeniedHttpException($e->getMessage(), $e);
} catch (EntityNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (DocumentTooLargeException $e) {
throw new DocumentTooLargeHttpException($e->getMessage(), $e);
}
Expand Down Expand Up @@ -181,6 +173,7 @@ public function updateAction($primaryType, $ids)
$resources = 1 < count($params->entities)
? $this->get('hateoas.resource_manager')
->createCollectionFactory()
->setParams($params)
->addEntities($params->entities)
->create()
: $this->get('hateoas.resource_manager')
Expand All @@ -189,6 +182,7 @@ public function updateAction($primaryType, $ids)
->create();
$json = $this->get('hateoas.resource_manager')
->createSerializerFactory()
->setParams($params)
->setDocumentResources($resources)
->create()
->serialize();
Expand All @@ -209,8 +203,8 @@ public function updateAction($primaryType, $ids)
public function deleteAction($primaryType, $ids)
{
try {
$params = $this->get('hateoas.request_parser')->parse();
} catch (ResourceNotFoundException $e) {
$params = $this->get('hateoas.request_parser')->parse($this->getRequest());
} catch (NotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (ActionNotAllowedException $e) {
throw new MethodNotAllowedHttpException(
Expand All @@ -220,8 +214,6 @@ public function deleteAction($primaryType, $ids)
throw new BadRequestHttpException($e->getMessage(), $e);
} catch (EntityAccessDeniedException $e) {
throw new AccessDeniedHttpException($e->getMessage(), $e);
} catch (EntityNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (DocumentTooLargeException $e) {
throw new DocumentTooLargeHttpException($e->getMessage(), $e);
}
Expand All @@ -243,9 +235,82 @@ public function deleteAction($primaryType, $ids)
return $this->createNoCacheResponse(NULL, Response::HTTP_NO_CONTENT);
}

/**
* @Route("/{primaryType}/{id}/links/{relationship}", name="hateoas_magic_link", methods="POST")
* @param string $primaryType
* @param string $id
* @param string $relationship
* @throws AccessDeniedHttpException
* @throws NotFoundHttpException
* @throws BadRequestHttpException
* @see http://jsonapi.org/format/#crud-updating-relationships
*/
public function linkAction($primaryType, $id, $relationship)
{
try {
$params = $this->get('hateoas.request_parser')->parse($this->getRequest());
} catch (NotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
} catch (ActionNotAllowedException $e) {
throw new MethodNotAllowedHttpException(
$e->getAllowedMethods(), $e->getMessage(), $e
);
} catch (ParseException $e) {
throw new BadRequestHttpException($e->getMessage(), $e);
} catch (EntityAccessDeniedException $e) {
throw new AccessDeniedHttpException($e->getMessage(), $e);
}

$metadata = $this->get('hateoas.metadata_miner')
->mine($params->primaryClass);
$relation = NULL;
$relatedResource = NULL;
}

/**
* @Route("/{primaryType}/{id}/links/{relationship}", name="hateoas_magic_update_link", methods="PUT")
* @param string $primaryType
* @param string $id
* @param string $relationship
* @throws AccessDeniedHttpException
* @throws NotFoundHttpException
* @throws BadRequestHttpException
* @see http://jsonapi.org/format/#crud-updating-relationships
*/
public function updateLinkAction($primaryType, $id, $relationship)
{}

/**
* @Route("/{primaryType}/{id}/links/{relationship}", name="hateoas_magic_unlink_one", methods="DELETE")
* @param string $primaryType
* @param string $id
* @param string $relationship
* @throws AccessDeniedHttpException
* @throws NotFoundHttpException
* @throws BadRequestHttpException
* @see http://jsonapi.org/format/#crud-updating-relationships
*/
public function unlinkOneAction($primaryType, $id, $relationship)
{}

/**
* @Route("/{primaryType}/{id}/links/{relationship}/{ids}", name="hateoas_magic_unlink_many", methods="DELETE")
* @param string $primaryType
* @param string $id
* @param string $relationship
* @param string $ids
* @throws AccessDeniedHttpException
* @throws NotFoundHttpException
* @throws BadRequestHttpException
* @see http://jsonapi.org/format/#crud-updating-relationships
*/
public function unlinkManyAction($primaryType, $id, $relationship, $ids)
{}

/**
* @param array &$data
* @return array
* @todo Move.
*/
private function extractLinks(array &$data)
{
Expand Down
Loading

0 comments on commit 085e42a

Please sign in to comment.