From c39c7c3df2b7cce6f6810d9709a7e186b181c4c8 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sun, 10 Jul 2016 13:00:07 +0200 Subject: [PATCH] Implemented customers API resource controller. --- .../controllers/api/v1/Customers.php | 92 +++++++++++++++++-- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/src/application/controllers/api/v1/Customers.php b/src/application/controllers/api/v1/Customers.php index cdf1ffc8..9a5c6404 100644 --- a/src/application/controllers/api/v1/Customers.php +++ b/src/application/controllers/api/v1/Customers.php @@ -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); + } } }