1 | <?php |
||
2 | |||
3 | namespace App\Repositories; |
||
4 | |||
5 | use App\Models\Interaction; |
||
6 | use App\Models\User; |
||
7 | use App\Repositories\Traits\ByCurrentUser; |
||
8 | use Illuminate\Database\Query\Builder; |
||
9 | use Illuminate\Support\Collection; |
||
10 | |||
11 | class InteractionRepository extends AbstractRepository |
||
12 | { |
||
13 | use ByCurrentUser; |
||
14 | |||
15 | public function getModelClass(): string |
||
16 | { |
||
17 | return Interaction::class; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * Get all songs favorited by a user. |
||
22 | */ |
||
23 | public function getUserFavorites(User $user): Collection |
||
24 | { |
||
25 | return $this->model->where([ |
||
26 | 'user_id' => $user->id, |
||
27 | 'liked' => true, |
||
28 | ]) |
||
29 | ->with('song') |
||
30 | ->get() |
||
31 | ->pluck('song'); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return Interaction[] |
||
36 | */ |
||
37 | public function getRecentlyPlayed(User $user, ?int $count = null): array |
||
38 | { |
||
39 | /** @var Builder $query */ |
||
40 | $query = $this->model |
||
41 | ->where('user_id', $user->id) |
||
42 | ->where('play_count', '>', 0) |
||
43 | ->orderBy('updated_at', 'DESC'); |
||
44 | |||
45 | if ($count) { |
||
0 ignored issues
–
show
|
|||
46 | $query = $query->take($count); |
||
47 | } |
||
48 | |||
49 | return $query |
||
50 | ->get() |
||
51 | ->pluck('song_id') |
||
52 | ->all(); |
||
53 | } |
||
54 | } |
||
55 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: