koel/app/Repositories/AlbumRepository.php

92 lines
2.9 KiB
PHP
Raw Normal View History

2018-08-29 06:30:39 +00:00
<?php
namespace App\Repositories;
2024-01-03 17:02:18 +00:00
use App\Facades\License;
2022-06-10 10:47:46 +00:00
use App\Models\Album;
use App\Models\Artist;
2022-06-10 10:47:46 +00:00
use App\Models\User;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Query\JoinClause;
2022-07-27 15:32:36 +00:00
use Illuminate\Support\Collection;
2018-08-29 06:30:39 +00:00
2022-07-29 06:47:10 +00:00
class AlbumRepository extends Repository
2018-08-29 06:30:39 +00:00
{
2022-06-10 10:47:46 +00:00
/** @return Collection|array<array-key, Album> */
2024-01-03 17:02:18 +00:00
public function getRecentlyAdded(int $count = 6, ?User $user = null): Collection
2022-06-10 10:47:46 +00:00
{
return Album::query()
2022-06-10 10:47:46 +00:00
->isStandard()
2024-01-03 17:02:18 +00:00
->accessibleBy($user ?? $this->auth->user())
->groupBy('albums.id')
->distinct()
->latest('albums.created_at')
2022-06-10 10:47:46 +00:00
->limit($count)
2024-01-03 17:02:18 +00:00
->get('albums.*');
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Album> */
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
2022-06-10 10:47:46 +00:00
{
$user ??= $this->auth->user();
2024-01-03 17:02:18 +00:00
$query = Album::query()
2022-06-10 10:47:46 +00:00
->isStandard()
2024-01-03 17:02:18 +00:00
->accessibleBy($user);
if (License::isCommunity()) {
// if the license is Plus, accessibleBy() would have already joined the songs table
// and we don't want to join it twice
$query->leftJoin('songs', 'albums.id', 'songs.album_id');
}
return $query->join('interactions', static function (JoinClause $join) use ($user): void {
$join->on('songs.id', 'interactions.song_id')->where('interactions.user_id', $user->id);
})
->groupBy('albums.id')
->distinct()
2022-06-10 10:47:46 +00:00
->orderByDesc('play_count')
->limit($count)
->get('albums.*');
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Album> */
2024-01-03 17:02:18 +00:00
public function getMany(array $ids, bool $inThatOrder = false, ?User $user = null): Collection
{
$albums = Album::query()
->isStandard()
->accessibleBy($user ?? auth()->user())
->whereIn('albums.id', $ids)
->groupBy('albums.id')
->distinct()
->get('albums.*');
return $inThatOrder ? $albums->orderByArray($ids) : $albums;
}
/** @return Collection|array<array-key, Album> */
public function getByArtist(Artist $artist, ?User $user = null): Collection
{
return Album::query()
2024-01-03 17:02:18 +00:00
->accessibleBy($user ?? $this->auth->user())
->where('albums.artist_id', $artist->id)
->orWhereIn('albums.id', $artist->songs()->pluck('album_id'))
->orderBy('albums.name')
->groupBy('albums.id')
->distinct()
->get('albums.*');
}
2024-01-03 17:02:18 +00:00
public function paginate(?User $user = null): Paginator
{
return Album::query()
2024-01-03 17:02:18 +00:00
->accessibleBy($user ?? $this->auth->user())
->isStandard()
2024-01-03 17:02:18 +00:00
->orderBy('albums.name')
->groupBy('albums.id')
->distinct()
->select('albums.*')
->simplePaginate(21);
}
2018-08-29 06:30:39 +00:00
}