Implemented settings API resource controller and parser.

This commit is contained in:
Alex Tselegidis 2016-07-10 16:56:43 +02:00
parent b583f444e5
commit a9b43ea18d
4 changed files with 143 additions and 25 deletions

View file

@ -60,8 +60,7 @@ $resources = [
'categories', 'categories',
'admins', 'admins',
'providers', 'providers',
'secretaries', 'secretaries'
'settings'
]; ];
foreach($resources as $resource) { 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/' . $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 */ /* End of file routes.php */
/* Location: ./application/config/routes.php */ /* Location: ./application/config/routes.php */

View file

@ -13,6 +13,10 @@
require_once __DIR__ . '/API_V1_Controller.php'; 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 * Settings Controller
* *
@ -20,53 +24,109 @@ require_once __DIR__ . '/API_V1_Controller.php';
* @subpackage API * @subpackage API
*/ */
class Settings extends API_V1_Controller { class Settings extends API_V1_Controller {
/**
* Settings Resource Parser
*
* @var \EA\Engine\Api\V1\Parsers\Settings
*/
protected $parser;
/** /**
* Class Constructor * Class Constructor
*/ */
public function __construct() { public function __construct() {
parent::__construct(); parent::__construct();
$this->load->model('settings_model');
$this->parser = new \EA\Engine\Api\V1\Parsers\Settings;
} }
/** /**
* GET API Method * GET API Method
* *
* @param int $id Optional (null), the record ID to be returned. * @param string $name Optional (null), the setting name to be returned.
*
* @return \EA\Engine\Api\V1\Response Returns data response.
*/ */
public function get($id = null) { public function get($name = null) {
try {
$settings = $this->settings_model->get_settings();
if ($name !== null) {
$setting = null;
foreach ($settings as $entry) {
if ($entry['name'] === $name) {
$setting = $entry;
break;
}
} }
/** if (empty($setting)) {
* POST API Method $this->_throwRecordNotFound();
* }
* @return @return \EA\Engine\Api\V1\Response Returns data response.
*/
public function post() {
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 * PUT API Method
* *
* @param int $id The record ID to be updated. * @param string $name The setting name to be updated.
*
* @return @return \EA\Engine\Api\V1\Response Returns data response.
*/ */
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 * DELETE API Method
* *
* @param int $id The record ID to be deleted. * @param string $name The setting name to be deleted.
*
* @return @return \EA\Engine\Api\V1\Response Returns data response.
*/ */
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));
}
} }
} }

View file

@ -0,0 +1,55 @@
<?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\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;
}
}

View file

@ -1,5 +1,4 @@
{ {
"id": 1,
"name": "book_advance_timeout", "name": "book_advance_timeout",
"value": "100" "value": "100"
} }