koel/app/Repositories/InteractionRepository.php

54 lines
1.1 KiB
PHP
Raw Normal View History

2018-08-29 06:15:11 +00:00
<?php
namespace App\Repositories;
use App\Models\Interaction;
use App\Models\User;
use App\Repositories\Traits\ByCurrentUser;
use Illuminate\Database\Query\Builder;
2018-08-29 06:15:11 +00:00
use Illuminate\Support\Collection;
class InteractionRepository extends AbstractRepository
{
use ByCurrentUser;
public function getModelClass(): string
{
return Interaction::class;
}
/**
* Get all songs favorited by a user.
*/
public function getUserFavorites(User $user): Collection
{
return $this->model->where([
'user_id' => $user->id,
'like' => true,
])
->with('song')
->get()
->pluck('song');
}
/**
* @return Interaction[]
*/
public function getRecentlyPlayed(User $user, ?int $count = null): array
{
/** @var Builder $query */
$query = $this->model
->where('user_id', $user->id)
->orderBy('updated_at', 'DESC');
if ($count) {
$query = $query->take($count);
}
return $query
->get()
->pluck('song_id')
->all();
}
2018-08-29 06:15:11 +00:00
}