Skip to content

Commit 5c08d92

Browse files
authored
Handle action handle when query chunk is zero. (#339)
* Handle action handle when query chunk is zero. * Apply fixes from StyleCI (#340)
1 parent 7d9c53c commit 5c08d92

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/Actions/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class Action extends RestController implements JsonSerializable
4444

4545
public static function indexQuery(RestifyRequest $request, $query)
4646
{
47-
return $query;
47+
//
4848
}
4949

5050
/**

src/Http/Requests/ActionRequest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binaryk\LaravelRestify\Actions\Action;
66
use Binaryk\LaravelRestify\Services\Search\RepositorySearchService;
77
use Closure;
8+
use Illuminate\Database\Eloquent\Builder;
89
use Illuminate\Support\Collection;
910

1011
class ActionRequest extends RestifyRequest
@@ -32,17 +33,26 @@ protected function actionExists()
3233
});
3334
}
3435

36+
public function builder(Action $action, int $size): Builder
37+
{
38+
return tap(RepositorySearchService::instance()->search($this, $this->repository()), function ($query) use ($action) {
39+
$action::indexQuery($this, $query);
40+
})
41+
->when($this->input('repositories') !== 'all', function ($query) {
42+
$query->whereKey($this->input('repositories', []));
43+
})
44+
->latest($this->model()->getKeyName());
45+
}
46+
3547
public function collectRepositories(Action $action, $count, Closure $callback)
3648
{
3749
$output = [];
3850

39-
tap(RepositorySearchService::instance()->search($this, $this->repository()), function ($query) use ($action) {
40-
$action::indexQuery($this, $query);
41-
})
42-
->when($this->input('repositories') !== 'all', function ($query) {
43-
$query->whereKey($this->input('repositories', []));
44-
})->latest($this->model()->getKeyName())
45-
->chunk($count, function ($chunk) use ($callback, &$output) {
51+
if (($query = $this->builder($action, $count))->count() === 0) {
52+
$output[] = $callback(Collection::make([]));
53+
}
54+
55+
$query->chunk($count, function ($chunk) use ($callback, &$output) {
4656
$output[] = $callback(Collection::make($chunk));
4757
});
4858

tests/Actions/PerformActionsControllerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Binaryk\LaravelRestify\Tests\Actions;
44

5+
use Binaryk\LaravelRestify\Actions\Action;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
57
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PublishPostAction;
68
use Binaryk\LaravelRestify\Tests\Fixtures\User\ActivateAction;
79
use Binaryk\LaravelRestify\Tests\Fixtures\User\DisableProfileAction;
810
use Binaryk\LaravelRestify\Tests\IntegrationTest;
11+
use Illuminate\Http\Request;
12+
use Illuminate\Support\Collection;
913

1014
class PerformActionsControllerTest extends IntegrationTest
1115
{
@@ -35,6 +39,32 @@ public function test_could_perform_action_for_multiple_repositories()
3539
$this->assertEquals(1, PublishPostAction::$applied[0][1]->id);
3640
}
3741

42+
public function test_could_perform_action_using_all()
43+
{
44+
$this->assertDatabaseCount('posts', 0);
45+
46+
PostRepository::partialMock()
47+
->shouldReceive('actions')
48+
->andReturn([
49+
new class extends Action {
50+
public static $uriKey = 'publish';
51+
52+
public function handle(Request $request, Collection $collection)
53+
{
54+
return response()->json([
55+
'fromHandle' => $collection->count(),
56+
]);
57+
}
58+
},
59+
]);
60+
61+
$this->post('posts/action?action=publish', [
62+
'repositories' => 'all',
63+
])->assertOk()->assertJsonFragment([
64+
'fromHandle' => 0,
65+
]);
66+
}
67+
3868
public function test_cannot_apply_a_show_action_to_index()
3969
{
4070
$post = $this->mockPosts(1, 2);

0 commit comments

Comments
 (0)