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;
|
2018-10-20 21:46:12 +00:00
|
|
|
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,
|
2019-05-24 08:20:47 +00:00
|
|
|
'liked' => true,
|
2018-08-29 06:15:11 +00:00
|
|
|
])
|
|
|
|
->with('song')
|
|
|
|
->get()
|
|
|
|
->pluck('song');
|
|
|
|
}
|
2018-10-20 21:46:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Interaction[]
|
|
|
|
*/
|
|
|
|
public function getRecentlyPlayed(User $user, ?int $count = null): array
|
|
|
|
{
|
|
|
|
/** @var Builder $query */
|
|
|
|
$query = $this->model
|
|
|
|
->where('user_id', $user->id)
|
2019-04-14 14:12:16 +00:00
|
|
|
->where('play_count', '>', 0)
|
2018-10-20 21:46:12 +00:00
|
|
|
->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
|
|
|
}
|