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',
'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 */

View file

@ -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));
}
}
}

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",
"value": "100"
}