koel/app/Repositories/ArtistRepository.php

59 lines
1.7 KiB
PHP
Raw Normal View History

2018-08-29 06:30:39 +00:00
<?php
namespace App\Repositories;
2022-06-10 10:47:46 +00:00
use App\Models\Artist;
use App\Models\User;
2020-12-24 12:41:18 +00:00
use App\Repositories\Traits\Searchable;
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
{
2020-12-24 12:41:18 +00:00
use Searchable;
2022-06-10 10:47:46 +00:00
/** @return Collection|array<array-key, Artist> */
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
{
$user ??= auth()->user();
return Artist::query()
->leftJoin('songs', 'artists.id', '=', 'songs.artist_id')
->leftJoin('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',
])
2022-06-10 10:47:46 +00:00
->isStandard()
->orderByDesc('play_count')
->limit($count)
->get('artists.*');
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Artist> */
public function getMany(array $ids, bool $inThatOrder = false): Collection
2022-06-10 10:47:46 +00:00
{
$artists = Artist::query()
2022-06-10 10:47:46 +00:00
->isStandard()
->whereIn('id', $ids)
2022-06-10 10:47:46 +00:00
->get();
return $inThatOrder ? $artists->orderByArray($ids) : $artists;
}
public function paginate(): Paginator
{
return Artist::query()
->isStandard()
->orderBy('name')
->simplePaginate(21);
}
2018-08-29 06:30:39 +00:00
}