mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
feat: add Song search
This commit is contained in:
parent
a4ef8b0546
commit
c9fe724036
5 changed files with 43 additions and 2 deletions
|
@ -5,6 +5,7 @@ namespace App\Http\Controllers\API\Search;
|
|||
use App\Http\Controllers\API\Controller;
|
||||
use App\Services\SearchService;
|
||||
use Illuminate\Http\Request;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class ExcerptSearchController extends Controller
|
||||
{
|
||||
|
@ -18,7 +19,7 @@ class ExcerptSearchController extends Controller
|
|||
public function index(Request $request)
|
||||
{
|
||||
if (!$request->get('q')) {
|
||||
return ['results' => []];
|
||||
throw new InvalidArgumentException('A search query is required.');
|
||||
}
|
||||
|
||||
return [
|
||||
|
|
34
app/Http/Controllers/API/Search/SongSearchController.php
Normal file
34
app/Http/Controllers/API/Search/SongSearchController.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API\Search;
|
||||
|
||||
use App\Http\Controllers\API\Controller;
|
||||
use App\Models\Song;
|
||||
use App\Services\SearchService;
|
||||
use Illuminate\Http\Request;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class SongSearchController extends Controller
|
||||
{
|
||||
private $searchService;
|
||||
|
||||
public function __construct(SearchService $searchService)
|
||||
{
|
||||
$this->searchService = $searchService;
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
if (!$request->get('q')) {
|
||||
throw new InvalidArgumentException('A search query is required.');
|
||||
}
|
||||
|
||||
return [
|
||||
'songs' => $this->searchService->searchSongs($request->get('q'))
|
||||
->get()
|
||||
->map(static function (Song $song): string {
|
||||
return $song->id;
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -40,4 +40,9 @@ class SearchService
|
|||
{
|
||||
return $query->take($count)->get();
|
||||
}
|
||||
|
||||
public function searchSongs(string $keywords): Builder
|
||||
{
|
||||
return $this->songRepository->search($keywords);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit dbdcc595ba5f625c291ce6042a7e230d0273ded3
|
||||
Subproject commit 5d69f9965b9bc8196562db9646764ca15abb6fa5
|
|
@ -78,6 +78,7 @@ Route::group(['namespace' => 'API'], static function (): void {
|
|||
|
||||
Route::group(['namespace' => 'Search', 'prefix' => 'search'], static function (): void {
|
||||
Route::get('/', 'ExcerptSearchController@index');
|
||||
Route::get('songs', 'SongSearchController@index');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue