forked from mirrors/easyappointments
Implemented providers API resource controller and parser.
This commit is contained in:
parent
ccf92fe784
commit
0bd1da56e1
2 changed files with 248 additions and 13 deletions
|
@ -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;
|
||||
|
||||
/**
|
||||
* Providers Controller
|
||||
*
|
||||
|
@ -20,53 +24,125 @@ require_once __DIR__ . '/API_V1_Controller.php';
|
|||
* @subpackage API
|
||||
*/
|
||||
class Providers extends API_V1_Controller {
|
||||
/**
|
||||
* Providers Resource Parser
|
||||
*
|
||||
* @var \EA\Engine\Api\V1\Parsers\Providers
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->model('providers_model');
|
||||
$this->parser = new \EA\Engine\Api\V1\Parsers\Providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET API Method
|
||||
*
|
||||
* @param int $id Optional (null), the record ID to be returned.
|
||||
*
|
||||
* @return \EA\Engine\Api\V1\Response Returns data response.
|
||||
* @param int $id Optional (null), the record ID to be returned.
|
||||
*/
|
||||
public function get($id = null) {
|
||||
|
||||
try {
|
||||
$condition = $id !== null ? 'id = ' . $id : null;
|
||||
$providers = $this->providers_model->get_batch($condition);
|
||||
|
||||
if ($id !== null && count($providers) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$response = new Response($providers);
|
||||
|
||||
$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 provider to the database.
|
||||
$request = new Request();
|
||||
$provider = $request->getBody();
|
||||
$this->parser->decode($provider);
|
||||
|
||||
if (isset($provider['id'])) {
|
||||
unset($provider['id']);
|
||||
}
|
||||
|
||||
$id = $this->providers_model->add($provider);
|
||||
|
||||
// Fetch the new object from the database and return it to the client.
|
||||
$batch = $this->providers_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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 provider record.
|
||||
$batch = $this->providers_model->get_batch('id = ' . $id);
|
||||
|
||||
if ($id !== null && count($batch) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$request = new Request();
|
||||
$updatedProvider = $request->getBody();
|
||||
$baseProvider = $batch[0];
|
||||
$this->parser->decode($updatedProvider, $baseProvider);
|
||||
$updatedProvider['id'] = $id;
|
||||
$id = $this->providers_model->add($updatedProvider);
|
||||
|
||||
// Fetch the updated object from the database and return it to the client.
|
||||
$batch = $this->providers_model->get_batch('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->providers_model->delete($id);
|
||||
|
||||
$response = new Response([
|
||||
'code' => 200,
|
||||
'message' => 'Record was deleted successfully!'
|
||||
]);
|
||||
|
||||
$response->output();
|
||||
} catch (\Exception $exception) {
|
||||
$this->_handleException($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
159
src/engine/Api/V1/Parsers/Providers.php
Normal file
159
src/engine/Api/V1/Parsers/Providers.php
Normal file
|
@ -0,0 +1,159 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Providers Parser
|
||||
*
|
||||
* This class will handle the encoding and decoding from the API requests.
|
||||
*/
|
||||
class Providers 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,
|
||||
'firstname' => $response['first_name'],
|
||||
'lastname' => $response['last_name'],
|
||||
'email' => $response['email'],
|
||||
'mobile' => $response['mobile_number'],
|
||||
'phone' => $response['phone_number'],
|
||||
'address' => $response['address'],
|
||||
'city' => $response['city'],
|
||||
'state' => $response['state'],
|
||||
'zip' => $response['zip_code'],
|
||||
'notes' => $response['notes'],
|
||||
'services' => $response['services'],
|
||||
'settings' => [
|
||||
'username' => $response['settings']['username'],
|
||||
'notifications' => filter_var($response['settings']['notifications'], FILTER_VALIDATE_BOOLEAN),
|
||||
'googleSync' => filter_var($response['settings']['google_sync'], FILTER_VALIDATE_BOOLEAN),
|
||||
'googleCalendar' => $response['settings']['google_calendar'],
|
||||
'googleToken' => $response['settings']['google_token'],
|
||||
'syncFutureDays' => $response['settings']['sync_future_days'] !== null ? (int)$response['settings']['sync_future_days'] : null,
|
||||
'syncPastDays' => $response['settings']['sync_past_days'] !== null ? (int)$response['settings']['sync_past_days'] : null,
|
||||
'workingPlan' => json_decode($response['settings']['working_plan'], true),
|
||||
]
|
||||
];
|
||||
|
||||
$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['firstname'])) {
|
||||
$decodedRequest['first_name'] = $request['firstname'];
|
||||
}
|
||||
|
||||
if (!empty($request['lastname'])) {
|
||||
$decodedRequest['last_name'] = $request['lastname'];
|
||||
}
|
||||
|
||||
if (!empty($request['email'])) {
|
||||
$decodedRequest['email'] = $request['email'];
|
||||
}
|
||||
|
||||
if (!empty($request['mobile'])) {
|
||||
$decodedRequest['mobile_number'] = $request['mobile'];
|
||||
}
|
||||
|
||||
if (!empty($request['phone'])) {
|
||||
$decodedRequest['phone_number'] = $request['phone'];
|
||||
}
|
||||
|
||||
if (!empty($request['address'])) {
|
||||
$decodedRequest['address'] = $request['address'];
|
||||
}
|
||||
|
||||
if (!empty($request['city'])) {
|
||||
$decodedRequest['city'] = $request['city'];
|
||||
}
|
||||
|
||||
if (!empty($request['state'])) {
|
||||
$decodedRequest['state'] = $request['state'];
|
||||
}
|
||||
|
||||
if (!empty($request['zip'])) {
|
||||
$decodedRequest['zip_code'] = $request['zip'];
|
||||
}
|
||||
|
||||
if (!empty($request['notes'])) {
|
||||
$decodedRequest['notes'] = $request['notes'];
|
||||
}
|
||||
|
||||
if (!empty($request['services'])) {
|
||||
$decodedRequest['services'] = $request['services'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings'])) {
|
||||
if (empty($decodedRequest['settings'])) {
|
||||
$decodedRequest['settings'] = [];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['username'])) {
|
||||
$decodedRequest['settings']['username'] = $request['settings']['username'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['password'])) {
|
||||
$decodedRequest['settings']['password'] = $request['settings']['password'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['notifications'])) {
|
||||
$decodedRequest['settings']['notifications'] = filter_var($request['settings']['notifications'],
|
||||
FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['googleSync'])) {
|
||||
$decodedRequest['settings']['google_sync'] = filter_var($request['settings']['googleSync'],
|
||||
FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['googleCalendar'])) {
|
||||
$decodedRequest['settings']['google_calendar'] = $request['settings']['googleCalendar'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['googleToken'])) {
|
||||
$decodedRequest['settings']['google_token'] = $request['settings']['googleToken'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['syncFutureDays'])) {
|
||||
$decodedRequest['settings']['sync_future_days'] = $request['settings']['syncFutureDays'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['syncPastDays'])) {
|
||||
$decodedRequest['settings']['sync_past_days'] = $request['settings']['syncPastDays'];
|
||||
}
|
||||
|
||||
if (!empty($request['settings']['workingPlan'])) {
|
||||
$decodedRequest['settings']['working_plan'] = json_encode($request['settings']['workingPlan']);
|
||||
}
|
||||
}
|
||||
|
||||
$request = $decodedRequest;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue