This commit is contained in:
Toby Zerner 2019-07-23 21:03:52 +00:00
parent bb415b0486
commit a9feac5ffd
5 changed files with 50 additions and 10 deletions

View File

@ -34,6 +34,8 @@ interface AdapterInterface
public function delete($model);
public function filterByIds($query, array $ids);
public function filterByAttribute($query, Attribute $attribute, $value);
public function filterByHasOne($query, HasOne $relationship, array $ids);

View File

@ -117,6 +117,13 @@ class EloquentAdapter implements AdapterInterface
$model->delete();
}
public function filterByIds($query, array $ids)
{
$key = $query->getModel()->getQualifiedKeyName();
$query->whereIn($key, $ids);
}
public function filterByAttribute($query, Attribute $field, $value)
{
$property = $this->getAttributeProperty($field);
@ -145,8 +152,9 @@ class EloquentAdapter implements AdapterInterface
public function filterByHasOne($query, HasOne $field, array $ids)
{
$property = $this->getRelationshipProperty($field);
$foreignKey = $query->getModel()->{$property}()->getQualifiedForeignKey();
$relation = $query->getModel()->{$this->getRelationshipProperty($field)}();
$foreignKey = $relation->getQualifiedForeignKeyName();
$query->whereIn($foreignKey, $ids);
}

View File

@ -25,12 +25,17 @@ class ResourceNotFoundException extends \RuntimeException implements ErrorProvid
return [
new Error(
new Error\Title('Resource Not Found'),
new Error\Status('404'),
new Error\Status($this->getJsonApiStatus()),
new Error\Detail($this->getMessage())
)
];
}
public function getJsonApiStatus(): string
{
return '404';
}
public function getType(): string
{
return $this->type;

View File

@ -32,11 +32,31 @@ class Create implements RequestHandlerInterface
$model = $this->resource->getAdapter()->create();
$data = $this->parseData($request->getParsedBody());
$adapter = $this->resource->getAdapter();
$this->assertFieldsExist($data);
$this->assertFieldsWritable($data, $model, $request);
$this->fillDefaultValues($data, $request);
$this->loadRelatedResources($data, $request);
$this->assertDataValid($data, $model, $request, true);
$this->applyValues($data, $model, $request);
foreach ($schema->creatingCallbacks as $callback) {
$callback($request, $model);
}
$this->save($model, $request, true);
$adapter->save($model);
$this->saveFields($data, $model, $request);
$this->runSavedCallbacks($data, $model, $request);
foreach ($schema->createdCallbacks as $callback) {
$callback($request, $model);

View File

@ -40,11 +40,11 @@ class Index implements RequestHandlerInterface
$query = $adapter->query();
foreach ($schema->scopes as $scope) {
$scope($request, $query);
$request = $scope($request, $query) ?: $request;
}
foreach ($schema->indexScopes as $scope) {
$scope($request, $query);
$request = $scope($request, $query) ?: $request;
}
if ($filter = $request->getAttribute('jsonApiFilter')) {
@ -57,7 +57,9 @@ class Index implements RequestHandlerInterface
$paginationLinks = [];
$members = [
new Link\SelfLink($this->buildUrl($request))
new Link\SelfLink($this->buildUrl($request)),
new Meta('offset', $offset),
new Meta('limit', $limit),
];
if ($offset > 0) {
@ -197,9 +199,7 @@ class Index implements RequestHandlerInterface
}
foreach ($filter as $name => $value) {
if (! isset($schema->fields[$name])
|| ! $schema->fields[$name]->filterable
) {
if ($name !== 'id' && (! isset($schema->fields[$name]) || ! $schema->fields[$name]->filterable)) {
throw new BadRequestException("Invalid filter [$name]", "filter[$name]");
}
}
@ -261,6 +261,11 @@ class Index implements RequestHandlerInterface
$adapter = $this->resource->getAdapter();
foreach ($filter as $name => $value) {
if ($name === 'id') {
$adapter->filterByIds($query, explode(',', $value));
continue;
}
$field = $schema->fields[$name];
if ($field->filter) {