From c426330cac8b4816bd208066d23621f12697bd97 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Fri, 4 Mar 2022 11:17:03 +0200 Subject: [PATCH 1/6] feat: adding custom fields for belongs to relationships --- src/Fields/EagerField.php | 1 + tests/Fields/BelongsToFieldTest.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Fields/EagerField.php b/src/Fields/EagerField.php index dcf8209c6..d72dbb13d 100644 --- a/src/Fields/EagerField.php +++ b/src/Fields/EagerField.php @@ -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)) { diff --git a/tests/Fields/BelongsToFieldTest.php b/tests/Fields/BelongsToFieldTest.php index 443b13b64..cff44254f 100644 --- a/tests/Fields/BelongsToFieldTest.php +++ b/tests/Fields/BelongsToFieldTest.php @@ -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 { @@ -207,6 +208,25 @@ 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 From 9e6cd176dac2e91ecc3ca25a67a6b43381fb3b0e Mon Sep 17 00:00:00 2001 From: binaryk Date: Fri, 4 Mar 2022 09:17:25 +0000 Subject: [PATCH 2/6] Fix styling --- tests/Fields/BelongsToFieldTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Fields/BelongsToFieldTest.php b/tests/Fields/BelongsToFieldTest.php index cff44254f..dda664634 100644 --- a/tests/Fields/BelongsToFieldTest.php +++ b/tests/Fields/BelongsToFieldTest.php @@ -221,7 +221,8 @@ public function test_belongs_to_could_choose_columns(): void $this->getJson(PostRepository::to($post->id, [ 'include' => 'user[name]', - ]))->assertJson(fn(AssertableJson $json) => $json + ]))->assertJson( + fn (AssertableJson $json) => $json ->has('data.relationships.user.attributes.name') ->missing('data.relationships.user.attributes.email') ->etc() From 0409cea58acbc14901d36651ab976e1323dfe6b9 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Fri, 4 Mar 2022 11:18:30 +0200 Subject: [PATCH 3/6] fix: allow belongs to many choose columns --- src/Fields/BelongsToMany.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 15c0bed6b..70727a552 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -57,6 +57,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) From 17be060e9a17d17fce9bbf60cd9e40137a77e6bd Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Fri, 4 Mar 2022 11:19:35 +0200 Subject: [PATCH 4/6] fix: wip --- src/Fields/BelongsToMany.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 70727a552..d4dc1e8e3 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -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 { From f1281b1063f28bf70056ecc917f678ee68c6a01f Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Fri, 4 Mar 2022 11:29:20 +0200 Subject: [PATCH 5/6] fix: tests --- src/Http/Requests/GetterRequest.php | 13 +++++++------ src/Repositories/ResolvesGetters.php | 2 +- tests/Fixtures/Post/Getters/PostsIndexGetter.php | 2 ++ tests/Fixtures/Post/Getters/PostsShowGetter.php | 3 +-- tests/Getters/PerformGetterControllerTest.php | 2 -- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Http/Requests/GetterRequest.php b/src/Http/Requests/GetterRequest.php index 9280a5f70..3a15ec40f 100644 --- a/src/Http/Requests/GetterRequest.php +++ b/src/Http/Requests/GetterRequest.php @@ -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.' @@ -38,9 +38,10 @@ 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', [])); }) diff --git a/src/Repositories/ResolvesGetters.php b/src/Repositories/ResolvesGetters.php index 3fcb45a4a..c59031e5e 100644 --- a/src/Repositories/ResolvesGetters.php +++ b/src/Repositories/ResolvesGetters.php @@ -8,7 +8,7 @@ trait ResolvesGetters { - public function availableGetters(GetterRequest $request) + public function availableGetters(GetterRequest $request): Collection { $getters = $request->isForRepositoryRequest() ? $this->resolveShowGetters($request) diff --git a/tests/Fixtures/Post/Getters/PostsIndexGetter.php b/tests/Fixtures/Post/Getters/PostsIndexGetter.php index 50f6aee52..215fa1310 100644 --- a/tests/Fixtures/Post/Getters/PostsIndexGetter.php +++ b/tests/Fixtures/Post/Getters/PostsIndexGetter.php @@ -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); diff --git a/tests/Fixtures/Post/Getters/PostsShowGetter.php b/tests/Fixtures/Post/Getters/PostsShowGetter.php index da27366ce..1efce55ab 100644 --- a/tests/Fixtures/Post/Getters/PostsShowGetter.php +++ b/tests/Fixtures/Post/Getters/PostsShowGetter.php @@ -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'); } diff --git a/tests/Getters/PerformGetterControllerTest.php b/tests/Getters/PerformGetterControllerTest.php index b77f9c1ea..d709ccaaa 100644 --- a/tests/Getters/PerformGetterControllerTest.php +++ b/tests/Getters/PerformGetterControllerTest.php @@ -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') @@ -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 From 8b1a7a32525cf001671c5d0f8cd12f57632910fd Mon Sep 17 00:00:00 2001 From: binaryk Date: Fri, 4 Mar 2022 09:29:44 +0000 Subject: [PATCH 6/6] Fix styling --- src/Http/Requests/GetterRequest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Http/Requests/GetterRequest.php b/src/Http/Requests/GetterRequest.php index 3a15ec40f..5ea5994c3 100644 --- a/src/Http/Requests/GetterRequest.php +++ b/src/Http/Requests/GetterRequest.php @@ -38,10 +38,12 @@ protected function getterExists(): bool public function builder(Getter $getter, int $size): Builder { - return tap(RepositorySearchService::make()->search($this, $this->repository()), + 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', [])); })