Repository navigation
Expand file tree
/
Copy pathProfileSearchController.php
More file actions
47 lines (42 loc) · 1.98 KB
/
Copy pathProfileSearchController.php
File metadata and controls
47 lines (42 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api;
use App\Http\Requests\ListApiProfileSearchRequest;
use App\Http\Resources\OrganizationResource;
use App\Http\Resources\UserResource;
use App\Models\User;
use App\Queries\OrganizationSuggestionsQuery;
use App\Queries\UserSearchQuery;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\JsonResponse;
final readonly class ProfileSearchController
{
/**
* The people and the organizations matching a name or handle, as the web's search finds them. People come with
* whether the user follows each one and whether each one follows them, and people the user muted or blocked, or
* who blocked them, are left out. Organizations are only those that already have a page, with whether the user
* follows each one.
*/
public function index(ListApiProfileSearchRequest $request, #[CurrentUser] User $user, UserSearchQuery $users, OrganizationSuggestionsQuery $organizations): JsonResponse
{
$term = $request->string('q')->value();
return response()->json([
'users' => UserResource::collection(new Collection($users->get($term, $user)->all())->loadExists([
'followers as is_followed' => function (Builder|Relation $query) use ($user): void {
$query->whereKey($user->id);
},
'following as follows_you' => function (Builder|Relation $query) use ($user): void {
$query->whereKey($user->id);
},
])),
'organizations' => OrganizationResource::collection(new Collection($organizations->get($term)->all())->loadExists([
'followers as is_followed' => function (Builder|Relation $query) use ($user): void {
$query->whereKey($user->id);
},
])),
]);
}
}