|
14 | 14 | use PSFS\base\types\helpers\attributes\HttpMethod; |
15 | 15 | use PSFS\base\types\helpers\attributes\Label; |
16 | 16 | use PSFS\base\types\helpers\attributes\Route as RouteAttribute; |
| 17 | +use PSFS\base\types\traits\Api\ApiCrudResponseTrait; |
17 | 18 | use PSFS\base\types\traits\Api\Crud\ApiListTrait; |
18 | 19 | use PSFS\base\types\traits\Api\ManagerTrait; |
19 | 20 |
|
|
22 | 23 | */ |
23 | 24 | abstract class Api extends Singleton |
24 | 25 | { |
25 | | - use ManagerTrait, ApiListTrait; |
| 26 | + use ManagerTrait, ApiListTrait, ApiCrudResponseTrait; |
26 | 27 |
|
27 | 28 | const API_LIST_NAME_FIELD = '__name__'; |
28 | 29 | const API_FIELDS_RESULT_FIELD = '__fields'; |
@@ -69,27 +70,15 @@ public function init() |
69 | 70 |
|
70 | 71 | private function checkActions($method) |
71 | 72 | { |
72 | | - switch ($method) { |
73 | | - default: |
74 | | - case 'modelList': |
75 | | - $this->action = self::API_ACTION_LIST; |
76 | | - break; |
77 | | - case 'get': |
78 | | - $this->action = self::API_ACTION_GET; |
79 | | - break; |
80 | | - case 'post': |
81 | | - $this->action = self::API_ACTION_POST; |
82 | | - break; |
83 | | - case 'put': |
84 | | - $this->action = self::API_ACTION_PUT; |
85 | | - break; |
86 | | - case 'delete': |
87 | | - $this->action = self::API_ACTION_DELETE; |
88 | | - break; |
89 | | - case 'bulk': |
90 | | - $this->action = self::API_ACTION_BULK; |
91 | | - break; |
92 | | - } |
| 73 | + $actionMap = [ |
| 74 | + 'modelList' => self::API_ACTION_LIST, |
| 75 | + 'get' => self::API_ACTION_GET, |
| 76 | + 'post' => self::API_ACTION_POST, |
| 77 | + 'put' => self::API_ACTION_PUT, |
| 78 | + 'delete' => self::API_ACTION_DELETE, |
| 79 | + 'bulk' => self::API_ACTION_BULK, |
| 80 | + ]; |
| 81 | + $this->action = $actionMap[(string)$method] ?? self::API_ACTION_LIST; |
93 | 82 | } |
94 | 83 |
|
95 | 84 | /** |
@@ -175,16 +164,12 @@ public function post() |
175 | 164 | $message = t('Selected model could not be saved'); |
176 | 165 | } |
177 | 166 | } catch (\Exception $e) { |
178 | | - if (Config::getParam('debug')) { |
179 | | - $message = t('An error occurred while saving the item: ') . '<br>' . $e->getMessage(); |
180 | | - } else { |
181 | | - $message = t('An error occurred while saving the item: ') . '<br>' . $e->getCode(); |
182 | | - } |
183 | | - $context = []; |
184 | | - if (null !== $e->getPrevious()) { |
185 | | - $context[] = $e->getPrevious()->getMessage(); |
186 | | - } |
187 | | - Logger::log($e->getMessage(), LOG_CRIT, $context); |
| 167 | + $message = $this->buildMutationErrorMessage( |
| 168 | + 'An error occurred while saving the item: ', |
| 169 | + $e, |
| 170 | + (bool)Config::getParam('debug') |
| 171 | + ); |
| 172 | + $this->logCriticalException($e); |
188 | 173 | } |
189 | 174 |
|
190 | 175 | return $this->json(new JsonResponse($model, $saved, $saved ? 1 : 0, 0, $message), $status); |
@@ -224,18 +209,12 @@ public function put($pk) |
224 | 209 | $message = t('An error occurred while updating the item, please check logs'); |
225 | 210 | } |
226 | 211 | } catch (\Exception $e) { |
227 | | - if (Config::getParam('debug')) { |
228 | | - $message = t('An error occurred while updating the item: ') . '<br>' . $e->getMessage(); |
229 | | - } else { |
230 | | - $message = t( |
231 | | - 'An error occurred while updating the item, please check logs: ' |
232 | | - ) . '<br>' . $e->getCode(); |
233 | | - } |
234 | | - $context = []; |
235 | | - if (null !== $e->getPrevious()) { |
236 | | - $context[] = $e->getPrevious()->getMessage(); |
237 | | - } |
238 | | - Logger::log($e->getMessage(), LOG_CRIT, $context); |
| 212 | + $message = $this->buildMutationErrorMessage( |
| 213 | + 'An error occurred while updating the item, please check logs: ', |
| 214 | + $e, |
| 215 | + (bool)Config::getParam('debug') |
| 216 | + ); |
| 217 | + $this->logCriticalException($e); |
239 | 218 | } |
240 | 219 | } else { |
241 | 220 | $message = t('Referenced model for update was not found'); |
@@ -275,11 +254,7 @@ public function delete($pk = null) |
275 | 254 | $deleted = true; |
276 | 255 | } |
277 | 256 | } catch (\Exception $e) { |
278 | | - $context = []; |
279 | | - if (null !== $e->getPrevious()) { |
280 | | - $context[] = $e->getPrevious()->getMessage(); |
281 | | - } |
282 | | - Logger::log($e->getMessage(), LOG_CRIT, $context); |
| 257 | + $this->logCriticalException($e); |
283 | 258 | } |
284 | 259 | } |
285 | 260 |
|
|
0 commit comments