From d64760b0751ed9156dee6d8a0a03b34cec5a2625 Mon Sep 17 00:00:00 2001 From: Leonard Smith Date: Tue, 6 Oct 2020 22:28:24 -0500 Subject: [PATCH] Setup JSON:API for LexicalEntries --- app/JsonApi/LexicalEntries/Adapter.php | 54 +++++++++++++++++++++++++ app/JsonApi/LexicalEntries/Schema.php | 55 ++++++++++++++++++++++++++ app/JsonApi/Words/Adapter.php | 8 ++++ app/JsonApi/Words/Schema.php | 16 ++++++++ config/json-api-default.php | 1 + routes/api.php | 8 +++- 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 app/JsonApi/LexicalEntries/Adapter.php create mode 100644 app/JsonApi/LexicalEntries/Schema.php diff --git a/app/JsonApi/LexicalEntries/Adapter.php b/app/JsonApi/LexicalEntries/Adapter.php new file mode 100644 index 0000000..5ac1eb7 --- /dev/null +++ b/app/JsonApi/LexicalEntries/Adapter.php @@ -0,0 +1,54 @@ +filterWithScopes($query, $filters); + } + + /** + * @return \CloudCreativity\LaravelJsonApi\Eloquent\HasMany + */ + protected function words() + { + return $this->hasMany(); + } +} diff --git a/app/JsonApi/LexicalEntries/Schema.php b/app/JsonApi/LexicalEntries/Schema.php new file mode 100644 index 0000000..14d76d8 --- /dev/null +++ b/app/JsonApi/LexicalEntries/Schema.php @@ -0,0 +1,55 @@ +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, + ] + ]; + } +} diff --git a/app/JsonApi/Words/Adapter.php b/app/JsonApi/Words/Adapter.php index 1940965..0fd4e6a 100644 --- a/app/JsonApi/Words/Adapter.php +++ b/app/JsonApi/Words/Adapter.php @@ -51,4 +51,12 @@ class Adapter extends AbstractAdapter { return $this->hasOne(); } + + /** + * @return \CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo + */ + protected function lexicalEntry() + { + return $this->hasOne(); + } } diff --git a/app/JsonApi/Words/Schema.php b/app/JsonApi/Words/Schema.php index cb6f161..b4a472d 100644 --- a/app/JsonApi/Words/Schema.php +++ b/app/JsonApi/Words/Schema.php @@ -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, + ] + ]; + } } diff --git a/config/json-api-default.php b/config/json-api-default.php index f3629a8..3c0889d 100644 --- a/config/json-api-default.php +++ b/config/json-api-default.php @@ -70,6 +70,7 @@ return [ 'chapters' => \App\Chapter::class, 'verses' => \App\Verse::class, 'words' => \App\Word::class, + 'lexical-entries' => \App\LexicalEntry::class, ], /* diff --git a/routes/api.php b/routes/api.php index 2fd65fa..0a8823e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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(); + }); });