From e2817957bef93b41d0fca8e0cda67bec643d5314 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sun, 10 Jul 2016 16:13:12 +0200 Subject: [PATCH] Implemented categories API resource controller and parser. --- .../controllers/api/v1/Categories.php | 102 +++++++++++++++--- src/engine/Api/V1/Parsers/Categories.php | 60 +++++++++++ test/php/engine/Api/V1/fixtures/category.json | 5 + 3 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 src/engine/Api/V1/Parsers/Categories.php create mode 100644 test/php/engine/Api/V1/fixtures/category.json diff --git a/src/application/controllers/api/v1/Categories.php b/src/application/controllers/api/v1/Categories.php index 0ccc8063..1b2e7241 100644 --- a/src/application/controllers/api/v1/Categories.php +++ b/src/application/controllers/api/v1/Categories.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; + /** * Categories Controller * @@ -20,53 +24,125 @@ require_once __DIR__ . '/API_V1_Controller.php'; * @subpackage API */ class Categories extends API_V1_Controller { + /** + * Categories Resource Parser + * + * @var \EA\Engine\Api\V1\Parsers\Categories + */ + protected $parser; + /** * Class Constructor */ public function __construct() { parent::__construct(); + $this->load->model('services_model'); + $this->parser = new \EA\Engine\Api\V1\Parsers\Categories; } /** * 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 : ''; + $categories = $this->services_model->get_all_categories($condition); + + if ($id !== null && count($categories) === 0) { + $this->_throwRecordNotFound(); + } + + $response = new Response($categories); + + $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. + * POST API Method */ public function post() { - + try { + // Insert the category to the database. + $request = new Request(); + $category = $request->getBody(); + $this->parser->decode($category); + + if (isset($category['id'])) { + unset($category['id']); + } + + $id = $this->services_model->add_category($category); + + // Fetch the new object from the database and return it to the client. + $batch = $this->services_model->get_all_categories('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); + } } /** * PUT API Method * - * @param int $id The record ID to be updated. - * - * @return @return \EA\Engine\Api\V1\Response Returns data response. + * @param int $id The record ID to be updated. */ public function put($id) { + try { + // Update the category record. + $batch = $this->services_model->get_all_categories('id = ' . $id); + if ($id !== null && count($batch) === 0) { + $this->_throwRecordNotFound(); + } + + $request = new Request(); + $updatedCategory = $request->getBody(); + $baseCategory = $batch[0]; + $this->parser->decode($updatedCategory, $baseCategory); + $updatedCategory['id'] = $id; + $id = $this->services_model->add_category($updatedCategory); + + // Fetch the updated object from the database and return it to the client. + $batch = $this->services_model->get_all_categories('id = ' . $id); + $response = new Response($batch); + $response->encode($this->parser)->singleEntry($id)->output(); + } catch (\Exception $exception) { + $this->_handleException($exception); + } } /** * DELETE API Method * - * @param int $id The record ID to be deleted. - * - * @return @return \EA\Engine\Api\V1\Response Returns data response. + * @param int $id The record ID to be deleted. */ public function delete($id) { + try { + $result = $this->services_model->delete_category($id); + $response = new Response([ + 'code' => 200, + 'message' => 'Record was deleted successfully!' + ]); + + $response->output(); + } catch (\Exception $exception) { + $this->_handleException($exception); + } } } diff --git a/src/engine/Api/V1/Parsers/Categories.php b/src/engine/Api/V1/Parsers/Categories.php new file mode 100644 index 00000000..295a8fb1 --- /dev/null +++ b/src/engine/Api/V1/Parsers/Categories.php @@ -0,0 +1,60 @@ + + * @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\Parsers; + +/** + * Categories Parser + * + * This class will handle the encoding and decoding from the API requests. + */ +class Categories implements ParsersInterface { + /** + * Encode Response Array + * + * @param array &$response The response to be encoded. + */ + public function encode(array &$response) { + $encodedResponse = [ + 'id' => $response['id'] !== null ? (int)$response['id'] : null, + 'name' => $response['name'], + 'description' => $response['description'] + ]; + + $response = $encodedResponse; + } + + /** + * Decode Request + * + * @param array &$request The request to be decoded. + * @param array $base Optional (null), if provided it will be used as a base array. + */ + public function decode(array &$request, array $base = null) { + $decodedRequest = $base ?: []; + + if (!empty($request['id'])) { + $decodedRequest['id'] = $request['id']; + } + + if (!empty($request['name'])) { + $decodedRequest['name'] = $request['name']; + } + + if (!empty($request['description'])) { + $decodedRequest['description'] = $request['description']; + } + + $request = $decodedRequest; + } +} diff --git a/test/php/engine/Api/V1/fixtures/category.json b/test/php/engine/Api/V1/fixtures/category.json new file mode 100644 index 00000000..25276bd7 --- /dev/null +++ b/test/php/engine/Api/V1/fixtures/category.json @@ -0,0 +1,5 @@ +{ + "id": 5, + "name": "Test Category", + "description": "This category includes test services" +}