mirror of https://github.com/kcal-app/kcal.git
Add API relationships
This commit is contained in:
parent
166427dce6
commit
39455af3c9
|
@ -3,6 +3,7 @@
|
|||
namespace App\JsonApi\Adapters;
|
||||
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
@ -44,4 +45,14 @@ class IngredientAmountAdapter extends AbstractAdapter
|
|||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
protected function ingredient(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo();
|
||||
}
|
||||
|
||||
protected function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
namespace App\JsonApi\Adapters;
|
||||
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\MorphHasMany;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
@ -44,4 +46,19 @@ class JournalEntryAdapter extends AbstractAdapter
|
|||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
protected function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo();
|
||||
}
|
||||
|
||||
protected function foods(): MorphHasMany
|
||||
{
|
||||
return $this->morphMany();
|
||||
}
|
||||
|
||||
protected function recipes(): MorphHasMany
|
||||
{
|
||||
return $this->morphMany();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\JsonApi\Adapters;
|
||||
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\HasMany;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
@ -44,4 +45,14 @@ class RecipeAdapter extends AbstractAdapter
|
|||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
protected function ingredientAmounts(): HasMany
|
||||
{
|
||||
return $this->hasMany();
|
||||
}
|
||||
|
||||
protected function steps(): HasMany
|
||||
{
|
||||
return $this->hasMany();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\JsonApi\Adapters;
|
||||
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
@ -44,4 +45,9 @@ class RecipeStepAdapter extends AbstractAdapter
|
|||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
protected function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,4 +45,29 @@ class IngredientAmountSchema extends SchemaProvider
|
|||
'updatedAt' => $resource->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'ingredient' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['ingredient']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->ingredient;
|
||||
},
|
||||
],
|
||||
'parent' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['parent']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->parent;
|
||||
},
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,4 +43,37 @@ class JournalEntrySchema extends SchemaProvider
|
|||
'updatedAt' => $resource->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'user' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['user']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->user;
|
||||
},
|
||||
],
|
||||
'foods' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['foods']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->foods;
|
||||
},
|
||||
],
|
||||
'recipes' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['recipes']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->recipes;
|
||||
},
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\JsonApi\Schemas;
|
||||
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\HasMany;
|
||||
use Neomerx\JsonApi\Schema\SchemaProvider;
|
||||
|
||||
class RecipeSchema extends SchemaProvider
|
||||
|
@ -50,4 +51,29 @@ class RecipeSchema extends SchemaProvider
|
|||
'updatedAt' => $resource->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'steps' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['steps']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->steps;
|
||||
},
|
||||
],
|
||||
'ingredient-amounts' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['ingredient-amounts']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->ingredientAmounts;
|
||||
},
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,4 +36,17 @@ class RecipeStepSchema extends SchemaProvider
|
|||
'updatedAt' => $resource->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'recipe' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,15 +14,17 @@
|
|||
JsonApi::register('v1')->routes(function ($api) {
|
||||
$api->resource('foods')->readOnly();
|
||||
$api->resource('ingredient-amounts')->relationships(function ($relations) {
|
||||
$relations->hasOne('ingredient', 'parent')->readOnly();
|
||||
$relations->hasOne('ingredient')->readOnly();
|
||||
$relations->hasOne('ingredparentient')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('journal-entries')->relationships(function ($relations) {
|
||||
$relations->hasMany('foods', 'recipes')->readOnly();
|
||||
$relations->hasMany('foods')->readOnly();
|
||||
$relations->hasMany('recipes')->readOnly();
|
||||
$relations->hasOne('user')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('recipes')->relationships(function ($relations) {
|
||||
$relations->hasMany('recipe-steps')->uri('steps')->readOnly();
|
||||
$relations->hasMany('ingredient-amounts')->uri('ingredients')->readOnly();
|
||||
$relations->hasMany('steps')->readOnly();
|
||||
$relations->hasMany('ingredient-amounts')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('recipe-steps')->relationships(function ($relations) {
|
||||
$relations->hasOne('recipe')->readOnly();
|
||||
|
|
Loading…
Reference in New Issue