mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-26 01:43:06 +03:00
Implemented admins API resource controller and parser.
This commit is contained in:
parent
00203789bc
commit
655c400750
3 changed files with 212 additions and 12 deletions
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Admins Controller
|
* Admins Controller
|
||||||
*
|
*
|
||||||
|
@ -20,53 +24,125 @@ require_once __DIR__ . '/API_V1_Controller.php';
|
||||||
* @subpackage API
|
* @subpackage API
|
||||||
*/
|
*/
|
||||||
class Admins extends API_V1_Controller {
|
class Admins extends API_V1_Controller {
|
||||||
|
/**
|
||||||
|
* Admins Resource Parser
|
||||||
|
*
|
||||||
|
* @var \EA\Engine\Api\V1\Parsers\Admins
|
||||||
|
*/
|
||||||
|
protected $parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Constructor
|
* Class Constructor
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
$this->load->model('admins_model');
|
||||||
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Admins;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET API Method
|
* GET API Method
|
||||||
*
|
*
|
||||||
* @param int $id Optional (null), the record ID to be returned.
|
* @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) {
|
public function get($id = null) {
|
||||||
|
try {
|
||||||
|
$condition = $id !== null ? 'id = ' . $id : null;
|
||||||
|
$admins = $this->admins_model->get_batch($condition);
|
||||||
|
|
||||||
|
if ($id !== null && count($admins) === 0) {
|
||||||
|
$this->_throwRecordNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = new Response($admins);
|
||||||
|
|
||||||
|
$response->encode($this->parser)
|
||||||
|
->search()
|
||||||
|
->sort()
|
||||||
|
->paginate()
|
||||||
|
->minimize()
|
||||||
|
->singleEntry($id)
|
||||||
|
->output();
|
||||||
|
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
$this->_handleException($exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POST API Method
|
* POST API Method
|
||||||
*
|
|
||||||
* @return @return \EA\Engine\Api\V1\Response Returns data response.
|
|
||||||
*/
|
*/
|
||||||
public function post() {
|
public function post() {
|
||||||
|
try {
|
||||||
|
// Insert the admin to the database.
|
||||||
|
$request = new Request();
|
||||||
|
$admin = $request->getBody();
|
||||||
|
$this->parser->decode($admin);
|
||||||
|
|
||||||
|
if (isset($admin['id'])) {
|
||||||
|
unset($admin['id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = $this->admins_model->add($admin);
|
||||||
|
|
||||||
|
// Fetch the new object from the database and return it to the client.
|
||||||
|
$batch = $this->admins_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
|
* PUT API Method
|
||||||
*
|
*
|
||||||
* @param int $id The record ID to be updated.
|
* @param int $id The record ID to be updated.
|
||||||
*
|
|
||||||
* @return @return \EA\Engine\Api\V1\Response Returns data response.
|
|
||||||
*/
|
*/
|
||||||
public function put($id) {
|
public function put($id) {
|
||||||
|
try {
|
||||||
|
// Update the admin record.
|
||||||
|
$batch = $this->admins_model->get_batch('id = ' . $id);
|
||||||
|
|
||||||
|
if ($id !== null && count($batch) === 0) {
|
||||||
|
$this->_throwRecordNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = new Request();
|
||||||
|
$updatedAdmin = $request->getBody();
|
||||||
|
$baseAdmin = $batch[0];
|
||||||
|
$this->parser->decode($updatedAdmin, $baseAdmin);
|
||||||
|
$updatedAdmin['id'] = $id;
|
||||||
|
$id = $this->admins_model->add($updatedAdmin);
|
||||||
|
|
||||||
|
// Fetch the updated object from the database and return it to the client.
|
||||||
|
$batch = $this->admins_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
|
* DELETE API Method
|
||||||
*
|
*
|
||||||
* @param int $id The record ID to be deleted.
|
* @param int $id The record ID to be deleted.
|
||||||
*
|
|
||||||
* @return @return \EA\Engine\Api\V1\Response Returns data response.
|
|
||||||
*/
|
*/
|
||||||
public function delete($id) {
|
public function delete($id) {
|
||||||
|
try {
|
||||||
|
$result = $this->admins_model->delete($id);
|
||||||
|
|
||||||
|
$response = new Response([
|
||||||
|
'code' => 200,
|
||||||
|
'message' => 'Record was deleted successfully!'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->output();
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
$this->_handleException($exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
123
src/engine/Api/V1/Parsers/Admins.php
Normal file
123
src/engine/Api/V1/Parsers/Admins.php
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admins Parser
|
||||||
|
*
|
||||||
|
* This class will handle the encoding and decoding from the API requests.
|
||||||
|
*/
|
||||||
|
class Admins 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'],
|
||||||
|
'settings' => [
|
||||||
|
'username' => $response['settings']['username'],
|
||||||
|
'notifications' => filter_var($response['settings']['notifications'], FILTER_VALIDATE_BOOLEAN)
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$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['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 ($request['settings']['notifications'] !== null) {
|
||||||
|
$decodedRequest['settings']['notifications'] = filter_var($request['settings']['notifications'],
|
||||||
|
FILTER_VALIDATE_BOOLEAN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = $decodedRequest;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,10 +7,11 @@
|
||||||
"phone": "0123456789-1",
|
"phone": "0123456789-1",
|
||||||
"address": "Some Str. 123",
|
"address": "Some Str. 123",
|
||||||
"city": "Some City",
|
"city": "Some City",
|
||||||
|
"state": "Some City",
|
||||||
"zip": "12345",
|
"zip": "12345",
|
||||||
"notes": "Test admin notes.",
|
"notes": "Test admin notes.",
|
||||||
"settings":{
|
"settings":{
|
||||||
"username":"chrisdoe",
|
"username": "chrisdoe",
|
||||||
"notifications": true
|
"notifications": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue