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;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2018-08-29 06:15:11 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
class InteractionRepository extends Repository
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
|
|
|
use ByCurrentUser;
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @return Collection|array<Interaction> */
|
2018-08-29 06:15:11 +00:00
|
|
|
public function getUserFavorites(User $user): Collection
|
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return $this->model
|
|
|
|
->newQuery()
|
|
|
|
->where([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'liked' => true,
|
|
|
|
])
|
2018-08-29 06:15:11 +00:00
|
|
|
->with('song')
|
|
|
|
->pluck('song');
|
|
|
|
}
|
2018-10-20 21:46:12 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @return array<Interaction> */
|
2018-10-20 21:46:12 +00:00
|
|
|
public function getRecentlyPlayed(User $user, ?int $count = null): array
|
|
|
|
{
|
2022-11-27 15:29:29 +00:00
|
|
|
return $this->model
|
2022-08-09 18:45:11 +00:00
|
|
|
->newQuery()
|
2018-10-20 21:46:12 +00:00
|
|
|
->where('user_id', $user->id)
|
2019-04-14 14:12:16 +00:00
|
|
|
->where('play_count', '>', 0)
|
2022-11-27 15:29:29 +00:00
|
|
|
->latest('last_played_at')
|
|
|
|
->when($count, static fn (Builder $query, int $count) => $query->take($count))
|
|
|
|
->pluck('song_id')
|
|
|
|
->all();
|
2018-10-20 21:46:12 +00:00
|
|
|
}
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|