mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-22 07:52:29 +03:00
Implemented the PUT method of appointments resource.
This commit is contained in:
parent
a40bb1ae4c
commit
4c1ec9ea2f
2 changed files with 45 additions and 17 deletions
|
@ -45,6 +45,7 @@ class Appointments extends API_V1_Controller {
|
|||
* @param int $id Optional (null), the record ID to be returned.
|
||||
*/
|
||||
public function get($id = null) {
|
||||
try {
|
||||
$condition = $id !== null ? 'id = ' . $id : null;
|
||||
$appointments = $this->appointments_model->get_batch($condition);
|
||||
|
||||
|
@ -56,12 +57,16 @@ class Appointments extends API_V1_Controller {
|
|||
}
|
||||
|
||||
$response->output();
|
||||
} catch(\Exception $exception) {
|
||||
exit($this->_handleException($exception));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST API Method
|
||||
*/
|
||||
public function post() {
|
||||
try {
|
||||
$request = json_decode(file_get_contents('php://input'), true);
|
||||
$this->parser->decode($request);
|
||||
$id = $this->appointments_model->add($request);
|
||||
|
@ -69,6 +74,9 @@ class Appointments extends API_V1_Controller {
|
|||
$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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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'];
|
||||
|
|
Loading…
Reference in a new issue