find(Coin::class)->array(); usort($coins, function($a, $b) { return strcmp($a['code'], $b['code']); }); $output = compact('coins'); return $this->withJson($response, $output); } public function show(Request $request, Response $response, ModelFactory $factory, $coin_id): Response { $coin = $factory->find(Coin::class)->one($coin_id); if (!$coin) { return $this->withJson($response, ['coin' => null]); } $output = ['coin' => $coin->toArray()]; return $this->withJson($response, $output); } public function add(Request $request, Response $response, ModelFactory $factory): Response { $post = $request->getBody()->getContents(); $post = json_decode($post); $coin = Coin::add($factory, $post); $status = false; if ($coin->isNew()) { $status = $coin->save(); } $output = [ 'input' => $post, 'coin' => $coin->toArray(), 'created' => $status ]; return $this->withJson($response, $output); } public function edit(Request $request, Response $response, ModelFactory $factory, $coin_id): Response { $coin = $factory->find(Coin::class)->one($coin_id); if (!$coin) { return $this->withJson($response, ['coin' => null]); } $post = json_decode($request->getBody()->getContents()); $edited = $coin->edit($post); $output = ['input' => $post, 'coin' => $coin->toArray(), 'edited' => $edited]; return $this->withJson($response, $output); } public function delete(Request $request, Response $response, ModelFactory $factory, $coin_id): Response { $coin = $factory->find(Coin::class)->one($coin_id); if (!$coin) { return $this->withJson($response, ['coin' => null, 'deleted' => false]); } $output = [ 'coin' => $coin->toArray() ]; $status = $coin->delete(); $output['deleted'] = $status; return $this->withJson($response, $output); } }