forked from mirrors/easyappointments
Implemented customers API resource controller.
This commit is contained in:
parent
ba4aa48e47
commit
c39c7c3df2
1 changed files with 86 additions and 6 deletions
|
@ -13,6 +13,10 @@
|
|||
|
||||
require_once __DIR__ . '/API_V1_Controller.php';
|
||||
|
||||
use \EA\Engine\Api\V1\Response;
|
||||
use \EA\Engine\Api\V1\Request;
|
||||
use \EA\Engine\Types\NonEmptyString;
|
||||
|
||||
/**
|
||||
* Customers Controller
|
||||
*
|
||||
|
@ -20,31 +24,75 @@ require_once __DIR__ . '/API_V1_Controller.php';
|
|||
* @subpackage API
|
||||
*/
|
||||
class Customers extends API_V1_Controller {
|
||||
/**
|
||||
* Customers Resource Parser
|
||||
*
|
||||
* @var \EA\Engine\Api\V1\Parsers\Customers
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->model('customers_model');
|
||||
$this->parser = new \EA\Engine\Api\V1\Parsers\Customers;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET API Method
|
||||
*
|
||||
* @param int $id Optional (null), the record ID to be returned.
|
||||
*
|
||||
* @return \EA\Engine\Api\V1\Response Returns data response.
|
||||
*/
|
||||
public function get($id = null) {
|
||||
|
||||
try {
|
||||
$condition = $id !== null ? 'id = ' . $id : null;
|
||||
$customers = $this->customers_model->get_batch($condition);
|
||||
|
||||
if ($id !== null && count($customers) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$response = new Response($customers);
|
||||
|
||||
$response->encode($this->parser)
|
||||
->search()
|
||||
->sort()
|
||||
->paginate()
|
||||
->minimize()
|
||||
->singleEntry($id)
|
||||
->output();
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
$this->_handleException($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST API Method
|
||||
*
|
||||
* @return @return \EA\Engine\Api\V1\Response Returns data response.
|
||||
*/
|
||||
public function post() {
|
||||
|
||||
try {
|
||||
// Insert the customer to the database.
|
||||
$request = new Request();
|
||||
$customer = $request->getBody();
|
||||
$this->parser->decode($customer);
|
||||
|
||||
if (isset($customer['id'])) {
|
||||
unset($customer['id']);
|
||||
}
|
||||
|
||||
$id = $this->customers_model->add($customer);
|
||||
|
||||
// Fetch the new object from the database and return it to the client.
|
||||
$batch = $this->customers_model->get_batch('id = ' . $id);
|
||||
$response = new Response($batch);
|
||||
$status = new NonEmptyString('201 Created');
|
||||
$response->encode($this->parser)->singleEntry(true)->output($status);
|
||||
} catch (\Exception $exception) {
|
||||
$this->_handleException($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,7 +103,28 @@ class Customers extends API_V1_Controller {
|
|||
* @return @return \EA\Engine\Api\V1\Response Returns data response.
|
||||
*/
|
||||
public function put($id) {
|
||||
try {
|
||||
// Update the customer record.
|
||||
$batch = $this->customers_model->get_batch('id = ' . $id);
|
||||
|
||||
if ($id !== null && count($batch) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$request = new Request();
|
||||
$updatedCustomer = $request->getBody();
|
||||
$baseCustomer = $batch[0];
|
||||
$this->parser->decode($updatedCustomer, $baseCustomer);
|
||||
$updatedCustomer['id'] = $id;
|
||||
$id = $this->customers_model->add($updatedCustomer);
|
||||
|
||||
// Fetch the updated object from the database and return it to the client.
|
||||
$batch = $this->customers_model->get_batch('id = ' . $id);
|
||||
$response = new Response($batch);
|
||||
$response->encode($this->parser)->singleEntry($id)->output();
|
||||
} catch (\Exception $exception) {
|
||||
$this->_handleException($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,7 +135,18 @@ class Customers extends API_V1_Controller {
|
|||
* @return @return \EA\Engine\Api\V1\Response Returns data response.
|
||||
*/
|
||||
public function delete($id) {
|
||||
try {
|
||||
$result = $this->customers_model->delete($id);
|
||||
|
||||
$response = new Response([
|
||||
'code' => 200,
|
||||
'message' => 'Appointment was deleted successfully!'
|
||||
]);
|
||||
|
||||
$response->output();
|
||||
} catch (\Exception $exception) {
|
||||
$this->_handleException($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue