mirror of
https://github.com/koel/koel
synced 2024-11-24 13:13:05 +00:00
63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\V6;
|
|
|
|
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\Contracts\Database\Query\Builder;
|
|
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(
|
|
Song::search($keywords)->take($count)->get()->pluck('id')->all(),
|
|
$scopedUser
|
|
),
|
|
$this->artistRepository->getByIds(
|
|
Artist::search($keywords)->take($count)->get()->pluck('id')->all(),
|
|
$scopedUser
|
|
),
|
|
$this->albumRepository->getByIds(
|
|
Album::search($keywords)->take($count)->get()->pluck('id')->all(),
|
|
$scopedUser
|
|
),
|
|
);
|
|
}
|
|
|
|
/** @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)
|
|
->query(static function (Builder $builder) use ($scopedUser, $limit): void {
|
|
$builder->withMeta($scopedUser ?? auth()->user())->limit($limit);
|
|
})
|
|
->get();
|
|
}
|
|
}
|