mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-22 07:52:29 +03:00
Implemented the services API resource controller.
This commit is contained in:
parent
17e74d80eb
commit
d8c5eae39f
3 changed files with 169 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;
|
||||
|
||||
/**
|
||||
* Services Controller
|
||||
*
|
||||
|
@ -20,53 +24,125 @@ require_once __DIR__ . '/API_V1_Controller.php';
|
|||
* @subpackage API
|
||||
*/
|
||||
class Services extends API_V1_Controller {
|
||||
/**
|
||||
* Services Resource Parser
|
||||
*
|
||||
* @var \EA\Engine\Api\V1\Parsers\Services
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->model('services_model');
|
||||
$this->parser = new \EA\Engine\Api\V1\Parsers\Services;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET API Method
|
||||
*
|
||||
* @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) {
|
||||
|
||||
try {
|
||||
$condition = $id !== null ? 'id = ' . $id : null;
|
||||
$services = $this->services_model->get_batch($condition);
|
||||
|
||||
if ($id !== null && count($services) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$response = new Response($services);
|
||||
|
||||
$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.
|
||||
* POST API Method
|
||||
*/
|
||||
public function post() {
|
||||
|
||||
try {
|
||||
// Insert the service to the database.
|
||||
$request = new Request();
|
||||
$service = $request->getBody();
|
||||
$this->parser->decode($service);
|
||||
|
||||
if (isset($service['id'])) {
|
||||
unset($service['id']);
|
||||
}
|
||||
|
||||
$id = $this->services_model->add($service);
|
||||
|
||||
// Fetch the new object from the database and return it to the client.
|
||||
$batch = $this->services_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.
|
||||
*/
|
||||
public function put($id) {
|
||||
try {
|
||||
// Update the service record.
|
||||
$batch = $this->services_model->get_batch('id = ' . $id);
|
||||
|
||||
if ($id !== null && count($batch) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$request = new Request();
|
||||
$updatedService = $request->getBody();
|
||||
$baseService = $batch[0];
|
||||
$this->parser->decode($updatedService, $baseService);
|
||||
$updatedService['id'] = $id;
|
||||
$id = $this->services_model->add($updatedService);
|
||||
|
||||
// Fetch the updated object from the database and return it to the client.
|
||||
$batch = $this->services_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.
|
||||
*/
|
||||
public function delete($id) {
|
||||
try {
|
||||
$result = $this->services_model->delete($id);
|
||||
|
||||
$response = new Response([
|
||||
'code' => 200,
|
||||
'message' => 'Record was deleted successfully!'
|
||||
]);
|
||||
|
||||
$response->output();
|
||||
} catch (\Exception $exception) {
|
||||
$this->_handleException($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -114,8 +114,8 @@ class Services_Model extends CI_Model {
|
|||
}
|
||||
}
|
||||
|
||||
// Check if service category id is valid (only when present)
|
||||
if ($service['id_service_categories'] != NULL) {
|
||||
// Check if service category id is valid (only when present).
|
||||
if (!empty($service['id_service_categories'])) {
|
||||
$num_rows = $this->db->get_where('ea_service_categories',
|
||||
array('id' => $service['id_service_categories']))->num_rows();
|
||||
if ($num_rows == 0) {
|
||||
|
|
80
src/engine/Api/V1/Parsers/Services.php
Normal file
80
src/engine/Api/V1/Parsers/Services.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Services Parser
|
||||
*
|
||||
* This class will handle the encoding and decoding from the API requests.
|
||||
*/
|
||||
class Services 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,
|
||||
'name' => $response['name'],
|
||||
'duration' => $response['duration'],
|
||||
'price' => $response['price'],
|
||||
'currency' => $response['currency'],
|
||||
'description' => $response['description'],
|
||||
'categoryId' => $response['id_service_categories'] !== null ? (int)$response['id_service_categories'] : null
|
||||
];
|
||||
|
||||
$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['name'])) {
|
||||
$decodedRequest['name'] = $request['name'];
|
||||
}
|
||||
|
||||
if (!empty($request['duration'])) {
|
||||
$decodedRequest['duration'] = $request['duration'];
|
||||
}
|
||||
|
||||
if (!empty($request['price'])) {
|
||||
$decodedRequest['price'] = $request['price'];
|
||||
}
|
||||
|
||||
if (!empty($request['currency'])) {
|
||||
$decodedRequest['currency'] = $request['currency'];
|
||||
}
|
||||
|
||||
if (!empty($request['description'])) {
|
||||
$decodedRequest['description'] = $request['description'];
|
||||
}
|
||||
|
||||
if (!empty($request['categoryId'])) {
|
||||
$decodedRequest['id_service_categories'] = $request['categoryId'];
|
||||
}
|
||||
|
||||
$request = $decodedRequest;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue