2018-08-29 06:30:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
use App\Models\Album;
|
2022-10-18 14:07:41 +00:00
|
|
|
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;
|
2022-08-09 18:45:11 +00:00
|
|
|
use Illuminate\Contracts\Pagination\Paginator;
|
2022-10-11 15:28:43 +00:00
|
|
|
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;
|
2018-08-29 09:41:24 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
/** @return Collection|array<array-key, Album> */
|
2022-10-11 15:28:43 +00:00
|
|
|
public function getRecentlyAdded(int $count = 6): Collection
|
2022-06-10 10:47:46 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return Album::query()
|
2022-06-10 10:47:46 +00:00
|
|
|
->isStandard()
|
2022-10-11 15:28:43 +00:00
|
|
|
->latest('created_at')
|
2022-06-10 10:47:46 +00:00
|
|
|
->limit($count)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return Collection|array<array-key, Album> */
|
2022-10-11 15:28:43 +00:00
|
|
|
public function getMostPlayed(int $count = 6, ?User $user = null): Collection
|
2022-06-10 10:47:46 +00:00
|
|
|
{
|
2022-10-11 15:28:43 +00:00
|
|
|
$user ??= $this->auth->user();
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
return Album::query()
|
2022-10-11 15:28:43 +00:00
|
|
|
->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)
|
2022-10-11 15:28:43 +00:00
|
|
|
->get('albums.*');
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 14:07:41 +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();
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:28:43 +00:00
|
|
|
public function paginate(): Paginator
|
2022-08-09 18:45:11 +00:00
|
|
|
{
|
|
|
|
return Album::query()
|
|
|
|
->isStandard()
|
2022-10-11 15:28:43 +00:00
|
|
|
->orderBy('name')
|
2022-08-09 18:45:11 +00:00
|
|
|
->simplePaginate(21);
|
|
|
|
}
|
2018-08-29 06:30:39 +00:00
|
|
|
}
|