Skip to content

Commit 2c9c8a8

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

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/Http/Controllers/ProfileController.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
use Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest;
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
use Binaryk\LaravelRestify\Repositories\Repository;
8+
use Binaryk\LaravelRestify\Services\Search\RepositorySearchService;
79

810
class ProfileController extends RepositoryController
911
{
1012
public function __invoke(RestifyRequest $request)
1113
{
14+
if ($repository = $this->guessRepository($request)) {
15+
return $repository;
16+
}
17+
1218
$user = $request->user();
1319

1420
if (isset($user->{ProfileAvatarRequest::$userAvatarAttribute})) {
@@ -22,11 +28,28 @@ public function __invoke(RestifyRequest $request)
2228
$meta = [];
2329

2430
if (method_exists($user, 'profile')) {
25-
$meta = (array) call_user_func([$user, 'profile'], $request);
31+
$meta = (array)call_user_func([$user, 'profile'], $request);
2632
}
2733

2834
return $this->response()
2935
->data($user)
3036
->meta($meta);
3137
}
38+
39+
public function guessRepository(RestifyRequest $request): ?Repository
40+
{
41+
$repository = $request->repository('users');
42+
43+
if (!$repository) {
44+
return null;
45+
}
46+
47+
$user = tap(RepositorySearchService::instance()->search(
48+
$request, $repository
49+
), function ($query) use ($request, $repository) {
50+
$repository::indexQuery($request, $query);
51+
})->firstOrFail();
52+
53+
return $repository->withResource($user);
54+
}
3255
}

src/Services/Search/RepositorySearchService.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function search(RestifyRequest $request, Repository $repository)
1919

2020
$query = $this->applyFilters($request, $repository, $query);
2121

22-
return tap($this->prepareRelations($request, $this->prepareOrders($request, $query), $this->fixedInput), $this->applyIndexQuery($request, $repository));
22+
return tap($this->prepareOrders($request, $query), $this->applyIndexQuery($request, $repository));
2323
}
2424

2525
public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
@@ -30,7 +30,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
3030
continue;
3131
}
3232

33-
$value = $request->get($key, data_get($extra, "match.$key"));
33+
$value = $request->input($key, data_get($extra, "match.$key"));
3434

3535
if (empty($value)) {
3636
continue;
@@ -70,7 +70,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
7070

7171
public function prepareOrders(RestifyRequest $request, $query, $extra = [])
7272
{
73-
$orderings = explode(',', $request->get('sort', ''));
73+
$orderings = explode(',', $request->input('sort', ''));
7474

7575
if (isset($extra['sort'])) {
7676
$orderings = $extra['sort'];
@@ -93,7 +93,7 @@ public function prepareOrders(RestifyRequest $request, $query, $extra = [])
9393

9494
public function prepareRelations(RestifyRequest $request, $query, $extra = [])
9595
{
96-
$relations = array_merge($extra, explode(',', $request->get('related')));
96+
$relations = array_merge($extra, explode(',', $request->input('related')));
9797

9898
foreach ($relations as $relation) {
9999
if (in_array($relation, $this->repository->getWiths())) {
@@ -106,7 +106,7 @@ public function prepareRelations(RestifyRequest $request, $query, $extra = [])
106106

107107
public function prepareSearchFields(RestifyRequest $request, $query, $extra = [])
108108
{
109-
$search = $request->get('search', data_get($extra, 'search', ''));
109+
$search = $request->input('search', data_get($extra, 'search', ''));
110110

111111
if (empty($search)) {
112112
return $query;

tests/Controllers/ProfileControllerTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function test_profile_returns_authenticated_user()
2424
$response = $this->getJson('/restify-api/profile')
2525
->assertStatus(200)
2626
->assertJsonStructure([
27-
'data',
27+
'attributes',
2828
]);
2929

3030
$response->assertJsonFragment([
@@ -37,7 +37,7 @@ public function test_profile_returns_authenticated_user_with_related_posts()
3737
$response = $this->getJson('/restify-api/profile?related=posts')
3838
->assertStatus(200)
3939
->assertJsonStructure([
40-
'data',
40+
'attributes',
4141
]);
4242

4343
$response->assertJsonFragment([
@@ -51,10 +51,7 @@ public function test_profile_returns_authenticated_user_with_meta_profile_data()
5151
$this->getJson('/restify-api/profile')
5252
->assertStatus(200)
5353
->assertJsonStructure([
54-
'data',
55-
'meta' => [
56-
'roles',
57-
],
54+
'meta',
5855
]);
5956
}
6057

@@ -64,7 +61,6 @@ public function test_profile_update()
6461
'email' => '[email protected]',
6562
'name' => 'Eduard',
6663
])
67-
->dump()
6864
->assertStatus(200);
6965

7066
$response->assertJsonFragment([
@@ -115,7 +111,6 @@ public function test_profile_validation_from_repository()
115111
'email' => '[email protected]',
116112
'name' => 'Ed',
117113
])
118-
->dump()
119114
->assertStatus(400)
120115
->assertJsonStructure([
121116
'errors' => [

tests/Fixtures/User/UserRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @author Eduard Lupacescu <[email protected]>
1212
*/
13-
class UserRepository extends Repository implements Mergeable
13+
class UserRepository extends Repository
1414
{
1515
public static $model = User::class;
1616

@@ -19,6 +19,10 @@ class UserRepository extends Repository implements Mergeable
1919
'name',
2020
];
2121

22+
public static $related = [
23+
'posts',
24+
];
25+
2226
public function fields(RestifyRequest $request)
2327
{
2428
return [

0 commit comments

Comments
 (0)