Setup JSON:API for LexicalEntries

This commit is contained in:
Leonard Smith 2020-10-06 22:28:24 -05:00
parent b5f067e185
commit d64760b075
6 changed files with 141 additions and 1 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace App\JsonApi\LexicalEntries;
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class Adapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
*/
public function __construct(StandardStrategy $paging)
{
parent::__construct(new \App\LexicalEntry(), $paging);
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
*/
protected function filter($query, Collection $filters)
{
$this->filterWithScopes($query, $filters);
}
/**
* @return \CloudCreativity\LaravelJsonApi\Eloquent\HasMany
*/
protected function words()
{
return $this->hasMany();
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace App\JsonApi\LexicalEntries;
use Neomerx\JsonApi\Schema\SchemaProvider;
class Schema extends SchemaProvider
{
/**
* @var string
*/
protected $resourceType = 'lexical-entries';
/**
* @param \App\LexicalEntry $resource
* the domain record being serialized.
* @return string
*/
public function getId($resource)
{
return (string) $resource->getRouteKey();
}
/**
* @param \App\LexicalEntry $resource
* the domain record being serialized.
* @return array
*/
public function getAttributes($resource)
{
return [
'lemma' => $resource->lemma,
'commentary' => $resource->commentary,
'created-at' => $resource->created_at->toAtomString(),
'updated-at' => $resource->updated_at->toAtomString(),
];
}
/**
* @param object $resource
* @param bool $isPrimary
* @param array $includeRelationships
* @return array|\bool[][]
*/
public function getRelationships($resource, $isPrimary, array $includeRelationships)
{
return [
'words' => [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
]
];
}
}

View File

@ -51,4 +51,12 @@ class Adapter extends AbstractAdapter
{
return $this->hasOne();
}
/**
* @return \CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo
*/
protected function lexicalEntry()
{
return $this->hasOne();
}
}

View File

@ -40,4 +40,20 @@ class Schema extends SchemaProvider
'updated-at' => $resource->updated_at->toAtomString(),
];
}
/**
* @param object $resource
* @param bool $isPrimary
* @param array $includeRelationships
* @return array|\bool[][]
*/
public function getRelationships($resource, $isPrimary, array $includeRelationships)
{
return [
'lexical-entry' => [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
]
];
}
}

View File

@ -70,6 +70,7 @@ return [
'chapters' => \App\Chapter::class,
'verses' => \App\Verse::class,
'words' => \App\Word::class,
'lexical-entries' => \App\LexicalEntry::class,
],
/*

View File

@ -31,5 +31,11 @@ JsonApi::register('default')->routes(function ($api) {
$relations->hasMany('words')->readOnly();
});
$api->resource('words')->readOnly();
$api->resource('words')->readOnly()->relationships(function ($relations) {
$relations->hasOne('lexical-entry')->readOnly();
});
$api->resource('lexical-entries')->readOnly()->relationships(function ($relations) {
$relations->hasMany('words')->readOnly();
});
});