Skip to content

Commit 1ab844c

Browse files
7.x (#486)
* fix: support ukraine * Clean up & upgrading php + laravel (#464) * fix: cleaning the repository method * Fix styling * fix: wip * fix: wip * fix: wip * fix: wip * fix: wip * fix: wip * feat: drop laravel 8 support * fix: wip * Fix styling * fix: wip * Fix styling * fix: refactoring matches * fix: wip * Fix styling * fix: wip * fix: wip * Larastan (#461) * fix: support ukraine * adding larastan * wip * Fix styling * Fix styling * fix: wip * Fix styling * fix: wip * fix: php 8.1 * fix: cover windows tests Co-authored-by: binaryk <[email protected]> * fix: delete unused Co-authored-by: binaryk <[email protected]> * fix: route method * Fix styling * fix: roadmap * fix: Make sure any action isn't permitted unless the Model Policy exists * fix: wip * Fix styling * fix: Block requests without policy (#466) * fix: Block requests without policy * Fix styling * fix: wip * fix: wip * fix: wip * fix: tests * fix: config Co-authored-by: binaryk <[email protected]> * fix: Adding package tools and fixing related bug (#467) * Tests 2 (#468) * fix: use route helper * Fix styling * fix: tests refactoring * Fix styling * fix: route key * fix: wip * Fix styling * fix: phpunit config * fix: coverage * fix: factories * Fix styling * fix: wip * fix: wip * Fix styling Co-authored-by: binaryk <[email protected]> * fix: wip * fix: wip * fix: wip * Custom logs (#469) * fix: assertables & prototypes * Fix styling * fix: observer for logs * Fix styling * fix: wip * Fix styling * fix: custom logs * fix: wip * Fix styling * fix: sideeffect * Fix styling * fix: wip Co-authored-by: binaryk <[email protected]> * With eager loading (#470) * fix: adding with for the show * fix: cleanup responses using helper * Fix styling Co-authored-by: binaryk <[email protected]> * fix: List restify routes through artisan command (#471) * fix: List restify routes through artisan command * Fix styling * fix: wip * fix: tests Co-authored-by: binaryk <[email protected]> * fix: wip * fix: helpers for action logs * Fix styling * fix: fix eager fields without key in include * fix: wip * fix: custom serializer (#472) * fix: custom serializer * Fix styling * fix: serializer docs Co-authored-by: binaryk <[email protected]> * Fix styling * Relationships nested (#473) * fix: eager loading nested relationships * Fix styling * fix: wip * fix: eager loading nested relationships * fix: wip Co-authored-by: binaryk <[email protected]> * fix: don't check related if no query (#475) * Fix after bulk method calls (#476) * fix after bulk method calls * fix number of calls * revert returns * Fix styling * Keep model attributes for deletedBulk method. (#477) * fix after bulk method calls * fix number of calls * revert returns * keep model attributes * fix: fix psalm and tests (#478) * fix: fix psalm and tests * fix: wip * fix: wip * Performance (#474) * fix: catch issues * Fix styling * fix: ensure eager loading works * Fix styling * fix: performance improvements * Fix styling * fix: related performance improvements * Fix styling * fix: drop cast support * Fix styling * fix: optimize current repository search Co-authored-by: binaryk <[email protected]> * Recursive related (#479) * fix: collection * Fix styling * fix: tests * fix: wip * Fix styling * fix: recursive * Fix styling * fix: recursive related including columns * fix: typo * Fix styling Co-authored-by: binaryk <[email protected]> * Feedback related (#480) * fix: feedback from related pr * Fix styling * fix: wip * fix: wip * Fix styling Co-authored-by: binaryk <[email protected]> * Dynamic meta (#481) * fix: configurable meta render * Fix styling Co-authored-by: binaryk <[email protected]> * fix: formatting (#482) * Docs 7x (#483) * fix: repository generator improvements & sanctum middleware scaffolding * Fix styling Co-authored-by: binaryk <[email protected]> * Docs 7x (#484) * fix: repository generator improvements & sanctum middleware scaffolding * feat: [7.x] Eager fields recognize pattern of the key to retrieve repository. * Fix styling * fix: updating docs for v7 * fix: merge * Fix styling * fix: wip * fix: wip * Fix styling Co-authored-by: binaryk <[email protected]> * Last formatting (#485) * fix: wip * fix: wip Co-authored-by: binaryk <[email protected]> Co-authored-by: Daniel Bănciulea <[email protected]>
1 parent 3ce98e0 commit 1ab844c

File tree

5 files changed

+174
-9
lines changed

5 files changed

+174
-9
lines changed

src/Http/Controllers/RepositoryDestroyBulkController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ class RepositoryDestroyBulkController
1010
{
1111
public function __invoke(RepositoryDestroyBulkRequest $request)
1212
{
13-
$collection = DB::transaction(function () use ($request) {
13+
$repositories = collect();
14+
15+
DB::transaction(function () use ($request, $repositories) {
1416
return $request->collect()
15-
->each(function (int|string $key, int $row) use ($request) {
17+
->each(function (int|string $key, int $row) use ($request, $repositories) {
1618
$model = $request->modelQuery($key)->lockForUpdate()->firstOrFail();
1719

20+
$repositories->push($model->attributesToArray());
21+
1822
/**
1923
* @var Repository $repository
2024
*/
@@ -30,6 +34,8 @@ public function __invoke(RepositoryDestroyBulkRequest $request)
3034
});
3135
});
3236

37+
$request->repository()::deletedBulk($repositories, $request);
38+
3339
return ok();
3440
}
3541
}

src/Http/Controllers/RepositoryUpdateBulkController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function __invoke(RepositoryUpdateBulkRequest $request)
3030
});
3131
});
3232

33+
$request->repository()::savedBulk($collection, $request);
34+
$request->repository()::updatedBulk($collection, $request);
35+
3336
return $this->response()
3437
->success();
3538
}

src/Repositories/Repository.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ public function resolveIndexPivots(RestifyRequest $request): array
530530
*/
531531
public function resolveRelationships($request): array
532532
{
533+
if (! $request->related()->hasRelated()) {
534+
return [];
535+
}
536+
533537
return static::collectRelated()
534538
->forRequest($request, $this)
535539
->mapIntoRelated($request, $this)
@@ -726,6 +730,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request)
726730
});
727731
});
728732

733+
static::savedBulk($entities, $request);
729734
static::storedBulk($entities, $request);
730735

731736
return data($entities);
@@ -808,14 +813,12 @@ public function updateBulk(RestifyRequest $request, $repositoryId, int $row)
808813
->authorizedUpdateBulk($request)
809814
->each(fn (Field $field) => $field->actionHandler->handle($request, $this->resource, $row));
810815

811-
static::updatedBulk($this->resource, $request);
812-
813-
return ok();
816+
return response()->json();
814817
}
815818

816819
public function deleteBulk(RestifyRequest $request, $repositoryId, int $row)
817820
{
818-
$status = DB::transaction(function () use ($request) {
821+
DB::transaction(function () use ($request) {
819822
if (in_array(HasActionLogs::class, class_uses_recursive($this->resource))) {
820823
Restify::actionLog()
821824
->forRepositoryDestroy($this->resource, $request->user())
@@ -825,8 +828,6 @@ public function deleteBulk(RestifyRequest $request, $repositoryId, int $row)
825828
return $this->resource->delete();
826829
});
827830

828-
static::deleted($status, $request);
829-
830831
return ok(code: 204);
831832
}
832833

@@ -986,7 +987,17 @@ public static function storedBulk(Collection $repositories, $request)
986987
//
987988
}
988989

989-
public static function updatedBulk($model, $request)
990+
public static function updatedBulk(Collection $repositories, $request)
991+
{
992+
//
993+
}
994+
995+
public static function savedBulk(Collection $repositories, $request)
996+
{
997+
//
998+
}
999+
1000+
public static function deletedBulk(Collection $repositories, $request)
9901001
{
9911002
//
9921003
}

tests/Fixtures/User/UserPolicy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public function storeBulk($user)
3939
return $_SERVER['restify.users.storeBulk'] ?? true;
4040
}
4141

42+
public function updateBulk($user)
43+
{
44+
return $_SERVER['restify.users.updateBulk'] ?? true;
45+
}
46+
47+
public function deleteBulk($user)
48+
{
49+
return $_SERVER['restify.users.deleteBulk'] ?? true;
50+
}
51+
4252
public function update($user, $model)
4353
{
4454
return $_SERVER['restify.users.update'] ?? true;
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Unit;
4+
5+
use Binaryk\LaravelRestify\Restify;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
8+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
9+
use Illuminate\Support\Collection;
10+
11+
class RepositoryAfterBulkTest extends IntegrationTest
12+
{
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
Restify::repositories([
18+
WithAfterBulkOverrides::class,
19+
]);
20+
}
21+
22+
public function test_it_calls_the_overriden_stored_bulk_method(): void
23+
{
24+
$user = User::factory()->make();
25+
26+
$this->postJson(WithAfterBulkOverrides::route().'/bulk', [
27+
[
28+
'name' => $user->name,
29+
'email' => '[email protected]',
30+
'password' => $user->password,
31+
],
32+
])->assertSuccessful();
33+
34+
$this->assertEquals('[email protected]', $user->first()->email);
35+
}
36+
37+
public function test_it_calls_the_overriden_updated_bulk_method(): void
38+
{
39+
$user = User::factory()->create();
40+
41+
$this->postJson(WithAfterBulkOverrides::route().'/bulk/update', [
42+
[
43+
'id' => $user->id,
44+
'email' => '[email protected]',
45+
],
46+
])->assertSuccessful();
47+
48+
$this->assertEquals('[email protected]', $user->fresh()->email);
49+
}
50+
51+
public function test_it_calls_the_overriden_saved_bulk_method_for_create(): void
52+
{
53+
$user = User::factory()->make();
54+
55+
$this->postJson(WithAfterBulkOverrides::route().'/bulk', [
56+
[
57+
'name' => $user->name,
58+
'email' => '[email protected]',
59+
'password' => $user->password,
60+
],
61+
])->assertSuccessful();
62+
63+
$this->assertEquals('John Saved', $user->first()->name);
64+
}
65+
66+
public function test_it_calls_the_overriden_saved_bulk_method_for_update(): void
67+
{
68+
$user = User::factory()->create();
69+
70+
$this->postJson(WithAfterBulkOverrides::route().'/bulk/update', [
71+
[
72+
'id' => $user->id,
73+
'email' => '[email protected]',
74+
],
75+
])->assertSuccessful();
76+
77+
$this->assertEquals('John Saved', $user->fresh()->name);
78+
}
79+
80+
public function test_it_calls_the_overriden_deleted_bulk_method(): void
81+
{
82+
$user = User::factory()->create();
83+
84+
$this->deleteJson(WithAfterBulkOverrides::route().'/bulk/delete', [
85+
$user->id,
86+
])->assertSuccessful();
87+
88+
$this->assertDatabaseMissing(User::class, ['id' => $user->id]);
89+
90+
$this->assertDatabaseHas(User::class, [
91+
'email' => $user->email,
92+
'name' => $user->name,
93+
]);
94+
}
95+
}
96+
97+
class WithAfterBulkOverrides extends UserRepository
98+
{
99+
public static function storedBulk(Collection $repositories, $request)
100+
{
101+
$user = User::find($repositories->first()['id']);
102+
103+
$user->update([
104+
'email' => '[email protected]',
105+
]);
106+
}
107+
108+
public static function updatedBulk(Collection $repositories, $request)
109+
{
110+
$user = User::find($repositories->first()['id']);
111+
112+
$user->update([
113+
'email' => '[email protected]',
114+
]);
115+
}
116+
117+
public static function savedBulk(Collection $repositories, $request)
118+
{
119+
$user = User::find($repositories->first()['id']);
120+
121+
$user->update([
122+
'name' => 'John Saved',
123+
]);
124+
}
125+
126+
public static function deletedBulk(Collection $repositories, $request)
127+
{
128+
$first = $repositories->first();
129+
130+
User::factory()->create([
131+
'email' => $first['email'],
132+
'name' => $first['name'],
133+
]);
134+
}
135+
}

0 commit comments

Comments
 (0)