Skip to content

Commit d56ddbe

Browse files
committed
Use user repository to update the user profile
1 parent f3e94f8 commit d56ddbe

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

src/Http/Controllers/ProfileUpdateController.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Binaryk\LaravelRestify\Http\Controllers;
44

55
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6+
use Binaryk\LaravelRestify\Repositories\Repository;
7+
use Illuminate\Support\Facades\Auth;
68
use Illuminate\Support\Facades\Hash;
79

810
class ProfileUpdateController extends RepositoryController
@@ -11,19 +13,38 @@ public function __invoke(RestifyRequest $request)
1113
{
1214
$user = $request->user();
1315

14-
$request->validate([
15-
'email' => 'sometimes|required|unique:users,email,'.$user->id,
16-
'password' => 'sometimes|required|min:5|confirmed',
17-
]);
18-
19-
if ($request->has('password')) {
20-
$request->merge([
21-
'password' => Hash::make($request->get('password')),
16+
if ($repository = $this->guessRepository($request)) {
17+
$repository->allowToUpdate($request)->update($request, Auth::id());
18+
} else {
19+
$request->validate([
20+
'email' => 'sometimes|required|unique:users,email,'.$user->id,
21+
'password' => 'sometimes|required|min:5|confirmed',
2222
]);
23-
}
2423

25-
$user->update($request->only($user->getFillable()));
24+
if ($request->has('password')) {
25+
$request->merge([
26+
'password' => Hash::make($request->get('password')),
27+
]);
28+
}
29+
30+
$user->update($request->only($user->getFillable()));
31+
}
2632

2733
return $this->response()->data($user->fresh());
2834
}
35+
36+
public function guessRepository(RestifyRequest $request): ?Repository
37+
{
38+
$repository = $request->repository('users');
39+
40+
if (! $repository) {
41+
return null;
42+
}
43+
44+
$repository->withResource(
45+
$repository::query($request)->whereKey(Auth::id())->first()
46+
);
47+
48+
return $repository;
49+
}
2950
}

tests/Controllers/ProfileControllerTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function test_profile_returns_authenticated_user_with_related_posts()
3636
{
3737
$response = $this->getJson('/restify-api/profile?related=posts')
3838
->assertStatus(200)
39-
4039
->assertJsonStructure([
4140
'data',
4241
]);
@@ -65,6 +64,7 @@ public function test_profile_update()
6564
'email' => '[email protected]',
6665
'name' => 'Eduard',
6766
])
67+
->dump()
6868
->assertStatus(200);
6969

7070
$response->assertJsonFragment([
@@ -95,7 +95,8 @@ public function test_profile_update_unique_email()
9595
$this->putJson('restify-api/profile', [
9696
'email' => '[email protected]',
9797
'name' => 'Eduard',
98-
])->assertStatus(400);
98+
])
99+
->assertStatus(400);
99100
}
100101

101102
public function test_profile_upload_avatar()
@@ -107,4 +108,21 @@ public function test_profile_upload_avatar()
107108
])
108109
->assertStatus(200);
109110
}
111+
112+
public function test_profile_validation_from_repository()
113+
{
114+
$this->putJson('/restify-api/profile', [
115+
'email' => '[email protected]',
116+
'name' => 'Ed',
117+
])
118+
->dump()
119+
->assertStatus(400)
120+
->assertJsonStructure([
121+
'errors' => [
122+
[
123+
'name'
124+
]
125+
]
126+
]);
127+
}
110128
}

tests/Fixtures/User/UserRepository.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Binaryk\LaravelRestify\Tests\Fixtures\User;
44

5+
use Binaryk\LaravelRestify\Fields\Field;
56
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
use Binaryk\LaravelRestify\Repositories\Mergeable;
68
use Binaryk\LaravelRestify\Repositories\Repository;
79

810
/**
911
* @author Eduard Lupacescu <[email protected]>
1012
*/
11-
class UserRepository extends Repository
13+
class UserRepository extends Repository implements Mergeable
1214
{
1315
public static $model = User::class;
1416

@@ -20,6 +22,11 @@ class UserRepository extends Repository
2022
public function fields(RestifyRequest $request)
2123
{
2224
return [
25+
Field::new('name')->rules('sometimes', 'nullable', 'min:4'),
26+
27+
Field::new('email')->rules('required', 'unique:users'),
28+
29+
Field::new('password'),
2330
];
2431
}
2532

0 commit comments

Comments
 (0)