2022-06-10 10:47:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\V6;
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
use App\Builders\SongBuilder;
|
2022-06-10 10:47:46 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\AlbumRepository;
|
|
|
|
use App\Repositories\ArtistRepository;
|
|
|
|
use App\Repositories\SongRepository;
|
|
|
|
use App\Values\ExcerptSearchResult;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
class SearchService
|
|
|
|
{
|
|
|
|
public const DEFAULT_EXCERPT_RESULT_COUNT = 6;
|
|
|
|
public const DEFAULT_MAX_SONG_RESULT_COUNT = 500;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
private SongRepository $songRepository,
|
|
|
|
private AlbumRepository $albumRepository,
|
|
|
|
private ArtistRepository $artistRepository
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function excerptSearch(
|
|
|
|
string $keywords,
|
|
|
|
?User $scopedUser = null,
|
|
|
|
int $count = self::DEFAULT_EXCERPT_RESULT_COUNT
|
|
|
|
): ExcerptSearchResult {
|
|
|
|
$scopedUser ??= auth()->user();
|
|
|
|
|
|
|
|
return ExcerptSearchResult::make(
|
|
|
|
$this->songRepository->getByIds(
|
2022-07-05 14:19:52 +00:00
|
|
|
Song::search($keywords)->get()->take($count)->pluck('id')->all(),
|
2022-06-10 10:47:46 +00:00
|
|
|
$scopedUser
|
|
|
|
),
|
2022-10-11 15:28:43 +00:00
|
|
|
$this->artistRepository->getByIds(Artist::search($keywords)->get()->take($count)->pluck('id')->all()),
|
|
|
|
$this->albumRepository->getByIds(Album::search($keywords)->get()->take($count)->pluck('id')->all()),
|
2022-06-10 10:47:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return Collection|array<array-key, Song> */
|
|
|
|
public function searchSongs(
|
|
|
|
string $keywords,
|
|
|
|
?User $scopedUser = null,
|
|
|
|
int $limit = self::DEFAULT_MAX_SONG_RESULT_COUNT
|
|
|
|
): Collection {
|
|
|
|
return Song::search($keywords)
|
2022-08-09 18:45:11 +00:00
|
|
|
->query(static function (SongBuilder $builder) use ($scopedUser, $limit): void {
|
2022-06-10 10:47:46 +00:00
|
|
|
$builder->withMeta($scopedUser ?? auth()->user())->limit($limit);
|
|
|
|
})
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
}
|