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;
|
2022-08-09 18:45:11 +00:00
|
|
|
use Illuminate\Contracts\Pagination\Paginator;
|
2022-06-10 10:47:46 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2022-10-11 15:28:43 +00:00
|
|
|
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> */
|
2022-10-11 15:28:43 +00:00
|
|
|
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
|
2018-08-29 09:41:24 +00:00
|
|
|
{
|
2022-10-11 15:28:43 +00:00
|
|
|
$user ??= auth()->user();
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
return Artist::query()
|
2022-10-11 15:28:43 +00:00
|
|
|
->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)
|
2022-10-11 15:28:43 +00:00
|
|
|
->get('artists.*');
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:28:43 +00:00
|
|
|
public function getOne(int $id): Artist
|
2022-06-10 10:47:46 +00:00
|
|
|
{
|
2022-10-11 15:28:43 +00:00
|
|
|
return Artist::query()->find($id);
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return Collection|array<array-key, Artist> */
|
2022-10-11 15:28:43 +00:00
|
|
|
public function getByIds(array $ids): Collection
|
2022-06-10 10:47:46 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return Artist::query()
|
2022-06-10 10:47:46 +00:00
|
|
|
->isStandard()
|
2022-10-11 15:28:43 +00:00
|
|
|
->whereIn('id', $ids)
|
2022-06-10 10:47:46 +00:00
|
|
|
->get();
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
2022-08-09 18:45:11 +00:00
|
|
|
|
2022-10-11 15:28:43 +00:00
|
|
|
public function paginate(): Paginator
|
2022-08-09 18:45:11 +00:00
|
|
|
{
|
|
|
|
return Artist::query()
|
|
|
|
->isStandard()
|
2022-10-11 15:28:43 +00:00
|
|
|
->orderBy('name')
|
2022-08-09 18:45:11 +00:00
|
|
|
->simplePaginate(21);
|
|
|
|
}
|
2018-08-29 06:30:39 +00:00
|
|
|
}
|