mirror of https://github.com/kcal-app/kcal.git
Re-enable User API endpoint
This commit is contained in:
parent
5537e6d5e4
commit
923e39aec4
|
|
@ -4,6 +4,7 @@ namespace App\JsonApi\Adapters;
|
|||
|
||||
use App\Models\User;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\HasMany;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
|
|
@ -41,4 +42,12 @@ class UserAdapter extends AbstractAdapter
|
|||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step relationships.
|
||||
*/
|
||||
protected function journalEntries(): HasMany
|
||||
{
|
||||
return $this->hasMany();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,4 +32,20 @@ class UserSchema extends SchemaProvider
|
|||
'updatedAt' => $resource->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'journal-entries' => [
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['journal-entries']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->ingredientAmounts;
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,6 +74,13 @@ final class User extends Authenticatable
|
|||
return $this->hasMany(Goal::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User's journal entries.
|
||||
*/
|
||||
public function journalEntries(): HasMany {
|
||||
return $this->hasMany(JournalEntry::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get User's past, present, and future goals.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ JsonApi::register('v1')->routes(function ($api) {
|
|||
$relations->hasOne('recipe')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('tags')->readOnly();
|
||||
// $api->resource('users')->relationships(function ($relations) {
|
||||
// $relations->hasMany('journal-entries')->readOnly();
|
||||
// })->readOnly();
|
||||
$api->resource('users')->relationships(function ($relations) {
|
||||
$relations->hasMany('journal-entries')->readOnly();
|
||||
})->readOnly();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\JsonApi;
|
||||
|
||||
use App\Models\User;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class UserApiTest extends JsonApiTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function factory(): UserFactory
|
||||
{
|
||||
return User::factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function resourceName(): string
|
||||
{
|
||||
return 'users';
|
||||
}
|
||||
|
||||
public function testCanGetIndex(): void
|
||||
{
|
||||
// Initial user created by test so only make 9 new instances.
|
||||
$this->createInstances(9);
|
||||
$index_url = route($this->indexRouteName);
|
||||
$response = $this->get($index_url);
|
||||
$response->assertOk();
|
||||
$response->assertJson(['data' => true]);
|
||||
$response->assertJsonCount(10, 'data');
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue