2018-08-29 06:30:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2024-01-11 22:14:22 +00:00
|
|
|
use App\Builders\ArtistBuilder;
|
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;
|
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;
|
2024-04-18 11:27:07 +00:00
|
|
|
use Illuminate\Support\Collection as BaseCollection;
|
2018-08-29 06:30:39 +00:00
|
|
|
|
2024-04-24 21:58:19 +00:00
|
|
|
/** @extends Repository<Artist> */
|
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> */
|
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();
|
|
|
|
|
2024-01-11 22:14:22 +00:00
|
|
|
return Artist::query()
|
2024-01-03 17:02:18 +00:00
|
|
|
->isStandard()
|
2024-01-11 22:14:22 +00:00
|
|
|
->accessibleBy($user)
|
|
|
|
->unless(
|
|
|
|
License::isPlus(), // if the license is Plus, accessibleBy() would have already joined with `songs`
|
|
|
|
static fn (ArtistBuilder $query) => $query->leftJoin('songs', 'artists.id', 'songs.artist_id')
|
|
|
|
)
|
|
|
|
->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)
|
2024-07-14 18:49:40 +00:00
|
|
|
->get(['artists.*', 'play_count']);
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return Collection|array<array-key, Artist> */
|
2024-05-31 05:40:34 +00:00
|
|
|
public function getMany(array $ids, bool $preserveOrder = false, ?User $user = null): Collection|BaseCollection
|
2022-06-10 10:47:46 +00:00
|
|
|
{
|
2024-01-01 11:40:21 +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.*');
|
2024-01-01 11:40:21 +00:00
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
return $preserveOrder ? $artists->orderByArray($ids) : $artists;
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
2022-08-09 18:45:11 +00:00
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
public function paginate(?User $user = null): Paginator
|
2022-08-09 18:45:11 +00:00
|
|
|
{
|
|
|
|
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.*')
|
2022-08-09 18:45:11 +00:00
|
|
|
->simplePaginate(21);
|
|
|
|
}
|
2018-08-29 06:30:39 +00:00
|
|
|
}
|