feat: add Song search

This commit is contained in:
Phan An 2020-12-24 23:35:39 +01:00
parent a4ef8b0546
commit c9fe724036
5 changed files with 43 additions and 2 deletions

View file

@ -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 [

View 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;
}),
];
}
}

View file

@ -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

View file

@ -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');
});
});