From 4c1ec9ea2f5db9f9d1eab51179c99a5ac0c867a9 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 9 Jul 2016 22:01:03 +0200 Subject: [PATCH] Implemented the PUT method of appointments resource. --- .../controllers/api/v1/Appointments.php | 58 ++++++++++++++----- src/engine/Api/V1/Parsers/Appointments.php | 4 +- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/application/controllers/api/v1/Appointments.php b/src/application/controllers/api/v1/Appointments.php index a21d2aff..e0533804 100644 --- a/src/application/controllers/api/v1/Appointments.php +++ b/src/application/controllers/api/v1/Appointments.php @@ -45,30 +45,38 @@ class Appointments extends API_V1_Controller { * @param int $id Optional (null), the record ID to be returned. */ public function get($id = null) { - $condition = $id !== null ? 'id = ' . $id : null; - $appointments = $this->appointments_model->get_batch($condition); + try { + $condition = $id !== null ? 'id = ' . $id : null; + $appointments = $this->appointments_model->get_batch($condition); - $response = new Response($appointments); - $response->encode($this->parser)->search()->sort()->paginate()->minimize(); + $response = new Response($appointments); + $response->encode($this->parser)->search()->sort()->paginate()->minimize(); - if ($id !== null) { - $response->singleEntry(); - } + if ($id !== null) { + $response->singleEntry(); + } - $response->output(); + $response->output(); + } catch(\Exception $exception) { + exit($this->_handleException($exception)); + } } /** * POST API Method */ public function post() { - $request = json_decode(file_get_contents('php://input'), true); - $this->parser->decode($request); - $id = $this->appointments_model->add($request); - $appointments = $this->appointments_model->get_batch('id = ' . $id); - $response = new Response($appointments); - $status = new NonEmptyString('201 Created'); - $response->encode($this->parser)->singleEntry()->output($status); + try { + $request = json_decode(file_get_contents('php://input'), true); + $this->parser->decode($request); + $id = $this->appointments_model->add($request); + $appointments = $this->appointments_model->get_batch('id = ' . $id); + $response = new Response($appointments); + $status = new NonEmptyString('201 Created'); + $response->encode($this->parser)->singleEntry()->output($status); + } catch(\Exception $exception) { + exit($this->_handleException($exception)); + } } /** @@ -77,7 +85,23 @@ class Appointments extends API_V1_Controller { * @param int $id The record ID to be updated. */ public function put($id) { + try { + if (empty($id) || !is_numeric($id)) { + throw new \EA\Engine\Api\V1\Exception('The provided URI ID is not a numeric value: ' . $id); + } + $appointment = $this->appointments_model->get_row($id); + $request = json_decode(file_get_contents('php://input'), true); + $this->parser->decode($request, $appointment); + $request['id'] = $id; + $id = $this->appointments_model->add($request); + $appointments = $this->appointments_model->get_batch('id = ' . $id); + $response = new Response($appointments); + $status = new NonEmptyString('201 Created'); + $response->encode($this->parser)->singleEntry()->output($status); + } catch(\Exception $exception) { + exit($this->_handleException($exception)); + } } /** @@ -86,7 +110,11 @@ class Appointments extends API_V1_Controller { * @param int $id The record ID to be deleted. */ public function delete($id) { + try { + } catch(\Exception $exception) { + exit($this->_handleException($exception)); + } } } diff --git a/src/engine/Api/V1/Parsers/Appointments.php b/src/engine/Api/V1/Parsers/Appointments.php index 000edb1e..9d187ef2 100644 --- a/src/engine/Api/V1/Parsers/Appointments.php +++ b/src/engine/Api/V1/Parsers/Appointments.php @@ -31,8 +31,8 @@ class Appointments implements ParsersInterface { $response = $encodedResponse; } - public function decode(array &$request) { - $decodedRequest = []; + public function decode(array &$request, array $base = null) { + $decodedRequest = $base ?: []; if (!empty($request['id'])) { $decodedRequest['id'] = $request['id'];