Implemented the DELETE method and corrected not-found exceptions for GET and PUT.

This commit is contained in:
Alex Tselegidis 2016-07-09 22:07:26 +02:00
parent 4c1ec9ea2f
commit bfc96409bc

View file

@ -49,6 +49,11 @@ class Appointments extends API_V1_Controller {
$condition = $id !== null ? 'id = ' . $id : null; $condition = $id !== null ? 'id = ' . $id : null;
$appointments = $this->appointments_model->get_batch($condition); $appointments = $this->appointments_model->get_batch($condition);
if ($id !== null && count($appointments) === 0) {
throw new \EA\Engine\Api\V1\Exception('The requested appointment record was not found!', 404,
'Not Found');
}
$response = new Response($appointments); $response = new Response($appointments);
$response->encode($this->parser)->search()->sort()->paginate()->minimize(); $response->encode($this->parser)->search()->sort()->paginate()->minimize();
@ -86,11 +91,13 @@ class Appointments extends API_V1_Controller {
*/ */
public function put($id) { public function put($id) {
try { try {
if (empty($id) || !is_numeric($id)) { $appointment = $this->appointments_model->get_batch('id = ' . $id);
throw new \EA\Engine\Api\V1\Exception('The provided URI ID is not a numeric value: ' . $id);
if ($id !== null && count($appointments) === 0) {
throw new \EA\Engine\Api\V1\Exception('The requested appointment record was not found!', 404,
'Not Found');
} }
$appointment = $this->appointments_model->get_row($id);
$request = json_decode(file_get_contents('php://input'), true); $request = json_decode(file_get_contents('php://input'), true);
$this->parser->decode($request, $appointment); $this->parser->decode($request, $appointment);
$request['id'] = $id; $request['id'] = $id;
@ -111,6 +118,14 @@ class Appointments extends API_V1_Controller {
*/ */
public function delete($id) { public function delete($id) {
try { try {
$result = $this->appointments_model->delete($id);
$response = new Response([
'code' => 200,
'message' => 'Appointment was deleted successfully!'
]);
$response->output();
} catch(\Exception $exception) { } catch(\Exception $exception) {
exit($this->_handleException($exception)); exit($this->_handleException($exception));