koel/app/Repositories/AlbumRepository.php

61 lines
1.7 KiB
PHP
Raw Normal View History

2018-08-29 06:30:39 +00:00
<?php
namespace App\Repositories;
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;
2020-12-23 11:03:22 +00:00
use App\Repositories\Traits\Searchable;
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
{
2020-12-23 11:03:22 +00:00
use Searchable;
2022-06-10 10:47:46 +00:00
/** @return Collection|array<array-key, Album> */
public function getRecentlyAdded(int $count = 6): Collection
2022-06-10 10:47:46 +00:00
{
return Album::query()
2022-06-10 10:47:46 +00:00
->isStandard()
->latest('created_at')
2022-06-10 10:47:46 +00:00
->limit($count)
->get();
}
/** @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()
->leftJoin('songs', 'albums.id', 'songs.album_id')
->leftJoin('interactions', static function (JoinClause $join) use ($user): void {
$join->on('songs.id', 'interactions.song_id')->where('interactions.user_id', $user->id);
})
2022-06-10 10:47:46 +00:00
->isStandard()
->orderByDesc('play_count')
->limit($count)
->get('albums.*');
2022-06-10 10:47:46 +00:00
}
/** @return Collection|array<array-key, Album> */
public function getByArtist(Artist $artist): Collection
{
return Album::query()
->where('artist_id', $artist->id)
->orWhereIn('id', $artist->songs()->pluck('album_id'))
->orderBy('name')
->get();
}
public function paginate(): Paginator
{
return Album::query()
->isStandard()
->orderBy('name')
->simplePaginate(21);
}
2018-08-29 06:30:39 +00:00
}