2020-12-24 12:41:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Builders\SongBuilder;
|
2020-12-25 11:52:28 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Song;
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Models\User;
|
2020-12-24 12:41:18 +00:00
|
|
|
use App\Repositories\AlbumRepository;
|
|
|
|
use App\Repositories\ArtistRepository;
|
|
|
|
use App\Repositories\SongRepository;
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Values\ExcerptSearchResult;
|
2020-12-25 11:52:28 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2020-12-24 12:41:18 +00:00
|
|
|
|
|
|
|
class SearchService
|
|
|
|
{
|
2020-12-25 11:52:28 +00:00
|
|
|
public const DEFAULT_EXCERPT_RESULT_COUNT = 6;
|
2023-06-05 21:46:41 +00:00
|
|
|
public const DEFAULT_MAX_SONG_RESULT_COUNT = 500;
|
2020-12-25 11:52:28 +00:00
|
|
|
|
2020-12-24 12:41:18 +00:00
|
|
|
public function __construct(
|
2024-04-18 14:36:28 +00:00
|
|
|
private readonly SongRepository $songRepository,
|
|
|
|
private readonly AlbumRepository $albumRepository,
|
|
|
|
private readonly ArtistRepository $artistRepository
|
2020-12-24 12:41:18 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
public function excerptSearch(
|
|
|
|
string $keywords,
|
|
|
|
?User $scopedUser = null,
|
|
|
|
int $count = self::DEFAULT_EXCERPT_RESULT_COUNT
|
|
|
|
): ExcerptSearchResult {
|
|
|
|
$scopedUser ??= auth()->user();
|
2020-12-24 12:41:18 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
return ExcerptSearchResult::make(
|
2024-01-01 20:38:41 +00:00
|
|
|
$this->songRepository->getMany(
|
2024-01-01 11:40:21 +00:00
|
|
|
ids: Song::search($keywords)->get()->take($count)->pluck('id')->all(),
|
|
|
|
inThatOrder: true,
|
|
|
|
scopedUser: $scopedUser
|
2023-06-05 21:46:41 +00:00
|
|
|
),
|
2024-01-01 20:38:41 +00:00
|
|
|
$this->artistRepository->getMany(Artist::search($keywords)->get()->take($count)->pluck('id')->all(), true),
|
|
|
|
$this->albumRepository->getMany(Album::search($keywords)->get()->take($count)->pluck('id')->all(), true),
|
2023-06-05 21:46:41 +00:00
|
|
|
);
|
2020-12-24 12:41:18 +00:00
|
|
|
}
|
2020-12-24 22:35:39 +00:00
|
|
|
|
2023-06-05 21:46:41 +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)
|
|
|
|
->query(static function (SongBuilder $builder) use ($scopedUser, $limit): void {
|
2024-01-03 17:02:18 +00:00
|
|
|
$builder->withMetaFor($scopedUser ?? auth()->user())->limit($limit);
|
2023-06-05 21:46:41 +00:00
|
|
|
})
|
|
|
|
->get();
|
2020-12-24 22:35:39 +00:00
|
|
|
}
|
2020-12-24 12:41:18 +00:00
|
|
|
}
|