koel/app/Repositories/ArtistRepository.php

55 lines
1.5 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-10-23 09:13:54 +00:00
->groupBy(['artists.id', 'play_count'])
2022-06-10 10:47:46 +00:00
->isStandard()
->orderByDesc('play_count')
->limit($count)
->get('artists.*');
2022-06-10 10:47:46 +00:00
}
public function getOne(int $id): Artist
2022-06-10 10:47:46 +00:00
{
return Artist::query()->find($id);
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Artist> */
public function getByIds(array $ids): Collection
2022-06-10 10:47:46 +00:00
{
return Artist::query()
2022-06-10 10:47:46 +00:00
->isStandard()
->whereIn('id', $ids)
2022-06-10 10:47:46 +00:00
->get();
}
public function paginate(): Paginator
{
return Artist::query()
->isStandard()
->orderBy('name')
->simplePaginate(21);
}
2018-08-29 06:30:39 +00:00
}