Skip to content

Commit 34ebfb9

Browse files
authored
sort has one (#438)
* fix: Sorting by has one * fix: Return null for hasMany and belongsToMany * Fix styling Co-authored-by: binaryk <[email protected]>
1 parent a26b5dd commit 34ebfb9

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

src/Fields/BelongsToMany.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Closure;
1010
use Illuminate\Auth\Access\AuthorizationException;
1111
use Illuminate\Http\Request;
12-
use Illuminate\Support\Facades\Gate;
1312

1413
class BelongsToMany extends EagerField
1514
{
@@ -64,11 +63,8 @@ public function resolve($repository, $attribute = null)
6463
->resolveFromPivot($item->pivot)
6564
)
6665
->eagerState();
67-
} catch (AuthorizationException $e) {
68-
$class = get_class($item);
69-
$policy = get_class(Gate::getPolicyFor($item));
70-
71-
abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]");
66+
} catch (AuthorizationException) {
67+
return null;
7268
}
7369
});
7470

src/Fields/HasMany.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Binaryk\LaravelRestify\Repositories\Repository;
88
use Illuminate\Auth\Access\AuthorizationException;
99
use Illuminate\Http\Request;
10-
use Illuminate\Support\Facades\Gate;
1110

1211
class HasMany extends EagerField
1312
{
@@ -46,11 +45,8 @@ public function resolve($repository, $attribute = null)
4645
return $this->repositoryClass::resolveWith($item)
4746
->allowToShow(app(Request::class))
4847
->eagerState();
49-
} catch (AuthorizationException $e) {
50-
$class = get_class($item);
51-
$policy = get_class(Gate::getPolicyFor($item));
52-
53-
abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]");
48+
} catch (AuthorizationException) {
49+
return null;
5450
}
5551
});
5652

src/Repositories/Repository.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,15 @@ public function resolveRelationships($request): array
515515
return $collection->forIndex($request, $this);
516516
})
517517
->mapIntoRelated($request)
518-
->map(function (Related $related) use ($request) {
519-
return $related->resolve($request, $this)->getValue();
520-
})->all();
518+
->map(fn (Related $related) => $related->resolve($request, $this)->getValue())
519+
->map(function (mixed $items) {
520+
if ($items instanceof Collection) {
521+
return $items->filter();
522+
}
523+
524+
return $items;
525+
})
526+
->all();
521527
}
522528

523529
/**

tests/Fields/HasManyTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Binaryk\LaravelRestify\Tests\IntegrationTest;
1414
use Illuminate\Support\Collection;
1515
use Illuminate\Support\Facades\Gate;
16+
use Illuminate\Testing\Fluent\AssertableJson;
1617

1718
class HasManyTest extends IntegrationTest
1819
{
@@ -36,7 +37,7 @@ protected function tearDown(): void
3637
Repository::clearResolvedInstances();
3738
}
3839

39-
public function test_has_many_present_on_relations()
40+
public function test_has_many_present_on_relations(): void
4041
{
4142
$user = User::factory()->create();
4243

@@ -59,7 +60,7 @@ public function test_has_many_present_on_relations()
5960
]);
6061
}
6162

62-
public function test_has_many_paginated_on_relation()
63+
public function test_has_many_paginated_on_relation(): void
6364
{
6465
$user = tap($this->mockUsers()->first(), function ($user) {
6566
$this->mockPosts($user->id, 22);
@@ -69,7 +70,7 @@ public function test_has_many_paginated_on_relation()
6970
->assertJsonCount(20, 'data.relationships.posts');
7071
}
7172

72-
public function test_has_many_unauthorized_see_relationship_posts()
73+
public function test_has_many_filter_unauthorized_to_see_relationship_posts(): void
7374
{
7475
$_SERVER['restify.post.show'] = false;
7576

@@ -79,7 +80,8 @@ public function test_has_many_unauthorized_see_relationship_posts()
7980
});
8081

8182
$this->getJson(UserWithPosts::uriKey()."/$user->id?related=posts")
82-
->assertForbidden();
83+
->assertOk()
84+
->assertJson(fn (AssertableJson $json) => $json->count('data.relationships.posts', 0)->etc());
8385
}
8486

8587
public function test_field_ignored_when_storing()
@@ -107,7 +109,7 @@ public function test_can_display_other_pages()
107109
field('email'),
108110
field('password'),
109111

110-
HasMany::make('posts', PostRepository::class),
112+
HasMany::make('posts', PostRepository::class),
111113
]);
112114

113115
$this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?perPage=5")
@@ -131,7 +133,7 @@ public function test_can_apply_filters(): void
131133
field('email'),
132134
field('password'),
133135

134-
HasMany::make('posts', PostRepository::class),
136+
HasMany::make('posts', PostRepository::class),
135137
]);
136138

137139
$this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?title=wew")
@@ -155,7 +157,7 @@ public function test_filter_unauthorized_posts()
155157
field('email'),
156158
field('password'),
157159

158-
HasMany::make('posts', PostRepository::class),
160+
HasMany::make('posts', PostRepository::class),
159161
]);
160162

161163
$this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts")
@@ -183,7 +185,7 @@ public function test_can_store()
183185
field('email'),
184186
field('password'),
185187

186-
HasMany::make('posts', PostRepository::class),
188+
HasMany::make('posts', PostRepository::class),
187189
]);
188190

189191
$this->postJson(UserWithPosts::uriKey()."/{$u->id}/posts", [
@@ -204,7 +206,7 @@ public function test_can_show(): void
204206
field('email'),
205207
field('password'),
206208

207-
HasMany::make('posts', PostRepository::class),
209+
HasMany::make('posts', PostRepository::class),
208210
]);
209211

210212
$this->getJson(UserWithPosts::to("$userId/posts/$post->id"), [
@@ -300,7 +302,7 @@ class UserWithPosts extends Repository
300302
public static function related(): array
301303
{
302304
return [
303-
'posts' => HasMany::make('posts', PostRepository::class),
305+
'posts' => HasMany::make('posts', PostRepository::class),
304306
];
305307
}
306308

tests/Fields/MorphOneFieldTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ protected function tearDown(): void
3030
Repository::clearResolvedInstances();
3131
}
3232

33-
public function test_morph_one_present_on_show_when_specified_related()
33+
public function test_morph_one_present_on_show_when_specified_related(): void
3434
{
3535
$post = Post::factory()->create([
3636
'user_id' => User::factory(),
3737
]);
3838

39-
$relationships = $this->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user")
39+
$relationships = $this
40+
->withoutExceptionHandling()
41+
->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user")
4042
->assertJsonStructure([
4143
'data' => [
4244
'relationships' => [

0 commit comments

Comments
 (0)