Implemented the PUT method of appointments resource.

This commit is contained in:
Alex Tselegidis 2016-07-09 22:01:03 +02:00
parent a40bb1ae4c
commit 4c1ec9ea2f
2 changed files with 45 additions and 17 deletions

View file

@ -45,30 +45,38 @@ class Appointments extends API_V1_Controller {
* @param int $id Optional (null), the record ID to be returned. * @param int $id Optional (null), the record ID to be returned.
*/ */
public function get($id = null) { public function get($id = null) {
$condition = $id !== null ? 'id = ' . $id : null; try {
$appointments = $this->appointments_model->get_batch($condition); $condition = $id !== null ? 'id = ' . $id : null;
$appointments = $this->appointments_model->get_batch($condition);
$response = new Response($appointments); $response = new Response($appointments);
$response->encode($this->parser)->search()->sort()->paginate()->minimize(); $response->encode($this->parser)->search()->sort()->paginate()->minimize();
if ($id !== null) { if ($id !== null) {
$response->singleEntry(); $response->singleEntry();
}
$response->output();
} catch(\Exception $exception) {
exit($this->_handleException($exception));
} }
$response->output();
} }
/** /**
* POST API Method * POST API Method
*/ */
public function post() { public function post() {
$request = json_decode(file_get_contents('php://input'), true); try {
$this->parser->decode($request); $request = json_decode(file_get_contents('php://input'), true);
$id = $this->appointments_model->add($request); $this->parser->decode($request);
$appointments = $this->appointments_model->get_batch('id = ' . $id); $id = $this->appointments_model->add($request);
$response = new Response($appointments); $appointments = $this->appointments_model->get_batch('id = ' . $id);
$status = new NonEmptyString('201 Created'); $response = new Response($appointments);
$response->encode($this->parser)->singleEntry()->output($status); $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. * @param int $id The record ID to be updated.
*/ */
public function put($id) { 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. * @param int $id The record ID to be deleted.
*/ */
public function delete($id) { public function delete($id) {
try {
} catch(\Exception $exception) {
exit($this->_handleException($exception));
}
} }
} }

View file

@ -31,8 +31,8 @@ class Appointments implements ParsersInterface {
$response = $encodedResponse; $response = $encodedResponse;
} }
public function decode(array &$request) { public function decode(array &$request, array $base = null) {
$decodedRequest = []; $decodedRequest = $base ?: [];
if (!empty($request['id'])) { if (!empty($request['id'])) {
$decodedRequest['id'] = $request['id']; $decodedRequest['id'] = $request['id'];