From de4bc1217a928a974a042e9ddc95169019a3e7db Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 9 Jul 2016 21:11:33 +0200 Subject: [PATCH] Added API method for getting a single appointment. --- .../controllers/api/v1/Appointments.php | 14 ++++++++++---- src/engine/Api/V1/Response.php | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/application/controllers/api/v1/Appointments.php b/src/application/controllers/api/v1/Appointments.php index a3de0a93..e8ea86c1 100644 --- a/src/application/controllers/api/v1/Appointments.php +++ b/src/application/controllers/api/v1/Appointments.php @@ -45,13 +45,19 @@ class Appointments extends API_V1_Controller { * @param int $id Optional (null), the record ID to be returned. */ public function get($id = null) { - $appointments = $this->appointments_model->get_batch(); - + $condition = $id !== null ? 'id = ' . $id : null; + + $appointments = $this->appointments_model->get_batch($condition); + $response = new Response($appointments); - $status = new NonEmptyString('200 OK'); + $response->format($this->formatter)->search()->sort()->paginate()->minimize(); - $response->format($this->formatter)->search()->sort()->paginate()->minimize()->output(); + if ($id !== null) { + $response->singleEntry(); + } + + $response->output(); } /** diff --git a/src/engine/Api/V1/Response.php b/src/engine/Api/V1/Response.php index 5dee0d7e..1583b124 100644 --- a/src/engine/Api/V1/Response.php +++ b/src/engine/Api/V1/Response.php @@ -98,6 +98,19 @@ class Response { return $this; } + /** + * Return a single entry instead of an array of entries. + * + * This is useful whenever the client requests only a single entry. + * + * @return \EA\Engine\Api\V1\Response + */ + public function singleEntry() { + $this->response = array_shift($this->response); + + return $this; + } + /** * Output the response as a JSON with the provided status header. *