wip
This commit is contained in:
parent
bb415b0486
commit
a9feac5ffd
|
|
@ -34,6 +34,8 @@ interface AdapterInterface
|
||||||
|
|
||||||
public function delete($model);
|
public function delete($model);
|
||||||
|
|
||||||
|
public function filterByIds($query, array $ids);
|
||||||
|
|
||||||
public function filterByAttribute($query, Attribute $attribute, $value);
|
public function filterByAttribute($query, Attribute $attribute, $value);
|
||||||
|
|
||||||
public function filterByHasOne($query, HasOne $relationship, array $ids);
|
public function filterByHasOne($query, HasOne $relationship, array $ids);
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,13 @@ class EloquentAdapter implements AdapterInterface
|
||||||
$model->delete();
|
$model->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function filterByIds($query, array $ids)
|
||||||
|
{
|
||||||
|
$key = $query->getModel()->getQualifiedKeyName();
|
||||||
|
|
||||||
|
$query->whereIn($key, $ids);
|
||||||
|
}
|
||||||
|
|
||||||
public function filterByAttribute($query, Attribute $field, $value)
|
public function filterByAttribute($query, Attribute $field, $value)
|
||||||
{
|
{
|
||||||
$property = $this->getAttributeProperty($field);
|
$property = $this->getAttributeProperty($field);
|
||||||
|
|
@ -145,8 +152,9 @@ class EloquentAdapter implements AdapterInterface
|
||||||
|
|
||||||
public function filterByHasOne($query, HasOne $field, array $ids)
|
public function filterByHasOne($query, HasOne $field, array $ids)
|
||||||
{
|
{
|
||||||
$property = $this->getRelationshipProperty($field);
|
$relation = $query->getModel()->{$this->getRelationshipProperty($field)}();
|
||||||
$foreignKey = $query->getModel()->{$property}()->getQualifiedForeignKey();
|
|
||||||
|
$foreignKey = $relation->getQualifiedForeignKeyName();
|
||||||
|
|
||||||
$query->whereIn($foreignKey, $ids);
|
$query->whereIn($foreignKey, $ids);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,17 @@ class ResourceNotFoundException extends \RuntimeException implements ErrorProvid
|
||||||
return [
|
return [
|
||||||
new Error(
|
new Error(
|
||||||
new Error\Title('Resource Not Found'),
|
new Error\Title('Resource Not Found'),
|
||||||
new Error\Status('404'),
|
new Error\Status($this->getJsonApiStatus()),
|
||||||
new Error\Detail($this->getMessage())
|
new Error\Detail($this->getMessage())
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getJsonApiStatus(): string
|
||||||
|
{
|
||||||
|
return '404';
|
||||||
|
}
|
||||||
|
|
||||||
public function getType(): string
|
public function getType(): string
|
||||||
{
|
{
|
||||||
return $this->type;
|
return $this->type;
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,31 @@ class Create implements RequestHandlerInterface
|
||||||
|
|
||||||
$model = $this->resource->getAdapter()->create();
|
$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) {
|
foreach ($schema->creatingCallbacks as $callback) {
|
||||||
$callback($request, $model);
|
$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) {
|
foreach ($schema->createdCallbacks as $callback) {
|
||||||
$callback($request, $model);
|
$callback($request, $model);
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,11 @@ class Index implements RequestHandlerInterface
|
||||||
$query = $adapter->query();
|
$query = $adapter->query();
|
||||||
|
|
||||||
foreach ($schema->scopes as $scope) {
|
foreach ($schema->scopes as $scope) {
|
||||||
$scope($request, $query);
|
$request = $scope($request, $query) ?: $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($schema->indexScopes as $scope) {
|
foreach ($schema->indexScopes as $scope) {
|
||||||
$scope($request, $query);
|
$request = $scope($request, $query) ?: $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($filter = $request->getAttribute('jsonApiFilter')) {
|
if ($filter = $request->getAttribute('jsonApiFilter')) {
|
||||||
|
|
@ -57,7 +57,9 @@ class Index implements RequestHandlerInterface
|
||||||
|
|
||||||
$paginationLinks = [];
|
$paginationLinks = [];
|
||||||
$members = [
|
$members = [
|
||||||
new Link\SelfLink($this->buildUrl($request))
|
new Link\SelfLink($this->buildUrl($request)),
|
||||||
|
new Meta('offset', $offset),
|
||||||
|
new Meta('limit', $limit),
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($offset > 0) {
|
if ($offset > 0) {
|
||||||
|
|
@ -197,9 +199,7 @@ class Index implements RequestHandlerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($filter as $name => $value) {
|
foreach ($filter as $name => $value) {
|
||||||
if (! isset($schema->fields[$name])
|
if ($name !== 'id' && (! isset($schema->fields[$name]) || ! $schema->fields[$name]->filterable)) {
|
||||||
|| ! $schema->fields[$name]->filterable
|
|
||||||
) {
|
|
||||||
throw new BadRequestException("Invalid filter [$name]", "filter[$name]");
|
throw new BadRequestException("Invalid filter [$name]", "filter[$name]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -261,6 +261,11 @@ class Index implements RequestHandlerInterface
|
||||||
$adapter = $this->resource->getAdapter();
|
$adapter = $this->resource->getAdapter();
|
||||||
|
|
||||||
foreach ($filter as $name => $value) {
|
foreach ($filter as $name => $value) {
|
||||||
|
if ($name === 'id') {
|
||||||
|
$adapter->filterByIds($query, explode(',', $value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$field = $schema->fields[$name];
|
$field = $schema->fields[$name];
|
||||||
|
|
||||||
if ($field->filter) {
|
if ($field->filter) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue