2020-12-23 13:04:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API\Search;
|
|
|
|
|
|
|
|
use App\Http\Controllers\API\Controller;
|
|
|
|
use App\Services\SearchService;
|
|
|
|
use Illuminate\Http\Request;
|
2020-12-24 22:35:39 +00:00
|
|
|
use InvalidArgumentException;
|
2020-12-23 13:04:02 +00:00
|
|
|
|
|
|
|
class ExcerptSearchController extends Controller
|
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
private SearchService $searchService;
|
2020-12-23 13:04:02 +00:00
|
|
|
|
2020-12-24 12:41:18 +00:00
|
|
|
public function __construct(SearchService $searchService)
|
2020-12-23 13:04:02 +00:00
|
|
|
{
|
|
|
|
$this->searchService = $searchService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
if (!$request->get('q')) {
|
2020-12-24 22:35:39 +00:00
|
|
|
throw new InvalidArgumentException('A search query is required.');
|
2020-12-23 13:04:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 11:52:28 +00:00
|
|
|
$count = (int) $request->get('count', SearchService::DEFAULT_EXCERPT_RESULT_COUNT);
|
|
|
|
|
|
|
|
if ($count < 0) {
|
|
|
|
throw new InvalidArgumentException('Invalid count parameter.');
|
|
|
|
}
|
|
|
|
|
2020-12-23 13:04:02 +00:00
|
|
|
return [
|
2020-12-25 11:52:28 +00:00
|
|
|
'results' => $this->searchService->excerptSearch($request->get('q'), $count),
|
2020-12-23 13:04:02 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|