1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Notification\Repositories\Eloquent; |
4
|
|
|
|
5
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
6
|
|
|
use Modules\Notification\Repositories\NotificationRepository; |
7
|
|
|
|
8
|
|
|
final class EloquentNotificationRepository extends EloquentBaseRepository implements NotificationRepository |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param int $userId |
12
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
13
|
|
|
*/ |
14
|
|
|
public function latestForUser($userId) |
15
|
|
|
{ |
16
|
|
|
return $this->model->whereUserId($userId)->whereIsRead(false)->orderBy('created_at', 'desc')->take(10)->get(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Mark the given notification id as "read" |
21
|
|
|
* @param int $notificationId |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function markNotificationAsRead($notificationId) |
25
|
|
|
{ |
26
|
|
|
$notification = $this->find($notificationId); |
27
|
|
|
$notification->is_read = true; |
28
|
|
|
|
29
|
|
|
return $notification->save(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function all() |
33
|
|
|
{ |
34
|
|
|
return $this->model->all(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function find($id) |
38
|
|
|
{ |
39
|
|
|
return $this->model->find($id); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get all the notifications for the given user id |
44
|
|
|
* @param int $userId |
45
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
46
|
|
|
*/ |
47
|
|
|
public function allForUser($userId) |
48
|
|
|
{ |
49
|
|
|
return $this->model->whereUserId($userId)->orderBy('created_at', 'desc')->get(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get all the read notifications for the given user id |
54
|
|
|
* @param int $userId |
55
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
56
|
|
|
*/ |
57
|
|
|
public function allReadForUser($userId) |
58
|
|
|
{ |
59
|
|
|
return $this->model->whereUserId($userId)->whereIsRead(true)->orderBy('created_at', 'desc')->get(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get all the unread notifications for the given user id |
64
|
|
|
* @param int $userId |
65
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
66
|
|
|
*/ |
67
|
|
|
public function allUnreadForUser($userId) |
68
|
|
|
{ |
69
|
|
|
return $this->model->whereUserId($userId)->whereIsRead(false)->orderBy('created_at', 'desc')->get(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Delete all the notifications for the given user |
74
|
|
|
* @param int $userId |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function deleteAllForUser($userId) |
78
|
|
|
{ |
79
|
|
|
return $this->model->whereUserId($userId)->delete(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Mark all the notifications for the given user as read |
84
|
|
|
* @param int $userId |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function markAllAsReadForUser($userId) |
88
|
|
|
{ |
89
|
|
|
return $this->model->whereUserId($userId)->update(['is_read' => true]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|