-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlbumRepository.php
60 lines (52 loc) · 1.68 KB
/
AlbumRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace App\Repositories;
use App\Models\Album;
use App\Models\Artist;
use App\Models\User;
use App\Repositories\Traits\Searchable;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
class AlbumRepository extends Repository
{
use Searchable;
/** @return Collection|array<array-key, Album> */
public function getRecentlyAdded(int $count = 6): Collection
{
return Album::query()
->isStandard()
->latest('created_at')
->limit($count)
->get();
}
/** @return Collection|array<array-key, Album> */
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
{
$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);
})
->isStandard()
->orderByDesc('play_count')
->limit($count)
->get('albums.*');
}
/** @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);
}
}