Implemented the POST method of the appointments resource.

This commit is contained in:
Alex Tselegidis 2016-07-09 21:53:28 +02:00
parent de4bc1217a
commit a40bb1ae4c
5 changed files with 106 additions and 53 deletions

View file

@ -24,11 +24,11 @@ use \EA\Engine\Types\NonEmptyString;
*/
class Appointments extends API_V1_Controller {
/**
* Appointments Resource Formatter
* Appointments Resource Parser
*
* @var \EA\Engine\Api\V1\Formatters\Appointments
* @var \EA\Engine\Api\V1\Parsers\Appointments
*/
protected $formatter;
protected $parser;
/**
* Class Constructor
@ -36,7 +36,7 @@ class Appointments extends API_V1_Controller {
public function __construct() {
parent::__construct();
$this->load->model('appointments_model');
$this->formatter = new \EA\Engine\Api\V1\Formatters\Appointments;
$this->parser = new \EA\Engine\Api\V1\Parsers\Appointments;
}
/**
@ -46,12 +46,10 @@ class Appointments extends API_V1_Controller {
*/
public function get($id = null) {
$condition = $id !== null ? 'id = ' . $id : null;
$appointments = $this->appointments_model->get_batch($condition);
$response = new Response($appointments);
$response->format($this->formatter)->search()->sort()->paginate()->minimize();
$response = new Response($appointments);
$response->encode($this->parser)->search()->sort()->paginate()->minimize();
if ($id !== null) {
$response->singleEntry();
@ -64,7 +62,13 @@ class Appointments extends API_V1_Controller {
* POST API Method
*/
public function post() {
$request = json_decode(file_get_contents('php://input'), true);
$this->parser->decode($request);
$id = $this->appointments_model->add($request);
$appointments = $this->appointments_model->get_batch('id = ' . $id);
$response = new Response($appointments);
$status = new NonEmptyString('201 Created');
$response->encode($this->parser)->singleEntry()->output($status);
}
/**

View file

@ -1,37 +0,0 @@
<?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\Formatters;
class Appointments implements FormattersInterface {
public function format(array &$response) {
$temporaryResponse = [];
foreach ($response as $entry) {
$temporaryResponse[] = [
'id' => $entry['id'],
'book' => $entry['book_datetime'],
'start' => $entry['start_datetime'],
'end' => $entry['end_datetime'],
'hash' => $entry['hash'],
'notes' => $entry['notes'],
'customerId' => $entry['id_users_customer'],
'providerId' => $entry['id_users_provider'],
'serviceId' => $entry['id_services'],
'googleCalendarId' => $entry['id_google_calendar']
];
}
$response = $temporaryResponse;
}
}

View file

@ -0,0 +1,81 @@
<?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;
class Appointments implements ParsersInterface {
public function encode(array &$response) {
$encodedResponse = [
'id' => $response['id'],
'book' => $response['book_datetime'],
'start' => $response['start_datetime'],
'end' => $response['end_datetime'],
'hash' => $response['hash'],
'notes' => $response['notes'],
'customerId' => $response['id_users_customer'],
'providerId' => $response['id_users_provider'],
'serviceId' => $response['id_services'],
'googleCalendarId' => $response['id_google_calendar']
];
$response = $encodedResponse;
}
public function decode(array &$request) {
$decodedRequest = [];
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['hash'])) {
$decodedRequest['hash'] = $request['hash'];
}
if (!empty($request['notes'])) {
$decodedRequest['notes'] = $request['notes'];
}
if (!empty($request['customerId'])) {
$decodedRequest['id_users_customer'] = $request['customerId'];
}
if (!empty($request['providerId'])) {
$decodedRequest['id_users_provider'] = $request['providerId'];
}
if (!empty($request['serviceId'])) {
$decodedRequest['id_services'] = $request['serviceId'];
}
if (!empty($request['googleCalendarId'])) {
$decodedRequest['id_google_calendar'] = $request['googleCalendarId'];
}
$decodedRequest['is_unavailable'] = false;
$request = $decodedRequest;
}
}

View file

@ -11,8 +11,9 @@
* @since v1.2.0
* ---------------------------------------------------------------------------- */
namespace EA\Engine\Api\V1\Formatters;
namespace EA\Engine\Api\V1\Parsers;
interface FormattersInterface {
public function format(array &$response);
interface ParsersInterface {
public function encode(array &$response);
public function decode(array &$request);
}

View file

@ -13,6 +13,8 @@
namespace EA\Engine\Api\V1;
use EA\Engine\Types\NonEmptyString;
/**
* API v1 Response
*
@ -42,14 +44,16 @@ class Response {
}
/**
* Format the response entries to the API compatible structure.
* Encode the response entries to the API compatible structure.
*
* @param \Formatters\FormattersInterface $formatter Provide the corresponding formatter class.
* @param \Parsers\ParsersInterface $parser Provide the corresponding parser class.
*
* @return \EA\Engine\Api\V1\Response
*/
public function format(Formatters\FormattersInterface $formatter) {
$formatter->format($this->response);
public function encode(Parsers\ParsersInterface $parser) {
foreach ($this->response as &$entry) {
$parser->encode($entry);
}
return $this;
}