|
4 | 4 |
|
5 | 5 | namespace App\Handler\API;
|
6 | 6 |
|
| 7 | +use App\Handler\Exception\FormException; |
7 | 8 | use App\Middleware\DbMiddleware;
|
| 9 | +use Exception; |
8 | 10 | use Laminas\Db\Adapter\Adapter;
|
9 | 11 | use Laminas\Db\RowGateway\RowGateway;
|
10 | 12 | use Laminas\Db\Sql\TableIdentifier;
|
@@ -65,56 +67,67 @@ public function handle(ServerRequestInterface $request): ResponseInterface
|
65 | 67 | $id = $request->getAttribute('id');
|
66 | 68 | $data = $request->getParsedBody();
|
67 | 69 |
|
68 |
| - $objects = $this->getObjects($adapter); |
| 70 | + try { |
| 71 | + $objects = $this->getObjects($adapter); |
69 | 72 |
|
70 |
| - if (!is_null($id)) { |
71 |
| - $filter = array_filter($objects, function ($object) use ($id) { |
72 |
| - return $object->id === intval($id); |
73 |
| - }); |
| 73 | + if (!is_null($id)) { |
| 74 | + $filter = array_filter($objects, function ($object) use ($id) { |
| 75 | + return $object->id === intval($id); |
| 76 | + }); |
74 | 77 |
|
75 |
| - if (count($filter) === 1) { |
76 |
| - $object = current($filter); |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - switch ($request->getMethod()) { |
81 |
| - case 'GET': |
82 |
| - if (is_null($id)) { |
83 |
| - return new JsonResponse($objects); |
84 |
| - } elseif (isset($object)) { |
85 |
| - return new JsonResponse($object); |
86 |
| - } else { |
87 |
| - return new JsonResponse(new stdClass(), 404); |
88 |
| - } |
89 |
| - break; |
90 |
| - case 'POST': |
91 |
| - if (!is_null($data)) { |
92 |
| - $object = $this->insert($adapter, $data); |
93 |
| - |
94 |
| - return new JsonResponse($object); |
95 |
| - } else { |
96 |
| - return new JsonResponse(new stdClass(), 404); |
97 |
| - } |
98 |
| - break; |
99 |
| - case 'PUT': |
100 |
| - if (isset($object) && !is_null($data)) { |
101 |
| - $object = $this->update($adapter, $object, $data); |
102 |
| - |
103 |
| - return new JsonResponse($object); |
104 |
| - } else { |
105 |
| - return new JsonResponse(new stdClass(), 404); |
106 |
| - } |
107 |
| - break; |
108 |
| - case 'DELETE': |
109 |
| - if (isset($object)) { |
110 |
| - $object = $this->delete($adapter, $object); |
111 |
| - |
112 |
| - return new JsonResponse($object); |
113 |
| - } else { |
114 |
| - return new JsonResponse(new stdClass(), 404); |
| 78 | + if (count($filter) === 1) { |
| 79 | + $object = current($filter); |
115 | 80 | }
|
| 81 | + } |
116 | 82 |
|
117 |
| - break; |
| 83 | + switch ($request->getMethod()) { |
| 84 | + case 'GET': |
| 85 | + if (is_null($id)) { |
| 86 | + return new JsonResponse($objects); |
| 87 | + } elseif (isset($object)) { |
| 88 | + return new JsonResponse($object); |
| 89 | + } else { |
| 90 | + return new JsonResponse(new stdClass(), 404); |
| 91 | + } |
| 92 | + break; |
| 93 | + case 'POST': |
| 94 | + if (!is_null($data)) { |
| 95 | + $object = $this->insert($adapter, $data); |
| 96 | + |
| 97 | + return new JsonResponse($object); |
| 98 | + } else { |
| 99 | + return new JsonResponse(new stdClass(), 404); |
| 100 | + } |
| 101 | + break; |
| 102 | + case 'PUT': |
| 103 | + if (isset($object) && !is_null($data)) { |
| 104 | + $object = $this->update($adapter, $object, $data); |
| 105 | + |
| 106 | + return new JsonResponse($object); |
| 107 | + } else { |
| 108 | + return new JsonResponse(new stdClass(), 404); |
| 109 | + } |
| 110 | + break; |
| 111 | + case 'DELETE': |
| 112 | + if (isset($object)) { |
| 113 | + $object = $this->delete($adapter, $object); |
| 114 | + |
| 115 | + return new JsonResponse($object); |
| 116 | + } else { |
| 117 | + return new JsonResponse(new stdClass(), 404); |
| 118 | + } |
| 119 | + |
| 120 | + break; |
| 121 | + } |
| 122 | + } catch (FormException $e) { |
| 123 | + return new JsonResponse([ |
| 124 | + 'error' => $e->getMessage(), |
| 125 | + 'field' => $e->getField(), |
| 126 | + ], 500); |
| 127 | + } catch (Exception $e) { |
| 128 | + return new JsonResponse([ |
| 129 | + 'error' => $e->getMessage(), |
| 130 | + ], 500); |
118 | 131 | }
|
119 | 132 | }
|
120 | 133 |
|
|
0 commit comments