Configure basic API paging, sorting, and filtering

This commit is contained in:
Christopher C. Wells 2021-01-23 15:07:49 -08:00
parent 39455af3c9
commit 2e55ea1d98
8 changed files with 87 additions and 83 deletions

View File

@ -11,23 +11,22 @@ class FoodAdapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
* {@inheritdoc}
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
* {@inheritdoc}
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
* {@inheritdoc}
*/
protected $defaultSort = ['name'];
/**
* {@inheritdoc}
*/
public function __construct(StandardStrategy $paging)
{
@ -35,9 +34,7 @@ class FoodAdapter extends AbstractAdapter
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
* {@inheritdoc}
*/
protected function filter($query, Collection $filters)
{

View File

@ -2,54 +2,57 @@
namespace App\JsonApi\Adapters;
use App\Models\IngredientAmount;
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
use CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo;
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class IngredientAmountAdapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
* {@inheritdoc}
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
* {@inheritdoc}
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
* {@inheritdoc}
*/
protected $defaultSort = ['weight'];
/**
* {@inheritdoc}
*/
public function __construct(StandardStrategy $paging)
{
parent::__construct(new \App\Models\IngredientAmount(), $paging);
parent::__construct(new IngredientAmount(), $paging);
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
* {@inheritdoc}
*/
protected function filter($query, Collection $filters)
{
$this->filterWithScopes($query, $filters);
}
/**
* Ingredient relationship.
*/
protected function ingredient(): BelongsTo
{
return $this->belongsTo();
}
/**
* Parent (Recipe or JournalEntry).
*/
protected function parent(): BelongsTo
{
return $this->belongsTo();

View File

@ -6,30 +6,28 @@ 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;
class JournalEntryAdapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
* @inheritdoc
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
* @inheritdoc
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
* {@inheritdoc}
*/
protected $defaultSort = ['-date'];
/**
* @inheritdoc
*/
public function __construct(StandardStrategy $paging)
{
@ -37,28 +35,35 @@ class JournalEntryAdapter extends AbstractAdapter
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
* @inheritdoc
*/
protected function filter($query, Collection $filters)
{
$this->filterWithScopes($query, $filters);
}
/**
* User relationship.
*/
protected function user(): BelongsTo
{
return $this->belongsTo();
}
/**
* Food relationships.
*/
protected function foods(): MorphHasMany
{
return $this->morphMany();
return $this->morphMany($this->hasMany('foods'));
}
/**
* Recipe relationships.
*/
protected function recipes(): MorphHasMany
{
return $this->morphMany();
return $this->morphMany($this->hasMany('recipes'));
}
}

View File

@ -4,31 +4,30 @@ namespace App\JsonApi\Adapters;
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
use CloudCreativity\LaravelJsonApi\Eloquent\HasMany;
use CloudCreativity\LaravelJsonApi\Eloquent\MorphHasMany;
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class RecipeAdapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
* {@inheritdoc}
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
* {@inheritdoc}
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
* {@inheritdoc}
*/
protected $defaultSort = ['name'];
/**
* {@inheritdoc}
*/
public function __construct(StandardStrategy $paging)
{
@ -36,20 +35,24 @@ class RecipeAdapter extends AbstractAdapter
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
* {@inheritdoc}
*/
protected function filter($query, Collection $filters)
{
$this->filterWithScopes($query, $filters);
}
protected function ingredientAmounts(): HasMany
/**
* Ingredient amount relationships.
*/
protected function ingredientAmounts(): MorphHasMany
{
return $this->hasMany();
return $this->morphMany($this->hasMany('ingredientAmounts'));
}
/**
* Step relationships.
*/
protected function steps(): HasMany
{
return $this->hasMany();

View File

@ -12,23 +12,22 @@ class RecipeStepAdapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
* {@inheritdoc}
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
* {@inheritdoc}
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
* {@inheritdoc}
*/
protected $defaultSort = ['number'];
/**
* {@inheritdoc}
*/
public function __construct(StandardStrategy $paging)
{
@ -36,15 +35,16 @@ class RecipeStepAdapter extends AbstractAdapter
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
* {@inheritdoc}
*/
protected function filter($query, Collection $filters)
{
$this->filterWithScopes($query, $filters);
}
/**
* Recipe relationship.
*/
protected function recipe(): BelongsTo
{
return $this->belongsTo();

View File

@ -4,30 +4,28 @@ namespace App\JsonApi\Adapters;
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class UserAdapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
* {@inheritdoc}
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
* {@inheritdoc}
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
* {@inheritdoc}
*/
protected $defaultSort = ['name'];
/**
* {@inheritdoc}
*/
public function __construct(StandardStrategy $paging)
{
@ -35,9 +33,7 @@ class UserAdapter extends AbstractAdapter
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
* {@inheritdoc}
*/
protected function filter($query, Collection $filters)
{

View File

@ -94,7 +94,7 @@ class JournalEntry extends Model
}
/**
* Get all recipes related to this entry.
* Get all foods related to this entry.
*/
public function foods(): MorphToMany {
return $this->morphedByMany(Food::class, 'journalable');

View File

@ -15,7 +15,7 @@ JsonApi::register('v1')->routes(function ($api) {
$api->resource('foods')->readOnly();
$api->resource('ingredient-amounts')->relationships(function ($relations) {
$relations->hasOne('ingredient')->readOnly();
$relations->hasOne('ingredparentient')->readOnly();
$relations->hasOne('parent')->readOnly();
})->readOnly();
$api->resource('journal-entries')->relationships(function ($relations) {
$relations->hasMany('foods')->readOnly();