mirror of
https://github.com/koel/koel
synced 2024-12-14 06:32:28 +00:00
50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Interaction;
|
|
use App\Models\User;
|
|
use App\Repositories\Traits\ByCurrentUser;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class InteractionRepository extends AbstractRepository
|
|
{
|
|
use ByCurrentUser;
|
|
|
|
public function getModelClass(): string
|
|
{
|
|
return Interaction::class;
|
|
}
|
|
|
|
/** @return Collection|array<Interaction> */
|
|
public function getUserFavorites(User $user): Collection
|
|
{
|
|
return $this->model->where([
|
|
'user_id' => $user->id,
|
|
'liked' => true,
|
|
])
|
|
->with('song')
|
|
->get()
|
|
->pluck('song');
|
|
}
|
|
|
|
/** @return array<Interaction> */
|
|
public function getRecentlyPlayed(User $user, ?int $count = null): array
|
|
{
|
|
/** @var Builder $query */
|
|
$query = $this->model
|
|
->where('user_id', $user->id)
|
|
->where('play_count', '>', 0)
|
|
->orderBy('updated_at', 'DESC');
|
|
|
|
if ($count) {
|
|
$query = $query->take($count);
|
|
}
|
|
|
|
return $query
|
|
->get()
|
|
->pluck('song_id')
|
|
->all();
|
|
}
|
|
}
|