From a9b43ea18dc1a5f8460c0c8e9b0d02a3ffe90a0b Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sun, 10 Jul 2016 16:56:43 +0200 Subject: [PATCH] Implemented settings API resource controller and parser. --- src/application/config/routes.php | 8 +- .../controllers/api/v1/Settings.php | 104 ++++++++++++++---- src/engine/Api/V1/Parsers/Settings.php | 55 +++++++++ test/php/engine/Api/V1/fixtures/setting.json | 1 - 4 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 src/engine/Api/V1/Parsers/Settings.php diff --git a/src/application/config/routes.php b/src/application/config/routes.php index e5a76aed..7d14c18d 100644 --- a/src/application/config/routes.php +++ b/src/application/config/routes.php @@ -60,8 +60,7 @@ $resources = [ 'categories', 'admins', 'providers', - 'secretaries', - 'settings' + 'secretaries' ]; foreach($resources as $resource) { @@ -72,5 +71,10 @@ foreach($resources as $resource) { $route['api/v1/' . $resource . '/(:num)']['get'] = 'api/v1/' . $resource . '/get/$1'; } +$route['api/v1/settings']['get'] = 'api/v1/settings/get'; +$route['api/v1/settings/(:any)']['get'] = 'api/v1/settings/get/$1'; +$route['api/v1/settings/(:any)']['put'] = 'api/v1/settings/put/$1'; +$route['api/v1/settings/(:any)']['delete'] = 'api/v1/settings/delete/$1'; + /* End of file routes.php */ /* Location: ./application/config/routes.php */ diff --git a/src/application/controllers/api/v1/Settings.php b/src/application/controllers/api/v1/Settings.php index 065e4029..cff2fa38 100644 --- a/src/application/controllers/api/v1/Settings.php +++ b/src/application/controllers/api/v1/Settings.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; + /** * Settings Controller * @@ -20,53 +24,109 @@ require_once __DIR__ . '/API_V1_Controller.php'; * @subpackage API */ class Settings extends API_V1_Controller { + /** + * Settings Resource Parser + * + * @var \EA\Engine\Api\V1\Parsers\Settings + */ + protected $parser; + /** * Class Constructor */ public function __construct() { parent::__construct(); + $this->load->model('settings_model'); + $this->parser = new \EA\Engine\Api\V1\Parsers\Settings; } /** * GET API Method * - * @param int $id Optional (null), the record ID to be returned. - * - * @return \EA\Engine\Api\V1\Response Returns data response. + * @param string $name Optional (null), the setting name to be returned. */ - public function get($id = null) { - - } + public function get($name = null) { + try { + $settings = $this->settings_model->get_settings(); - /** - * POST API Method - * - * @return @return \EA\Engine\Api\V1\Response Returns data response. - */ - public function post() { - + if ($name !== null) { + $setting = null; + + foreach ($settings as $entry) { + if ($entry['name'] === $name) { + $setting = $entry; + break; + } + } + + if (empty($setting)) { + $this->_throwRecordNotFound(); + } + + unset($setting['id']); + + $settings = [ + $setting + ]; + } + + $response = new Response($settings); + + $response->encode($this->parser) + ->search() + ->sort() + ->paginate() + ->minimize() + ->singleEntry($name) + ->output(); + + } catch(\Exception $exception) { + exit($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 string $name The setting name to be updated. */ - public function put($id) { - + public function put($name) { + try { + $request = new Request(); + $value = $request->getBody()['value']; + $this->settings_model->set_setting($name, $value); + + // Fetch the updated object from the database and return it to the client. + $response = new Response([ + [ + 'name' => $name, + 'value' => $value + ] + ]); + $response->encode($this->parser)->singleEntry($name)->output(); + } catch(\Exception $exception) { + exit($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 string $name The setting name to be deleted. */ - public function delete($id) { + public function delete($name) { + try { + $result = $this->settings_model->remove_setting($name); + $response = new Response([ + 'code' => 200, + 'message' => 'Record was deleted successfully!' + ]); + + $response->output(); + } catch(\Exception $exception) { + exit($this->_handleException($exception)); + } } } diff --git a/src/engine/Api/V1/Parsers/Settings.php b/src/engine/Api/V1/Parsers/Settings.php new file mode 100644 index 00000000..97e84677 --- /dev/null +++ b/src/engine/Api/V1/Parsers/Settings.php @@ -0,0 +1,55 @@ + + * @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; + +/** + * Settings Parser + * + * This class will handle the encoding and decoding from the API requests. + */ +class Settings implements ParsersInterface { + /** + * Encode Response Array + * + * @param array &$response The response to be encoded. + */ + public function encode(array &$response) { + $encodedResponse = [ + 'name' => $response['name'], + 'value' => $response['value'] + ]; + + $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['name'])) { + $decodedRequest['name'] = $request['name']; + } + + if (!empty($request['value'])) { + $decodedRequest['value'] = $request['value']; + } + + $request = $decodedRequest; + } +} diff --git a/test/php/engine/Api/V1/fixtures/setting.json b/test/php/engine/Api/V1/fixtures/setting.json index c45fe3c1..8b2ea618 100644 --- a/test/php/engine/Api/V1/fixtures/setting.json +++ b/test/php/engine/Api/V1/fixtures/setting.json @@ -1,5 +1,4 @@ { - "id": 1, "name": "book_advance_timeout", "value": "100" }