2018-08-29 13:30:39 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2022-06-10 12:47:46 +02:00
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\User;
|
2020-12-24 13:41:18 +01:00
|
|
|
use App\Repositories\Traits\Searchable;
|
2022-08-09 20:45:11 +02:00
|
|
|
use Illuminate\Contracts\Pagination\Paginator;
|
2022-06-10 12:47:46 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2022-10-11 17:28:43 +02:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2018-08-29 13:30:39 +07:00
|
|
|
|
2022-07-29 08:47:10 +02:00
|
|
|
class ArtistRepository extends Repository
|
2018-08-29 13:30:39 +07:00
|
|
|
{
|
2020-12-24 13:41:18 +01:00
|
|
|
use Searchable;
|
|
|
|
|
2022-06-10 12:47:46 +02:00
|
|
|
/** @return Collection|array<array-key, Artist> */
|
2022-10-11 17:28:43 +02:00
|
|
|
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
|
2018-08-29 16:41:24 +07:00
|
|
|
{
|
2022-10-11 17:28:43 +02:00
|
|
|
$user ??= auth()->user();
|
|
|
|
|
2022-08-09 20:45:11 +02:00
|
|
|
return Artist::query()
|
2022-10-11 17:28:43 +02: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 11:13:54 +02:00
|
|
|
->groupBy(['artists.id', 'play_count'])
|
2022-06-10 12:47:46 +02:00
|
|
|
->isStandard()
|
|
|
|
->orderByDesc('play_count')
|
|
|
|
->limit($count)
|
2022-10-11 17:28:43 +02:00
|
|
|
->get('artists.*');
|
2022-06-10 12:47:46 +02:00
|
|
|
}
|
|
|
|
|
2022-10-11 17:28:43 +02:00
|
|
|
public function getOne(int $id): Artist
|
2022-06-10 12:47:46 +02:00
|
|
|
{
|
2022-10-11 17:28:43 +02:00
|
|
|
return Artist::query()->find($id);
|
2022-06-10 12:47:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return Collection|array<array-key, Artist> */
|
2022-10-11 17:28:43 +02:00
|
|
|
public function getByIds(array $ids): Collection
|
2022-06-10 12:47:46 +02:00
|
|
|
{
|
2022-08-09 20:45:11 +02:00
|
|
|
return Artist::query()
|
2022-06-10 12:47:46 +02:00
|
|
|
->isStandard()
|
2022-10-11 17:28:43 +02:00
|
|
|
->whereIn('id', $ids)
|
2022-06-10 12:47:46 +02:00
|
|
|
->get();
|
2018-08-29 16:41:24 +07:00
|
|
|
}
|
2022-08-09 20:45:11 +02:00
|
|
|
|
2022-10-11 17:28:43 +02:00
|
|
|
public function paginate(): Paginator
|
2022-08-09 20:45:11 +02:00
|
|
|
{
|
|
|
|
return Artist::query()
|
|
|
|
->isStandard()
|
2022-10-11 17:28:43 +02:00
|
|
|
->orderBy('name')
|
2022-08-09 20:45:11 +02:00
|
|
|
->simplePaginate(21);
|
|
|
|
}
|
2018-08-29 13:30:39 +07:00
|
|
|
}
|