Skip to content

Commit

Permalink
Upgraded to use version 2.1.*...totally decoupled from the HTTP layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Dec 17, 2015
1 parent 79629b3 commit bc7cafb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 61 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,21 @@ class EmployeesController extends JsonApiController
*/
public function getOrdersByEmployee(Request $request)
{
$resource = new ListResource($this->serializer);
$apiRequest = RequestFactory::create();
$page = $apiRequest->getPage();

if (!$page->size()) {
$page->setSize(10); //Default elements per page
}

$resource = new ListResource(
$this->serializer,
$page,
$apiRequest->getFields(),
$apiRequest->getSort(),
$apiRequest->getIncludedRelationships(),
$apiRequest->getFilters()
);

$totalAmount = function() use ($request) {
$id = (new Orders())->getKeyName();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"nilportugues/json-api": "2.0.*",
"nilportugues/json-api": "2.1.*",
"symfony/psr-http-message-bridge": "~0.2",
"nilportugues/serializer-eloquent": "^1.0"
},
Expand Down
130 changes: 86 additions & 44 deletions src/NilPortugues/Laravel5/JsonApi/Controller/JsonApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use NilPortugues\Api\JsonApi\Http\Factory\RequestFactory;
use NilPortugues\Api\JsonApi\Server\Actions\CreateResource;
use NilPortugues\Api\JsonApi\Server\Actions\DeleteResource;
use NilPortugues\Api\JsonApi\Server\Actions\ListResource;
Expand Down Expand Up @@ -45,19 +45,36 @@ public function __construct(JsonApiSerializer $serializer)
}

/**
* @param int $pageSize
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listAction()
public function listAction($pageSize = 10)
{
$resource = new ListResource($this->serializer);

$totalAmount = $this->totalAmountResourceCallable();
$results = $this->listResourceCallable();
$apiRequest = RequestFactory::create();
$page = $apiRequest->getPage();

if (!$page->size()) {
$page->setSize($pageSize);
}

$resource = new ListResource(
$this->serializer,
$page,
$apiRequest->getFields(),
$apiRequest->getSort(),
$apiRequest->getIncludedRelationships(),
$apiRequest->getFilters()
);

$controllerAction = '\\'.get_class($this).'@listAction';
$uri = action($controllerAction, []);
$response = $resource->get(
$this->totalAmountResourceCallable(),
$this->listResourceCallable(),
action('\\'.get_class($this).'@listAction', []),
get_class($this->getDataModel())
);

return $this->addHeaders($resource->get($totalAmount, $results, $uri, get_class($this->getDataModel())));
return $this->addHeaders($response);
}

/**
Expand Down Expand Up @@ -112,10 +129,21 @@ protected function addHeaders(Response $response)
*/
public function getAction(Request $request)
{
$find = $this->findResourceCallable($request);
$resource = new GetResource($this->serializer);
$apiRequest = RequestFactory::create();

$resource = new GetResource(
$this->serializer,
$apiRequest->getFields(),
$apiRequest->getIncludedRelationships()
);

$response = $resource->get(
$request->id,
get_class($this->getDataModel()),
$this->findResourceCallable($request)
);

return $this->addHeaders($resource->get($request->id, get_class($this->getDataModel()), $find));
return $this->addHeaders($response);
}

/**
Expand All @@ -130,10 +158,6 @@ protected function findResourceCallable(Request $request)
$idKey = $this->getDataModel()->getKeyName();
$model = $this->getDataModel()->query()->where($idKey, $request->id)->first();

if (empty($model)) {
throw new ModelNotFoundException('Not found');
}

return $model;
};
}
Expand All @@ -145,13 +169,15 @@ protected function findResourceCallable(Request $request)
*/
public function postAction(Request $request)
{
$createResource = $this->createResourceCallable();

$resource = new CreateResource($this->serializer);

return $this->addHeaders(
$resource->get((array) $request->get('data'), get_class($this->getDataModel()), $createResource)
$response = $resource->get(
(array) $request->get('data'),
get_class($this->getDataModel()),
$this->createResourceCallable()
);

return $this->addHeaders($response);
}

/**
Expand Down Expand Up @@ -191,20 +217,17 @@ protected function createResourceCallable()
*/
public function patchAction(Request $request)
{
$find = $this->findResourceCallable($request);
$update = $this->updateResourceCallable();

$resource = new PatchResource($this->serializer);

return $this->addHeaders(
$resource->get(
$request->id,
(array) $request->get('data'),
get_class($this->getDataModel()),
$find,
$update
)
$response = $resource->get(
$request->id,
(array) $request->get('data'),
get_class($this->getDataModel()),
$this->findResourceCallable($request),
$this->updateResourceCallable()
);

return $this->addHeaders($response);
}

/**
Expand Down Expand Up @@ -233,20 +256,17 @@ protected function updateResourceCallable()
*/
public function putAction(Request $request)
{
$find = $this->findResourceCallable($request);
$update = $this->updateResourceCallable();

$resource = new PutResource($this->serializer);

return $this->addHeaders(
$resource->get(
$request->id,
(array) $request->get('data'),
get_class($this->getDataModel()),
$find,
$update
)
$response = $resource->get(
$request->id,
(array) $request->get('data'),
get_class($this->getDataModel()),
$this->findResourceCallable($request),
$this->updateResourceCallable()
);

return $this->addHeaders($response);
}

/**
Expand All @@ -256,9 +276,31 @@ public function putAction(Request $request)
*/
public function deleteAction(Request $request)
{
$find = $this->findResourceCallable($request);
$resource = new DeleteResource($this->serializer);

return $this->addHeaders($resource->get($request->id, get_class($this->getDataModel()), $find));
$response = $resource->get(
$request->id,
get_class($this->getDataModel()),
$this->findResourceCallable($request),
$this->deleteResourceCallable($request)
);

return $this->addHeaders($response);
}

/**
* @param Request $request
*
* @return callable
* @codeCoverageIgnore
*/
protected function deleteResourceCallable(Request $request)
{
return function () use ($request) {
$idKey = $this->getDataModel()->getKeyName();
$model = $this->getDataModel()->query()->where($idKey, $request->id)->first();

return $model->delete();
};
}
}
8 changes: 4 additions & 4 deletions src/NilPortugues/Laravel5/JsonApi/Eloquent/EloquentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public static function paginate(JsonApiSerializer $serializer, Builder $builder)
$request = RequestFactory::create();

$builder->paginate(
$request->getPageSize(),
self::columns($serializer, $request->getFields()),
$request->getPage()->size(),
self::columns($serializer, $request->getFields()->get()),
'page',
$request->getPageNumber()
$request->getPage()->number()
);

return $builder;
Expand All @@ -52,7 +52,7 @@ public static function paginate(JsonApiSerializer $serializer, Builder $builder)
protected static function sort(JsonApiSerializer $serializer, Builder $builder, Model $model)
{
$mapping = $serializer->getTransformer()->getMappingByClassName(get_class($model));
$sorts = RequestFactory::create()->getSortDirection();
$sorts = RequestFactory::create()->getSort()->sorting();

if (!empty($sorts)) {
$aliased = $mapping->getAliasedProperties();
Expand Down
22 changes: 11 additions & 11 deletions tests/NilPortugues/Laravel5/JsonApi/JsonApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function testGetAction()
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testDeleteAction()
{
$this->createNewEmployee();
$response = $this->call('DELETE', 'http://localhost/api/v1/employees/1');

$this->assertEquals(204, $response->getStatusCode());
}

/**
* @return \Illuminate\Http\Response
*/
Expand Down Expand Up @@ -156,7 +164,7 @@ public function testPostActionReturnsErrorBecauseAttributesAreMissing()
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testPatchActionWhenEmployeeDoesNotExistReturns400()
public function testPatchActionWhenEmployeeDoesNotExistReturns404()
{
$content = <<<JSON
{
Expand All @@ -178,11 +186,11 @@ public function testPatchActionWhenEmployeeDoesNotExistReturns400()
[]
);

$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testPutActionWhenEmployeeDoesNotExistReturns400()
public function testPutActionWhenEmployeeDoesNotExistReturns404()
{
$content = <<<JSON
{
Expand Down Expand Up @@ -220,14 +228,6 @@ public function testPutActionWhenEmployeeDoesNotExistReturns400()
[]
);

$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testDeleteActionWhenEmployeeDoesNotExistReturns404()
{
$response = $this->call('DELETE', 'http://localhost/api/v1/employees/1000');

$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}
Expand Down

0 comments on commit bc7cafb

Please sign in to comment.