Deploy Ladill Hosting / deploy (push) Successful in 20s
Wire a JSON /search endpoint and Alpine dropdown so customers can find accounts, linked domains, and orders from the hosting app header. Co-authored-by: Cursor <cursoragent@cursor.com>
105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Hosting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\CustomerHostingOrder;
|
|
use App\Models\HostingAccount;
|
|
use App\Models\HostingAccountMember;
|
|
use App\Models\HostingProduct;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class SearchController extends Controller
|
|
{
|
|
public function __invoke(Request $request): JsonResponse
|
|
{
|
|
$q = trim((string) $request->query('q', ''));
|
|
|
|
if (mb_strlen($q) < 2) {
|
|
return response()->json(['results' => []]);
|
|
}
|
|
|
|
return response()->json([
|
|
'results' => $this->resultsForUser($request->user(), $q),
|
|
]);
|
|
}
|
|
|
|
/** @return list<array{type:string,title:string,subtitle:string,url:string}> */
|
|
private function resultsForUser($user, string $q): array
|
|
{
|
|
$like = '%'.$q.'%';
|
|
$results = [];
|
|
$seenUrls = [];
|
|
|
|
$push = function (array $item) use (&$results, &$seenUrls): void {
|
|
if (isset($seenUrls[$item['url']])) {
|
|
return;
|
|
}
|
|
$seenUrls[$item['url']] = true;
|
|
$results[] = $item;
|
|
};
|
|
|
|
$sharedTypes = [
|
|
HostingProduct::TYPE_SINGLE_DOMAIN,
|
|
HostingProduct::TYPE_MULTI_DOMAIN,
|
|
HostingProduct::TYPE_WORDPRESS,
|
|
];
|
|
|
|
$sharedAccountIds = HostingAccountMember::query()
|
|
->where('user_id', $user->id)
|
|
->pluck('hosting_account_id');
|
|
|
|
HostingAccount::query()
|
|
->where(function ($query) use ($user, $sharedAccountIds) {
|
|
$query->where('user_id', $user->id);
|
|
if ($sharedAccountIds->isNotEmpty()) {
|
|
$query->orWhereIn('id', $sharedAccountIds);
|
|
}
|
|
})
|
|
->whereHas('product', fn ($query) => $query->whereIn('type', $sharedTypes))
|
|
->where(function ($query) use ($like) {
|
|
$query->where('username', 'like', $like)
|
|
->orWhere('primary_domain', 'like', $like)
|
|
->orWhereHas('sites', fn ($sites) => $sites->where('domain', 'like', $like));
|
|
})
|
|
->with(['product', 'sites'])
|
|
->orderByDesc('updated_at')
|
|
->limit(8)
|
|
->get()
|
|
->each(function (HostingAccount $account) use ($push, $user): void {
|
|
$sharedDeveloperAccess = (int) $account->user_id !== (int) $user->id;
|
|
$push([
|
|
'type' => 'hosting',
|
|
'title' => $account->linkedDomainLabel() ?? $account->username,
|
|
'subtitle' => ($account->product?->name ?? 'Hosting').' · '.ucfirst((string) $account->status),
|
|
'url' => $sharedDeveloperAccess
|
|
? route('hosting.panel.index', $account)
|
|
: route('hosting.accounts.show', $account),
|
|
]);
|
|
});
|
|
|
|
CustomerHostingOrder::query()
|
|
->forUser($user->id)
|
|
->whereHas('product', fn ($query) => $query->whereIn('type', $sharedTypes))
|
|
->where(function ($query) use ($like) {
|
|
$query->where('domain_name', 'like', $like)
|
|
->orWhere('server_ip', 'like', $like);
|
|
})
|
|
->with('product')
|
|
->orderByDesc('updated_at')
|
|
->limit(5)
|
|
->get()
|
|
->each(function (CustomerHostingOrder $order) use ($push): void {
|
|
$push([
|
|
'type' => 'order',
|
|
'title' => $order->domain_name ?: ($order->product?->name ?? 'Hosting order #'.$order->id),
|
|
'subtitle' => 'Hosting order · '.ucwords(str_replace('_', ' ', (string) $order->status)),
|
|
'url' => route('hosting.orders.show', $order),
|
|
]);
|
|
});
|
|
|
|
return array_slice($results, 0, 12);
|
|
}
|
|
}
|