|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oc\FieldNotes\Persistence; |
|
4
|
|
|
|
|
5
|
|
|
use Oc\Repository\Exception\RecordNotFoundException; |
|
6
|
|
|
use Oc\Repository\Exception\RecordsNotFoundException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class FieldNoteService |
|
10
|
|
|
* |
|
11
|
|
|
* @package Oc\FieldNotes\Persistence |
|
|
|
|
|
|
12
|
|
|
*/ |
|
13
|
|
|
class FieldNoteService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var FieldNoteRepository |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
|
|
private $fieldNoteRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* FieldNoteService constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param FieldNoteRepository $fieldNoteRepository |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(FieldNoteRepository $fieldNoteRepository) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->fieldNoteRepository = $fieldNoteRepository; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Fetches all field notes. |
|
32
|
|
|
* |
|
33
|
|
|
* @return FieldNoteEntity[] |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function fetchAll() |
|
36
|
|
|
{ |
|
37
|
|
|
try { |
|
38
|
|
|
$result = $this->fieldNoteRepository->fetchAll(); |
|
39
|
|
|
} catch (RecordsNotFoundException $e) { |
|
40
|
|
|
$result = []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Fetch by given where clause. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $where |
|
50
|
|
|
* @param array $order |
|
51
|
|
|
* |
|
52
|
|
|
* @return FieldNoteEntity[] |
|
|
|
|
|
|
53
|
|
|
*/ |
|
54
|
|
|
public function fetchBy(array $where = [], array $order = []) |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
|
|
$result = $this->fieldNoteRepository->fetchBy($where, $order); |
|
58
|
|
|
} catch (RecordsNotFoundException $e) { |
|
59
|
|
|
$result = []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Fetches a page by slug. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $where |
|
69
|
|
|
* |
|
70
|
|
|
* @return null|FieldNoteEntity |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
|
|
public function fetchOneBy(array $where = []) |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$result = $this->fieldNoteRepository->fetchOneBy($where); |
|
76
|
|
|
} catch (RecordNotFoundException $e) { |
|
77
|
|
|
$result = null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Creates a field note in the database. |
|
85
|
|
|
* |
|
86
|
|
|
* @param FieldNoteEntity $entity |
|
|
|
|
|
|
87
|
|
|
* |
|
88
|
|
|
* @return FieldNoteEntity |
|
|
|
|
|
|
89
|
|
|
*/ |
|
90
|
|
|
public function create(FieldNoteEntity $entity) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->fieldNoteRepository->create($entity); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Update a field note in the database. |
|
97
|
|
|
* |
|
98
|
|
|
* @param FieldNoteEntity $entity |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return FieldNoteEntity |
|
|
|
|
|
|
101
|
|
|
*/ |
|
102
|
|
|
public function update(FieldNoteEntity $entity) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->fieldNoteRepository->update($entity); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Removes a field note from the database. |
|
109
|
|
|
* |
|
110
|
|
|
* @param FieldNoteEntity $entity |
|
|
|
|
|
|
111
|
|
|
* |
|
112
|
|
|
* @return FieldNoteEntity |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
public function remove(FieldNoteEntity $entity) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->fieldNoteRepository->remove($entity); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Fetch all field notes for field note listing in profile. |
|
121
|
|
|
* |
|
122
|
|
|
* @param int $userId |
|
123
|
|
|
* |
|
124
|
|
|
* @return FieldNoteEntity[] |
|
|
|
|
|
|
125
|
|
|
*/ |
|
126
|
|
|
public function getUserListing($userId) |
|
127
|
|
|
{ |
|
128
|
|
|
$fieldNotes = $this->fetchBy([ |
|
129
|
|
|
'user_id' => $userId |
|
130
|
|
|
], [ |
|
131
|
|
|
'date' => 'ASC', |
|
132
|
|
|
'id' => 'ASC' |
|
133
|
|
|
]); |
|
134
|
|
|
|
|
135
|
|
|
return $fieldNotes ?: []; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Fetches the latest field note for given user id. |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $userId |
|
142
|
|
|
* |
|
143
|
|
|
* @return FieldNoteEntity|null |
|
|
|
|
|
|
144
|
|
|
*/ |
|
145
|
|
|
public function getLatestUserFieldNote($userId) |
|
146
|
|
|
{ |
|
147
|
|
|
try { |
|
148
|
|
|
$result = $this->fieldNoteRepository->getLatestUserFieldNote($userId); |
|
149
|
|
|
} catch (RecordNotFoundException $e) { |
|
150
|
|
|
$result = null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $result; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|