Skip to content

Commit e77fd50

Browse files
committed
fix: fix user profile controller
1 parent 59db1f0 commit e77fd50

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

src/Http/Controllers/ProfileController.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ class ProfileController extends RepositoryController
1414
public function __invoke(ProfileRequestRequest $request): JsonResponse
1515
{
1616
if ($repository = $this->guessRepository($request)) {
17+
return $request->repositoryWith(tap($request->modelQuery(Auth::id(), 'users'),
18+
fn($query) => $repository::showQuery(
19+
$request,
20+
$repository::mainQuery($request,
21+
$query->with($repository::collectWiths($request, $repository)->all()))
22+
))->with($repository::collectWiths($request, $repository)->all())->firstOrFail(), 'users')
23+
->allowToShow($request)
24+
->show($request, Auth::id());
25+
1726
return data($repository->serializeForShow($request));
1827
}
1928

@@ -28,19 +37,11 @@ public function guessRepository(RestifyRequest $request): ?Repository
2837
return null;
2938
}
3039

31-
if (method_exists($repository, 'canUseForProfile')) {
32-
if (! call_user_func([$repository, 'canUseForProfile'], $request)) {
33-
return null;
34-
}
40+
if (method_exists($repository, 'canUseForProfile') && ! call_user_func([$repository, 'canUseForProfile'],
41+
$request)) {
42+
return null;
3543
}
3644

37-
$user = tap(RepositorySearchService::make()->search(
38-
$request,
39-
$repository
40-
), function ($query) use ($request, $repository) {
41-
$repository::indexQuery($request, $query);
42-
})->whereKey(Auth::id())->firstOrFail();
43-
44-
return $repository->withResource($user);
45+
return $repository;
4546
}
4647
}

src/Http/Requests/Concerns/InteractWithRepositories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function viaQuery(): Relation
9191
public function modelQuery(?string $repositoryId = null, ?string $uriKey = null): Builder|Relation
9292
{
9393
return $this->newQuery($uriKey)->where(
94-
$this->model()->getRouteKeyName(),
94+
$this->model($uriKey)->getRouteKeyName(),
9595
$repositoryId ?? $this->route('repositoryId')
9696
);
9797
}

tests/Controllers/ProfileControllerTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ protected function setUp(): void
3030

3131
public function test_profile_returns_authenticated_user(): void
3232
{
33+
$this->withoutExceptionHandling();
34+
3335
$response = $this->getJson(Restify::path('profile'))
3436
->assertOk()
3537
->assertJsonStructure([
@@ -46,16 +48,11 @@ public function test_profile_returns_authenticated_user_with_related_posts(): vo
4648
$this->getJson(Restify::path('profile', [
4749
'related' => 'posts',
4850
]))
49-
->assertOk()
50-
->assertJsonStructure([
51-
'data' => [
52-
'posts' => [
53-
[
54-
'title',
55-
],
56-
],
57-
],
58-
]);
51+
->assertJson(fn($json) => $json
52+
->where('data.attributes.email', $this->authenticatedAs->email)
53+
->has('data.relationships.posts')
54+
->etc()
55+
);
5956
}
6057

6158
public function test_profile_update()

tests/Fixtures/User/UserRepository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
77
use Binaryk\LaravelRestify\Repositories\Repository;
88
use Binaryk\LaravelRestify\Repositories\UserProfile;
9+
use Illuminate\Http\Request;
910

1011
class UserRepository extends Repository
1112
{
1213
use UserProfile;
1314

15+
public static function canUseForProfile(Request $request): bool
16+
{
17+
return true;
18+
}
19+
1420
public static string $model = User::class;
1521

1622
public static bool $wasBooted = false;

0 commit comments

Comments
 (0)