koel/app/Http/Controllers/API/Search/ExcerptSearchController.php

28 lines
782 B
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\API\Search;
2022-07-29 06:47:10 +00:00
use App\Http\Controllers\Controller;
use App\Services\SearchService;
use Illuminate\Http\Request;
2020-12-24 22:35:39 +00:00
use InvalidArgumentException;
class ExcerptSearchController extends Controller
{
2022-07-29 06:47:10 +00:00
public function __construct(private SearchService $searchService)
{
}
public function index(Request $request)
{
2022-07-29 06:47:10 +00:00
throw_unless((bool) $request->get('q'), new InvalidArgumentException('A search query is required.'));
$count = (int) $request->get('count', SearchService::DEFAULT_EXCERPT_RESULT_COUNT);
2022-07-29 06:47:10 +00:00
throw_if($count < 0, new InvalidArgumentException('Invalid count parameter.'));
return [
'results' => $this->searchService->excerptSearch($request->get('q'), $count),
];
}
}