forked from mirrors/easyappointments
Added new Request class for handling the common request operations.
This commit is contained in:
parent
6ebe484abd
commit
ff558f99c4
2 changed files with 55 additions and 9 deletions
|
@ -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');
|
||||
|
|
31
src/engine/Api/V1/Request.php
Normal file
31
src/engine/Api/V1/Request.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue