diff --git a/src/application/controllers/api/v1/Appointments.php b/src/application/controllers/api/v1/Appointments.php index 1fd3adeb..c19e4d1a 100644 --- a/src/application/controllers/api/v1/Appointments.php +++ b/src/application/controllers/api/v1/Appointments.php @@ -14,6 +14,7 @@ require_once __DIR__ . '/API_V1_Controller.php'; use \EA\Engine\Api\V1\Response; +use \EA\Engine\Api\V1\Request; use \EA\Engine\Types\NonEmptyString; /** @@ -72,9 +73,18 @@ class Appointments extends API_V1_Controller { */ public function post() { try { - $request = json_decode(file_get_contents('php://input'), true); - $this->parser->decode($request); - $id = $this->appointments_model->add($request); + // Insert the appointment to the database. + $request = new Request(); + $appointment = $request->getBody(); + $this->parser->decode($appointment); + + if (isset($appointment['id'])) { + unset($appointment['id']); + } + + $id = $this->appointments_model->add($appointment); + + // Fetch the new object from the database and return it to the client. $appointments = $this->appointments_model->get_batch('id = ' . $id); $response = new Response($appointments); $status = new NonEmptyString('201 Created'); @@ -91,17 +101,22 @@ class Appointments extends API_V1_Controller { */ public function put($id) { try { - $appointment = $this->appointments_model->get_batch('id = ' . $id); + // Update the appointment record. + $batch = $this->appointments_model->get_batch('id = ' . $id); - if ($id !== null && count($appointments) === 0) { + if ($id !== null && count($batch) === 0) { throw new \EA\Engine\Api\V1\Exception('The requested appointment record was not found!', 404, 'Not Found'); } - $request = json_decode(file_get_contents('php://input'), true); - $this->parser->decode($request, $appointment); - $request['id'] = $id; - $id = $this->appointments_model->add($request); + $request = new Request(); + $updatedAppointment = $request->getBody(); + $baseAppointment = $batch[0]; + $this->parser->decode($updatedAppointment, $baseAppointment); + $updatedAppointment['id'] = $id; + $id = $this->appointments_model->add($updatedAppointment); + + // Fetch the updated object from the database and return it to the client. $appointments = $this->appointments_model->get_batch('id = ' . $id); $response = new Response($appointments); $status = new NonEmptyString('201 Created'); diff --git a/src/engine/Api/V1/Request.php b/src/engine/Api/V1/Request.php new file mode 100644 index 00000000..4e8a5b7c --- /dev/null +++ b/src/engine/Api/V1/Request.php @@ -0,0 +1,31 @@ + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.2.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Api\V1; + +/** + * Request Class + * + * This class handles the common request handling before the data are manipulated and + * returned back with the Response class. + */ +class Request { + /** + * Get request body as an associative array. + * + * @return array + */ + public function getBody() { + return json_decode(file_get_contents('php://input'), true); + } +}