koel/app/Repositories/AlbumRepository.php

93 lines
3 KiB
PHP
Raw Normal View History

2018-08-29 06:30:39 +00:00
<?php
namespace App\Repositories;
use App\Builders\AlbumBuilder;
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
2024-04-24 21:58:19 +00:00
/**
* @extends Repository<Album>
*/
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();
return Album::query()
2022-06-10 10:47:46 +00:00
->isStandard()
->accessibleBy($user)
->unless(
License::isPlus(), // if the license is Plus, accessibleBy() would have already joined with `songs`
static fn (AlbumBuilder $query) => $query->leftJoin('songs', 'albums.id', 'songs.album_id')
)
->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', 'play_count')
2024-01-03 17:02:18 +00:00
->distinct()
2022-06-10 10:47:46 +00:00
->orderByDesc('play_count')
->limit($count)
->get(['albums.*', 'play_count']);
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Album> */
public function getMany(array $ids, bool $preserveOrder = false, ?User $user = null): Collection
2024-01-03 17:02:18 +00:00
{
$albums = Album::query()
->isStandard()
->accessibleBy($user ?? auth()->user())
->whereIn('albums.id', $ids)
->groupBy('albums.id')
->distinct()
->get('albums.*');
return $preserveOrder ? $albums->orderByArray($ids) : $albums;
2024-01-03 17:02:18 +00:00
}
/** @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
}