mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-12-22 14:42:23 +03:00
Implemented unavailabilities API resource controller and parser.
This commit is contained in:
parent
e2817957be
commit
b583f444e5
2 changed files with 171 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;
|
||||
|
||||
/**
|
||||
* Unavailabilities Controller
|
||||
*
|
||||
|
@ -20,53 +24,125 @@ require_once __DIR__ . '/API_V1_Controller.php';
|
|||
* @subpackage API
|
||||
*/
|
||||
class Unavailabilities extends API_V1_Controller {
|
||||
/**
|
||||
* Unavailabilities Resource Parser
|
||||
*
|
||||
* @var \EA\Engine\Api\V1\Parsers\Unavailabilities
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->model('appointments_model');
|
||||
$this->parser = new \EA\Engine\Api\V1\Parsers\Unavailabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 : 'is_unavailable = 1';
|
||||
$unavailabilities = $this->appointments_model->get_batch($condition);
|
||||
|
||||
if ($id !== null && count($unavailabilities) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$response = new Response($unavailabilities);
|
||||
|
||||
$response->encode($this->parser)
|
||||
->search()
|
||||
->sort()
|
||||
->paginate()
|
||||
->minimize()
|
||||
->singleEntry($id)
|
||||
->output();
|
||||
|
||||
} catch(\Exception $exception) {
|
||||
exit($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 appointment to the database.
|
||||
$request = new Request();
|
||||
$unavailability = $request->getBody();
|
||||
$this->parser->decode($unavailability);
|
||||
|
||||
if (isset($unavailability['id'])) {
|
||||
unset($unavailability['id']);
|
||||
}
|
||||
|
||||
$id = $this->appointments_model->add_unavailable($unavailability);
|
||||
|
||||
// Fetch the new object from the database and return it to the client.
|
||||
$batch = $this->appointments_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) {
|
||||
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 int $id The record ID to be updated.
|
||||
*/
|
||||
public function put($id) {
|
||||
try {
|
||||
// Update the appointment record.
|
||||
$batch = $this->appointments_model->get_batch('id = ' . $id);
|
||||
|
||||
if ($id !== null && count($batch) === 0) {
|
||||
$this->_throwRecordNotFound();
|
||||
}
|
||||
|
||||
$request = new Request();
|
||||
$updatedUnavailability = $request->getBody();
|
||||
$baseUnavailability = $batch[0];
|
||||
$this->parser->decode($updatedUnavailability, $baseUnavailability);
|
||||
$updatedUnavailability['id'] = $id;
|
||||
$id = $this->appointments_model->add_unavailable($updatedUnavailability);
|
||||
|
||||
// Fetch the updated object from the database and return it to the client.
|
||||
$batch = $this->appointments_model->get_batch('id = ' . $id);
|
||||
$response = new Response($batch);
|
||||
$response->encode($this->parser)->singleEntry($id)->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.
|
||||
*/
|
||||
public function delete($id) {
|
||||
try {
|
||||
$result = $this->appointments_model->delete_unavailable($id);
|
||||
|
||||
$response = new Response([
|
||||
'code' => 200,
|
||||
'message' => 'Record was deleted successfully!'
|
||||
]);
|
||||
|
||||
$response->output();
|
||||
} catch(\Exception $exception) {
|
||||
exit($this->_handleException($exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
82
src/engine/Api/V1/Parsers/Unavailabilities.php
Normal file
82
src/engine/Api/V1/Parsers/Unavailabilities.php
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Unavailabilities Parser
|
||||
*
|
||||
* This class will handle the encoding and decoding from the API requests.
|
||||
*/
|
||||
class Unavailabilities 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,
|
||||
'book' => $response['book_datetime'],
|
||||
'start' => $response['start_datetime'],
|
||||
'end' => $response['end_datetime'],
|
||||
'notes' => $response['notes'],
|
||||
'providerId' => $response['id_users_provider'] !== null ? (int)$response['id_users_provider'] : null,
|
||||
'googleCalendarId' => $response['id_google_calendar'] !== null ? (int)$response['id_google_calendar'] : 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['book'])) {
|
||||
$decodedRequest['book_datetime'] = $request['book'];
|
||||
}
|
||||
|
||||
if (!empty($request['start'])) {
|
||||
$decodedRequest['start_datetime'] = $request['start'];
|
||||
}
|
||||
|
||||
if (!empty($request['end'])) {
|
||||
$decodedRequest['end_datetime'] = $request['end'];
|
||||
}
|
||||
|
||||
if (!empty($request['notes'])) {
|
||||
$decodedRequest['notes'] = $request['notes'];
|
||||
}
|
||||
|
||||
if (!empty($request['providerId'])) {
|
||||
$decodedRequest['id_users_provider'] = $request['providerId'];
|
||||
}
|
||||
|
||||
if (!empty($request['googleCalendarId'])) {
|
||||
$decodedRequest['id_google_calendar'] = $request['googleCalendarId'];
|
||||
}
|
||||
|
||||
$decodedRequest['is_unavailable'] = true;
|
||||
|
||||
$request = $decodedRequest;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue