koel/app/Repositories/ArtistRepository.php

73 lines
2.2 KiB
PHP
Raw Normal View History

2018-08-29 06:30:39 +00:00
<?php
namespace App\Repositories;
2024-01-03 17:02:18 +00:00
use App\Facades\License;
2022-06-10 10:47:46 +00:00
use App\Models\Artist;
use App\Models\User;
use Illuminate\Contracts\Pagination\Paginator;
2022-06-10 10:47:46 +00:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Query\JoinClause;
2018-08-29 06:30:39 +00:00
2022-07-29 06:47:10 +00:00
class ArtistRepository extends Repository
2018-08-29 06:30:39 +00:00
{
2022-06-10 10:47:46 +00:00
/** @return Collection|array<array-key, Artist> */
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
{
2024-01-07 12:43:10 +00:00
/** @var ?User $user */
$user ??= auth()->user();
2024-01-03 17:02:18 +00:00
$query = Artist::query()
->isStandard()
->accessibleBy($user);
if (License::isCommunity()) {
// if the license is Plus, accessibleBy() would have already joined the songs table
// and we don't want to join it twice
$query->leftJoin('songs', 'artists.id', 'songs.artist_id');
}
return $query->join('interactions', static function (JoinClause $join) use ($user): void {
$join->on('interactions.song_id', '=', 'songs.id')->where('interactions.user_id', $user->id);
})
2022-12-02 23:34:51 +00:00
->groupBy([
'artists.id',
'play_count',
'artists.name',
'artists.image',
'artists.created_at',
'artists.updated_at',
])
2024-01-03 17:02:18 +00:00
->distinct()
2022-06-10 10:47:46 +00:00
->orderByDesc('play_count')
->limit($count)
->get('artists.*');
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Artist> */
2024-01-03 17:02:18 +00:00
public function getMany(array $ids, bool $inThatOrder = false, ?User $user = null): Collection
2022-06-10 10:47:46 +00:00
{
$artists = Artist::query()
2022-06-10 10:47:46 +00:00
->isStandard()
2024-01-03 17:02:18 +00:00
->accessibleBy($user ?? auth()->user())
->whereIn('artists.id', $ids)
->groupBy('artists.id')
->distinct()
->get('artists.*');
return $inThatOrder ? $artists->orderByArray($ids) : $artists;
}
2024-01-03 17:02:18 +00:00
public function paginate(?User $user = null): Paginator
{
return Artist::query()
->isStandard()
2024-01-03 17:02:18 +00:00
->accessibleBy($user ?? auth()->user())
->groupBy('artists.id')
->distinct()
->orderBy('artists.name')
->select('artists.*')
->simplePaginate(21);
}
2018-08-29 06:30:39 +00:00
}