1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Api\V1; |
4
|
|
|
|
5
|
|
|
use App\Interfaces\Controllers\IApi; |
6
|
|
|
use App\Reuse\Controllers\AbstractApi; |
7
|
|
|
use App\Http\Response; |
8
|
|
|
use App\Container; |
9
|
|
|
use App\Model\Repository\Users; |
10
|
|
|
use Exception; |
11
|
|
|
use OpenApi\Annotations as OA; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @OA\Info( |
15
|
|
|
* version="1.0", |
16
|
|
|
* title="Api Restful Controller" |
17
|
|
|
* ) |
18
|
|
|
*/ |
19
|
|
|
final class Restful extends AbstractApi implements IApi |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* user repository |
24
|
|
|
* |
25
|
|
|
* @var Users |
26
|
|
|
*/ |
27
|
|
|
protected $userRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* sql |
31
|
|
|
* |
32
|
|
|
* @var String |
33
|
|
|
*/ |
34
|
|
|
protected $sql; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* instanciate |
38
|
|
|
* |
39
|
|
|
* @param Container $container |
40
|
|
|
*/ |
41
|
6 |
|
public function __construct(Container $container) |
42
|
|
|
{ |
43
|
6 |
|
$this->userRepository = new Users($container); |
44
|
6 |
|
parent::__construct($container); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* index |
49
|
|
|
* |
50
|
|
|
* @OA\Get( |
51
|
|
|
* path="/api/v1/restful", |
52
|
|
|
* summary="Search for a something item", |
53
|
|
|
* @OA\RequestBody( |
54
|
|
|
* @OA\MediaType( |
55
|
|
|
* mediaType="application/json", |
56
|
|
|
* @OA\Schema( |
57
|
|
|
* @OA\Property( |
58
|
|
|
* property="id", |
59
|
|
|
* type="string" |
60
|
|
|
* ), |
61
|
|
|
* @OA\Property( |
62
|
|
|
* property="name", |
63
|
|
|
* type="string" |
64
|
|
|
* ), |
65
|
|
|
* example={"id": 10} |
66
|
|
|
* ) |
67
|
|
|
* ) |
68
|
|
|
* ), |
69
|
|
|
* @OA\Response( |
70
|
|
|
* response=200, |
71
|
|
|
* description="OK" |
72
|
|
|
* ) |
73
|
|
|
* ) |
74
|
|
|
* @return Restful |
75
|
|
|
*/ |
76
|
1 |
|
final public function index(): Restful |
77
|
|
|
{ |
78
|
1 |
|
$this->userRepository->find( |
79
|
1 |
|
['name'], |
80
|
|
|
[ |
81
|
1 |
|
'name' => ['john', 'elisa'], |
82
|
|
|
'job>' => 0 |
83
|
|
|
] |
84
|
|
|
); |
85
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
86
|
1 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* store |
91
|
|
|
* |
92
|
|
|
* @OA\Post( |
93
|
|
|
* path="/api/v1/restful", |
94
|
|
|
* summary="Adds a new something item", |
95
|
|
|
* @OA\RequestBody( |
96
|
|
|
* @OA\MediaType( |
97
|
|
|
* mediaType="application/json", |
98
|
|
|
* @OA\Schema( |
99
|
|
|
* @OA\Property( |
100
|
|
|
* property="id", |
101
|
|
|
* type="string" |
102
|
|
|
* ), |
103
|
|
|
* @OA\Property( |
104
|
|
|
* property="name", |
105
|
|
|
* type="string" |
106
|
|
|
* ), |
107
|
|
|
* example={"id": 10, "name": "Jessica Smith"} |
108
|
|
|
* ) |
109
|
|
|
* ) |
110
|
|
|
* ), |
111
|
|
|
* @OA\Response( |
112
|
|
|
* response=200, |
113
|
|
|
* description="OK" |
114
|
|
|
* ) |
115
|
|
|
* ) |
116
|
|
|
* @return Restful |
117
|
|
|
*/ |
118
|
1 |
|
final public function store(): Restful |
119
|
|
|
{ |
120
|
|
|
try { |
121
|
1 |
|
$this->userRepository->insert($this->request->getParams()); |
122
|
|
|
$this->sql = $this->userRepository->getSql(); |
123
|
1 |
|
} catch (\Exception $e) { |
124
|
1 |
|
$this->sql = $e->getMessage(); |
125
|
|
|
} |
126
|
1 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* update |
131
|
|
|
* |
132
|
|
|
* @OA\Put( |
133
|
|
|
* path="/api/v1/restful", |
134
|
|
|
* summary="Modify something item", |
135
|
|
|
* @OA\RequestBody( |
136
|
|
|
* @OA\MediaType( |
137
|
|
|
* mediaType="application/json", |
138
|
|
|
* @OA\Schema( |
139
|
|
|
* @OA\Property( |
140
|
|
|
* property="id", |
141
|
|
|
* type="string" |
142
|
|
|
* ), |
143
|
|
|
* @OA\Property( |
144
|
|
|
* property="name", |
145
|
|
|
* type="string" |
146
|
|
|
* ), |
147
|
|
|
* example={"id": 10, "name": "Jessica Smoke"} |
148
|
|
|
* ) |
149
|
|
|
* ) |
150
|
|
|
* ), |
151
|
|
|
* @OA\Response( |
152
|
|
|
* response=200, |
153
|
|
|
* description="OK" |
154
|
|
|
* ) |
155
|
|
|
* ) |
156
|
|
|
* @return Restful |
157
|
|
|
*/ |
158
|
1 |
|
final public function update(): Restful |
159
|
|
|
{ |
160
|
|
|
try { |
161
|
1 |
|
$this->userRepository->update($this->request->getParams()); |
162
|
|
|
$this->sql = $this->userRepository->getSql(); |
163
|
1 |
|
} catch (\Exception $e) { |
164
|
1 |
|
$this->sql = $e->getMessage(); |
165
|
|
|
} |
166
|
1 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* delete |
171
|
|
|
* |
172
|
|
|
* @OA\Delete( |
173
|
|
|
* path="/api/v1/restful", |
174
|
|
|
* summary="Delete something item", |
175
|
|
|
* @OA\RequestBody( |
176
|
|
|
* @OA\MediaType( |
177
|
|
|
* mediaType="application/json", |
178
|
|
|
* @OA\Schema( |
179
|
|
|
* @OA\Property( |
180
|
|
|
* property="id", |
181
|
|
|
* type="string" |
182
|
|
|
* ), |
183
|
|
|
* example={"id": 10} |
184
|
|
|
* ) |
185
|
|
|
* ) |
186
|
|
|
* ), |
187
|
|
|
* @OA\Response( |
188
|
|
|
* response=200, |
189
|
|
|
* description="OK" |
190
|
|
|
* ) |
191
|
|
|
* ) |
192
|
|
|
* @return Restful |
193
|
|
|
*/ |
194
|
1 |
|
final public function delete(): Restful |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
1 |
|
$this->userRepository->delete($this->request->getParams()); |
198
|
|
|
$this->sql = $this->userRepository->getSql(); |
199
|
1 |
|
} catch (\Exception $e) { |
200
|
1 |
|
$this->sql = $e->getMessage(); |
201
|
|
|
} |
202
|
1 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* set response with for a classname and action |
207
|
|
|
* |
208
|
|
|
* @param string $classname |
209
|
|
|
* @param string $action |
210
|
|
|
* @return Restful |
211
|
|
|
*/ |
212
|
1 |
|
protected function setResponse(string $classname, string $action): Restful |
213
|
|
|
{ |
214
|
1 |
|
$this->response |
215
|
1 |
|
->setCode(Response::HTTP_OK) |
216
|
1 |
|
->setContent( |
217
|
|
|
[ |
218
|
1 |
|
'error' => false, |
219
|
|
|
'datas' => [ |
220
|
1 |
|
'method' => $this->request->getMethod(), |
221
|
1 |
|
'params' => $this->request->getParams(), |
222
|
1 |
|
'controller' => $classname, |
223
|
1 |
|
'action' => $action, |
224
|
1 |
|
'sql' => $this->sql |
225
|
|
|
] |
226
|
|
|
] |
227
|
|
|
); |
228
|
1 |
|
return $this; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|