Skip to content

Commit

Permalink
feat: adding custom fields for belongs to relationships (#450)
Browse files Browse the repository at this point in the history
* feat: adding custom fields for belongs to relationships

* Fix styling

* fix: allow belongs to many choose columns

* fix: wip

* fix: tests

* Fix styling

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Mar 4, 2022
1 parent 8cb3e69 commit 3062103
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function __construct($relation, $parentRepository)

public function resolve($repository, $attribute = null)
{
/**
* @var Repository $repository
*/
if ($repository->model()->relationLoaded($this->relation)) {
$paginator = $repository->model()->getRelation($this->relation);
} else {
Expand All @@ -57,6 +60,7 @@ public function resolve($repository, $attribute = null)
try {
return $this->repositoryClass::resolveWith($item)
->allowToShow(app(Request::class))
->columns($this->getColumns())
->withPivots(
PivotsCollection::make($this->pivotFields)
->map(fn (Field $field) => clone $field)
Expand Down
1 change: 1 addition & 0 deletions src/Fields/EagerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function resolve($repository, $attribute = null)
try {
$this->value = $this->repositoryClass::resolveWith($relatedModel)
->allowToShow(app(Request::class))
->columns($this->getColumns())
->eagerState();
} catch (AuthorizationException $e) {
if (is_null($relatedModel)) {
Expand Down
15 changes: 9 additions & 6 deletions src/Http/Requests/GetterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public function getter(): Getter
{
return once(function () {
return $this->availableGetters()->first(function ($getter) {
dd($this->route('getter') ?? $this->query('getter'));

return $this->route('getter') ?? $this->query('getter') === $getter->uriKey();
return $this->route('getter')
? $this->route('getter') === $getter->uriKey()
: $this->query('getter') === $getter->uriKey();
}) ?: abort(
$this->getterExists() ? 403 : 404,
'Getter does not exists or you don\'t have enough permissions to perform it.'
Expand All @@ -38,9 +38,12 @@ protected function getterExists(): bool

public function builder(Getter $getter, int $size): Builder
{
return tap(RepositorySearchService::make()->search($this, $this->repository()), function ($query) use ($getter) {
$getter::indexQuery($this, $query);
})
return tap(
RepositorySearchService::make()->search($this, $this->repository()),
function ($query) use ($getter) {
$getter::indexQuery($this, $query);
}
)
->when($this->input('repositories') !== 'all', function ($query) {
$query->whereKey($this->input('repositories', []));
})
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/ResolvesGetters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

trait ResolvesGetters
{
public function availableGetters(GetterRequest $request)
public function availableGetters(GetterRequest $request): Collection
{
$getters = $request->isForRepositoryRequest()
? $this->resolveShowGetters($request)
Expand Down
21 changes: 21 additions & 0 deletions tests/Fields/BelongsToFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
use Binaryk\LaravelRestify\Tests\IntegrationTest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Testing\Fluent\AssertableJson;

class BelongsToFieldTest extends IntegrationTest
{
Expand Down Expand Up @@ -207,6 +208,26 @@ public function test_unauthorized_via_policy_when_updating()
$this->assertSame($post->fresh()->user->id, $firstOwnerId);
});
}

public function test_belongs_to_could_choose_columns(): void
{
$post = PostFactory::one();

PostRepository::partialMock()
->shouldReceive('related')
->andReturn([
'user' => BelongsTo::make('user', UserRepository::class),
]);

$this->getJson(PostRepository::to($post->id, [
'include' => 'user[name]',
]))->assertJson(
fn (AssertableJson $json) => $json
->has('data.relationships.user.attributes.name')
->missing('data.relationships.user.attributes.email')
->etc()
);
}
}

class PostWithUserRepository extends Repository
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/Post/Getters/PostsIndexGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class PostsIndexGetter extends Getter
{
public static $uriKey = 'posts-index-getter';

public function handle(Request $request): JsonResponse
{
return ok('it works', 200);
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Post/Getters/PostsShowGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use Binaryk\LaravelRestify\Getters\Getter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PostsShowGetter extends Getter
{
public static $uriKey = 'posts-show-getter';

public function handle(Request $request, ?Model $model = null): JsonResponse
public function handle(Request $request, ?Model $model = null)
{
return ok('show works');
}
Expand Down
2 changes: 0 additions & 2 deletions tests/Getters/PerformGetterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function test_could_perform_repository_getter(): void

$this
->getJson(PostRepository::getter(PostsShowGetter::class, 1))
->assertSuccessful()
->assertJson(
fn (AssertableJson $json) => $json
->where('message', 'show works')
Expand All @@ -49,7 +48,6 @@ public function test_unauthenticated_user_can_access_middleware_when_except_auth
$this
->withoutExceptionHandling()
->getJson(PostRepository::getter(UnauthenticatedActionGetter::class))
->dump()
->assertSuccessful()
->assertJson(
fn (AssertableJson $json) => $json
Expand Down

0 comments on commit 3062103

Please sign in to comment.