2020-12-24 13:41:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2023-06-05 23:46:41 +02:00
|
|
|
use App\Builders\SongBuilder;
|
2020-12-25 12:52:28 +01:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
2024-05-31 13:40:34 +08:00
|
|
|
use App\Models\Podcast;
|
2020-12-25 12:52:28 +01:00
|
|
|
use App\Models\Song;
|
2023-06-05 23:46:41 +02:00
|
|
|
use App\Models\User;
|
2020-12-24 13:41:18 +01:00
|
|
|
use App\Repositories\AlbumRepository;
|
|
|
|
use App\Repositories\ArtistRepository;
|
2024-05-19 13:49:42 +08:00
|
|
|
use App\Repositories\PodcastRepository;
|
2020-12-24 13:41:18 +01:00
|
|
|
use App\Repositories\SongRepository;
|
2023-06-05 23:46:41 +02:00
|
|
|
use App\Values\ExcerptSearchResult;
|
2020-12-25 12:52:28 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2020-12-24 13:41:18 +01:00
|
|
|
|
|
|
|
class SearchService
|
|
|
|
{
|
2020-12-25 12:52:28 +01:00
|
|
|
public const DEFAULT_EXCERPT_RESULT_COUNT = 6;
|
2023-06-05 23:46:41 +02:00
|
|
|
public const DEFAULT_MAX_SONG_RESULT_COUNT = 500;
|
2020-12-25 12:52:28 +01:00
|
|
|
|
2020-12-24 13:41:18 +01:00
|
|
|
public function __construct(
|
2024-04-18 16:36:28 +02:00
|
|
|
private readonly SongRepository $songRepository,
|
|
|
|
private readonly AlbumRepository $albumRepository,
|
2024-05-19 13:49:42 +08:00
|
|
|
private readonly ArtistRepository $artistRepository,
|
|
|
|
private readonly PodcastRepository $podcastRepository
|
2020-12-24 13:41:18 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2023-06-05 23:46:41 +02:00
|
|
|
public function excerptSearch(
|
|
|
|
string $keywords,
|
|
|
|
?User $scopedUser = null,
|
|
|
|
int $count = self::DEFAULT_EXCERPT_RESULT_COUNT
|
|
|
|
): ExcerptSearchResult {
|
|
|
|
$scopedUser ??= auth()->user();
|
2020-12-24 13:41:18 +01:00
|
|
|
|
2023-06-05 23:46:41 +02:00
|
|
|
return ExcerptSearchResult::make(
|
2024-05-19 13:49:42 +08:00
|
|
|
songs: $this->songRepository->getMany(
|
2024-01-01 12:40:21 +01:00
|
|
|
ids: Song::search($keywords)->get()->take($count)->pluck('id')->all(),
|
2024-05-31 13:40:34 +08:00
|
|
|
preserveOrder: true,
|
2024-01-01 12:40:21 +01:00
|
|
|
scopedUser: $scopedUser
|
2023-06-05 23:46:41 +02:00
|
|
|
),
|
2024-05-19 13:49:42 +08:00
|
|
|
artists: $this->artistRepository->getMany(
|
|
|
|
ids: Artist::search($keywords)->get()->take($count)->pluck('id')->all(),
|
2024-05-31 13:40:34 +08:00
|
|
|
preserveOrder: true
|
2024-05-19 13:49:42 +08:00
|
|
|
),
|
|
|
|
albums: $this->albumRepository->getMany(
|
|
|
|
ids: Album::search($keywords)->get()->take($count)->pluck('id')->all(),
|
2024-05-31 13:40:34 +08:00
|
|
|
preserveOrder: true
|
2024-05-19 13:49:42 +08:00
|
|
|
),
|
|
|
|
podcasts: $this->podcastRepository->getMany(
|
|
|
|
ids: Podcast::search($keywords)->get()->take($count)->pluck('id')->all(),
|
2024-05-31 13:40:34 +08:00
|
|
|
preserveOrder: true
|
2024-05-19 13:49:42 +08:00
|
|
|
),
|
2023-06-05 23:46:41 +02:00
|
|
|
);
|
2020-12-24 13:41:18 +01:00
|
|
|
}
|
2020-12-24 23:35:39 +01:00
|
|
|
|
2023-06-05 23:46:41 +02: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)
|
2024-06-05 17:49:23 +02:00
|
|
|
->query(
|
|
|
|
static fn (SongBuilder $builder) => $builder
|
|
|
|
->forUser($scopedUser ?? auth()->user())
|
|
|
|
->withMeta()
|
|
|
|
->limit($limit)
|
|
|
|
)
|
2023-06-05 23:46:41 +02:00
|
|
|
->get();
|
2020-12-24 23:35:39 +01:00
|
|
|
}
|
2020-12-24 13:41:18 +01:00
|
|
|
}
|