mirror of https://github.com/kcal-app/kcal.git
Add Goals API endpoint
This commit is contained in:
parent
1776d5c83b
commit
0ec83db285
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\JsonApi\Adapters;
|
||||
|
||||
use App\Models\Goal;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class GoalAdapter extends AbstractAdapter
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $filterScopes = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $defaultSort = ['-from', '-to'];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __construct(StandardStrategy $paging)
|
||||
{
|
||||
parent::__construct(new Goal(), $paging);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function filter($query, Collection $filters)
|
||||
{
|
||||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
protected function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -42,9 +42,11 @@ class UserAdapter extends AbstractAdapter
|
|||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step relationships.
|
||||
*/
|
||||
protected function goals(): HasMany
|
||||
{
|
||||
return $this->hasMany();
|
||||
}
|
||||
|
||||
protected function journalEntries(): HasMany
|
||||
{
|
||||
return $this->hasMany();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace App\JsonApi\Schemas;
|
||||
|
||||
use Neomerx\JsonApi\Schema\SchemaProvider;
|
||||
|
||||
class GoalSchema extends SchemaProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $resourceType = 'goals';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId($resource): string
|
||||
{
|
||||
return (string) $resource->getRouteKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes($resource): array
|
||||
{
|
||||
return [
|
||||
'frequency' => $resource->frequency,
|
||||
'from' => $resource->from,
|
||||
'goal' => $resource->goal,
|
||||
'name' => $resource->name,
|
||||
'to' => $resource->to,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,13 @@ class UserSchema extends SchemaProvider
|
|||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'goals' => [
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['goals']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->ingredientAmounts;
|
||||
},
|
||||
],
|
||||
'journal-entries' => [
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['journal-entries']),
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ return [
|
|||
*/
|
||||
'resources' => [
|
||||
'foods' => \App\Models\Food::class,
|
||||
'goals' => \App\Models\Goal::class,
|
||||
'ingredient-amounts' => \App\Models\IngredientAmount::class,
|
||||
'media' => \Spatie\MediaLibrary\MediaCollections\Models\Media::class,
|
||||
'journal-entries' => \App\Models\JournalEntry::class,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@ JsonApi::register('v1')->routes(function ($api) {
|
|||
$api->resource('foods')->relationships(function ($relations) {
|
||||
$relations->hasMany('tags')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('goals')->relationships(function ($relations) {
|
||||
$relations->hasOne('user')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('ingredient-amounts')->relationships(function ($relations) {
|
||||
$relations->hasOne('ingredient')->readOnly();
|
||||
$relations->hasOne('parent')->readOnly();
|
||||
|
|
@ -42,6 +45,7 @@ JsonApi::register('v1')->routes(function ($api) {
|
|||
})->readOnly();
|
||||
$api->resource('tags')->readOnly();
|
||||
$api->resource('users')->relationships(function ($relations) {
|
||||
$relations->hasMany('goals')->readOnly();
|
||||
$relations->hasMany('journal-entries')->readOnly();
|
||||
})->readOnly();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\Goal;
|
||||
use App\Models\User;
|
||||
use Database\Factories\GoalFactory;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class GoalApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): GoalFactory
|
||||
{
|
||||
return Goal::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'goals';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function createInstances(int $count = 1): Collection {
|
||||
return User::factory()->count(1)->hasGoals($count)->create();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue