Refactored all the API controller classes (and renamed them) to work well on top of the models

This commit is contained in:
Alex Tselegidis 2021-11-06 15:03:08 +01:00
parent b4f903e724
commit 8603a8ae33
18 changed files with 1772 additions and 1464 deletions

View file

@ -8,7 +8,7 @@
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
@ -16,9 +16,9 @@
*
* @package Controllers
*/
class Admins extends EA_Controller {
class Admins_api_v1 extends EA_Controller {
/**
* Class Constructor
* Admins_api_v1 constructor.
*/
public function __construct()
{
@ -36,16 +36,12 @@ class Admins extends EA_Controller {
}
/**
* Get a single admin or an admin collection.
*
* @param int|null $id Admin ID.
* Get an admin collection.
*/
public function get(int $id = NULL)
public function index()
{
try
{
$where = $id ? ['id' => $id] : NULL;
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
@ -57,7 +53,7 @@ class Admins extends EA_Controller {
$fields = $this->api->request_fields();
$admins = empty($keyword)
? $this->admins_model->get($where, $limit, $offset, $order_by)
? $this->admins_model->get(NULL, $limit, $offset, $order_by)
: $this->admins_model->search($keyword, $limit, $offset, $order_by);
foreach ($admins as &$admin)
@ -70,16 +66,42 @@ class Admins extends EA_Controller {
}
}
$response = $id && ! empty($admins) ? $admins[0] : $admins;
json_response($admins);
}
catch (Throwable $e)
{
json_exception($e);
}
}
if ( ! $response)
/**
* Get a single admin.
*
* @param int|null $id Admin ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$admin = $this->admins_model->find($id);
$this->admins_model->api_encode($admin);
if ( ! empty($fields))
{
response('Not Found', 404);
$this->admins_model->only($admin, $fields);
}
if ( ! $admin)
{
response('', 404);
return;
}
json_response($response);
json_response($admin);
}
catch (Throwable $e)
{
@ -90,7 +112,7 @@ class Admins extends EA_Controller {
/**
* Create an admin.
*/
public function post()
public function store()
{
try
{
@ -105,7 +127,7 @@ class Admins extends EA_Controller {
if ( ! array_key_exists('settings', $admin))
{
throw new Exception('No settings property provided.');
throw new InvalidArgumentException('No settings property provided.');
}
$admin_id = $this->admins_model->save($admin);
@ -127,7 +149,7 @@ class Admins extends EA_Controller {
*
* @param int $id Admin ID.
*/
public function put(int $id)
public function update(int $id)
{
try
{
@ -165,7 +187,7 @@ class Admins extends EA_Controller {
*
* @param int $id Admin ID.
*/
public function delete(int $id)
public function destroy(int $id)
{
try
{
@ -179,6 +201,8 @@ class Admins extends EA_Controller {
}
$this->admins_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{

View file

@ -1,243 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Appointments Controller
*
* @package Controllers
*/
class Appointments extends API_V1_Controller {
/**
* Appointments Resource Parser
*
* @var \EA\Engine\Api\V1\Parsers\Appointments
*/
protected $parser;
/**
* Class Constructor
*/
public function __construct()
{
parent::__construct();
$this->load->model('appointments_model');
$this->load->model('services_model');
$this->load->model('providers_model');
$this->load->model('customers_model');
$this->load->model('settings_model');
$this->load->library('synchronization');
$this->load->library('notifications');
$this->parser = new \EA\Engine\Api\V1\Parsers\Appointments;
}
/**
* GET API Method
*
* @param int $id Optional (null), the record ID to be returned.
*/
public function get($id = NULL)
{
try
{
$where = [
'is_unavailable' => FALSE
];
if ($id !== NULL)
{
$where['id'] = $id;
}
$appointments = $this->appointments_model->get($where, NULL, NULL, NULL, array_key_exists('aggregates', $_GET));
if ($id !== NULL && count($appointments) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($appointments);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the appointment to the database.
$request = new Request();
$appointment = $request->get_body();
$this->parser->decode($appointment);
if (isset($appointment['id']))
{
unset($appointment['id']);
}
// Generate end_datetime based on service duration if this field is not defined
if ( ! isset($appointment['end_datetime']))
{
$service = $this->services_model->find($appointment['id_services']);
if (isset($service['duration']))
{
$end_datetime = new DateTime($appointment['start_datetime']);
$end_datetime->add(new DateInterval('PT' . $service['duration'] . 'M'));
$appointment['end_datetime'] = $end_datetime->format('Y-m-d H:i:s');
}
}
$id = $this->appointments_model->save($appointment);
$appointment = $this->appointments_model->find($id);
$service = $this->services_model->find($appointment['id_services']);
$provider = $this->providers_model->find($appointment['id_users_provider']);
$customer = $this->customers_model->find($appointment['id_users_customer']);
$settings = [
'company_name' => setting('company_name'),
'company_email' => setting('company_email'),
'company_link' => setting('company_link'),
'date_format' => setting('date_format'),
'time_format' => setting('time_format')
];
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings, FALSE);
$this->notifications->notify_appointment_saved($appointment, $service, $provider, $customer, $settings, FALSE);
// Fetch the new object from the database and return it to the client.
$batch = $this->appointments_model->get(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the appointment record.
$batch = $this->appointments_model->get(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updated_appointment = $request->get_body();
$base_appointment = $batch[0];
$this->parser->decode($updated_appointment, $base_appointment);
$updated_appointment['id'] = $id;
$id = $this->appointments_model->save($updated_appointment);
$service = $this->services_model->find($updated_appointment['id_services']);
$provider = $this->providers_model->find($updated_appointment['id_users_provider']);
$customer = $this->customers_model->find($updated_appointment['id_users_customer']);
$settings = [
'company_name' => setting('company_name'),
'company_email' => setting('company_email'),
'company_link' => setting('company_link'),
'date_format' => setting('date_format'),
'time_format' => setting('time_format')
];
$this->synchronization->sync_appointment_saved($updated_appointment, $service, $provider, $customer, $settings, TRUE);
$this->notifications->notify_appointment_saved($updated_appointment, $service, $provider, $customer, $settings, TRUE);
// Fetch the updated object from the database and return it to the client.
$batch = $this->appointments_model->get(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$appointment = $this->appointments_model->find($id);
$service = $this->services_model->find($appointment['id_services']);
$provider = $this->providers_model->find($appointment['id_users_provider']);
$customer = $this->customers_model->find($appointment['id_users_customer']);
$settings = [
'company_name' => setting('company_name'),
'company_email' => setting('company_email'),
'company_link' => setting('company_link'),
'date_format' => setting('date_format'),
'time_format' => setting('time_format')
];
$this->appointments_model->delete($id);
$this->synchronization->sync_appointment_deleted($appointment, $provider);
$this->notifications->notify_appointment_deleted($appointment, $service, $provider, $customer, $settings);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,319 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Appointments API v1 controller.
*
* @package Controllers
*/
class Appointments_api_v1 extends EA_Controller {
/**
* Appointments_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('appointments_model');
$this->load->model('customers_model');
$this->load->model('providers_model');
$this->load->model('services_model');
$this->load->model('settings_model');
$this->load->library('api');
$this->load->library('synchronization');
$this->load->library('notifications');
$this->api->cors();
$this->api->auth();
$this->api->model('appointments_model');
}
/**
* Get an appointment collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$appointments = empty($keyword)
? $this->appointments_model->get(NULL, $limit, $offset, $order_by)
: $this->appointments_model->search($keyword, $limit, $offset, $order_by);
foreach ($appointments as &$appointment)
{
$this->appointments_model->api_encode($appointment);
$this->aggregates($appointment);
if ( ! empty($fields))
{
$this->appointments_model->only($appointment, $fields);
}
}
json_response($appointments);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single appointment.
*
* @param int|null $id Appointment ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$appointment = $this->appointments_model->find($id);
$this->appointments_model->api_encode($appointment);
if ( ! empty($fields))
{
$this->appointments_model->only($appointment, $fields);
}
if ( ! $appointment)
{
response('Not Found', 404);
return;
}
json_response($appointment);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create an appointment.
*/
public function store()
{
try
{
$appointment = request();
$this->appointments_model->api_decode($appointment);
if (array_key_exists('id', $appointment))
{
unset($appointment['id']);
}
if ( ! array_key_exists('end_datetime', $appointment))
{
$appointment['end_datetime'] = $this->calculate_end_datetime($appointment);
}
$appointment_id = $this->appointments_model->save($appointment);
$created_appointment = $this->appointments_model->find($appointment_id);
$this->notify_and_sync_appointment($created_appointment);
$this->appointments_model->api_encode($created_appointment);
json_response($created_appointment, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update an appointment.
*
* @param int $id Appointment ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->appointments_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_appointment = $occurrences[0];
$appointment = request();
$this->appointments_model->api_decode($appointment, $original_appointment);
$appointment_id = $this->appointments_model->save($appointment);
$updated_appointment = $this->appointments_model->find($appointment_id);
$this->notify_and_sync_appointment($updated_appointment, 'update');
$this->appointments_model->api_encode($updated_appointment);
json_response($updated_appointment);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete an appointment.
*
* @param int $id Appointment ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->appointments_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$deleted_appointment = $occurrences[0];
$service = $this->services_model->find($deleted_appointment['id_services']);
$provider = $this->providers_model->find($deleted_appointment['id_users_provider']);
$customer = $this->customers_model->find($deleted_appointment['id_users_customer']);
$settings = [
'company_name' => setting('company_name'),
'company_email' => setting('company_email'),
'company_link' => setting('company_link'),
'date_format' => setting('date_format'),
'time_format' => setting('time_format')
];
$this->appointments_model->delete($id);
$this->synchronization->sync_appointment_deleted($deleted_appointment, $provider);
$this->notifications->notify_appointment_deleted($deleted_appointment, $service, $provider, $customer, $settings);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Send the required notifications and trigger syncing after saving an appointment.
*
* @param array $appointment Appointment data.
* @param string $action Performed action ("store" or "update").
*/
private function notify_and_sync_appointment(array $appointment, string $action = 'store')
{
$manage_mode = $action === 'update';
$service = $this->services_model->find($appointment['id_services']);
$provider = $this->providers_model->find($appointment['id_users_provider']);
$customer = $this->customers_model->find($appointment['id_users_customer']);
$settings = [
'company_name' => setting('company_name'),
'company_email' => setting('company_email'),
'company_link' => setting('company_link'),
'date_format' => setting('date_format'),
'time_format' => setting('time_format')
];
$this->synchronization->sync_appointment_saved($appointment, $service, $provider, $customer, $settings, $manage_mode);
$this->notifications->notify_appointment_saved($appointment, $service, $provider, $customer, $settings, $manage_mode);
}
/**
* Calculate the end date time of an appointment based on the selected service.
*
* @param array $appointment Appointment data.
*
* @return string Returns the end date time value.
*
* @throws Exception
*/
private function calculate_end_datetime(array $appointment): string
{
$duration = $this->services_model->value($appointment['id_services'], 'duration');
$end = new DateTime($appointment['start_datetime']);
$end->add(new DateInterval('PT' . $duration . 'M'));
return $end->format('Y-m-d H:i:s');
}
/**
* Load the relations of the current appointment if the "aggregates" query parameter is present.
*
* This is a compatibility addition to the appointment resource which was the only one to support it.
*
* Use the "attach" query parameter instead as this one will be removed.
*
* @param array $appointment Appointment data.
*
* @deprecated 1.5.0
*/
private function aggregates(array &$appointment)
{
$aggregates = request('aggregates') !== NULL;
if ($aggregates)
{
$appointment['service'] = $this->services_model->find($appointment['serviceId'] ?? $appointment['id_services']);
$appointment['provider'] = $this->providers_model->find($appointment['providerId'] ?? $appointment['id_users_provider']);
$appointment['customer'] = $this->customers_model->find($appointment['customerId'] ?? $appointment['id_users_customer']);
$this->services_model->api_encode($appointment['service']);
$this->providers_model->api_encode($appointment['provider']);
$this->customers_model->api_encode($appointment['customer']);
}
}
}

View file

@ -8,36 +8,46 @@
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* @since v1.5.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
require_once __DIR__ . '/../../Appointments.php';
/**
* Availabilities Controller
* Availabilities API v1 controller.
*
* @package Controllers
*/
class Availabilities extends API_V1_Controller {
class Availabilities_api_v1 extends EA_Controller {
/**
* Class Constructor
* Availabilities_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('appointments_model');
$this->load->model('providers_model');
$this->load->model('services_model');
$this->load->model('settings_model');
$this->load->library('availability');
}
/**
* GET API Method
* Generate the available hours based on the selected date, service and provider.
*
* Provide the "providerId", "serviceId" and "date" GET parameters to get the availabilities for a specific date.
* If no "date" was provided then the current date will be used.
* This resource requires the following query parameters:
*
* - serviceId
* - providerI
* - date
*
* Based on those values it will generate the available hours, just like how the booking page works.
*
* You can then safely create a new appointment starting on one of the selected hours.
*
* Notice: The returned hours are in the provider's timezone.
*
* If no date parameter is provided then the current date will be used.
*/
public function get()
{
@ -60,13 +70,11 @@ class Availabilities extends API_V1_Controller {
$available_hours = $this->availability->get_available_hours($date, $service, $provider);
$this->output
->set_content_type('application/json')
->set_output(json_encode($available_hours));
json_response($available_hours);
}
catch (Throwable $e)
{
$this->handle_exception($e);
json_exception($e);
}
}
}

View file

@ -1,167 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Categories Controller
*
* @package Controllers
*/
class Categories extends API_V1_Controller {
/**
* Categories Resource Parser
*
* @var \EA\Engine\Api\V1\Parsers\Categories
*/
protected $parser;
/**
* Class Constructor
*/
public function __construct()
{
parent::__construct();
$this->load->model('services_model');
$this->parser = new \EA\Engine\Api\V1\Parsers\Categories;
}
/**
* GET API Method
*
* @param int $id Optional (null), the record ID to be returned.
*/
public function get($id = NULL)
{
try
{
$where = $id !== NULL ? ['id' => $id] : NULL;
$categories = $this->services_model->get_all_categories($where);
if ($id !== NULL && count($categories) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($categories);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the category to the database.
$request = new Request();
$category = $request->get_body();
$this->parser->decode($category);
if (isset($category['id']))
{
unset($category['id']);
}
$id = $this->services_model->add_category($category);
// Fetch the new object from the database and return it to the client.
$batch = $this->services_model->get_all_categories(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the category record.
$batch = $this->services_model->get_all_categories(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updated_category = $request->get_body();
$base_category = $batch[0];
$this->parser->decode($updated_category, $base_category);
$updated_category['id'] = $id;
$id = $this->services_model->add_category($updated_category);
// Fetch the updated object from the database and return it to the client.
$batch = $this->services_model->get_all_categories(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$this->services_model->delete_category($id);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,207 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Categories API v1 controller.
*
* @package Controllers
*/
class Categories_api_v1 extends EA_Controller {
/**
* Categories_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('service_categories_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('service_categories_model');
}
/**
* Get a service category collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$service_categories = empty($keyword)
? $this->service_categories_model->get(NULL, $limit, $offset, $order_by)
: $this->service_categories_model->search($keyword, $limit, $offset, $order_by);
foreach ($service_categories as &$service_category)
{
$this->service_categories_model->api_encode($service_category);
if ( ! empty($fields))
{
$this->service_categories_model->only($service_category, $fields);
}
}
json_response($service_categories);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single service category.
*
* @param int|null $id Service category ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$service_category = $this->service_categories_model->find($id);
$this->service_categories_model->api_encode($service_category);
if ( ! empty($fields))
{
$this->service_categories_model->only($service_category, $fields);
}
if ( ! $service_category)
{
response('', 404);
return;
}
json_response($service_category);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create a service category.
*/
public function store()
{
try
{
$service_category = request();
$this->service_categories_model->api_decode($service_category);
if (array_key_exists('id', $service_category))
{
unset($service_category['id']);
}
$service_category_id = $this->service_categories_model->save($service_category);
$created_service_category = $this->service_categories_model->find($service_category_id);
$this->service_categories_model->api_encode($created_service_category);
json_response($created_service_category, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update a service category.
*
* @param int $id Service category ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->service_categories_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_service_category = $occurrences[0];
$service_category = request();
$this->service_categories_model->api_decode($service_category, $original_service_category);
$service_category_id = $this->service_categories_model->save($service_category);
$updated_service_category = $this->service_categories_model->find($service_category_id);
$this->service_categories_model->api_encode($updated_service_category);
json_response($updated_service_category);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete a service category.
*
* @param int $id Service category ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->service_categories_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->service_categories_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -1,167 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Customers Controller
*
* @package Controllers
*/
class Customers extends API_V1_Controller {
/**
* Customers Resource Parser
*
* @var \EA\Engine\Api\V1\Parsers\Customers
*/
protected $parser;
/**
* Class Constructor
*/
public function __construct()
{
parent::__construct();
$this->load->model('customers_model');
$this->parser = new \EA\Engine\Api\V1\Parsers\Customers;
}
/**
* GET API Method
*
* @param int $id Optional (null), the record ID to be returned.
*/
public function get($id = NULL)
{
try
{
$conditions = $id !== NULL ? ['id' => $id] : NULL;
$customers = $this->customers_model->get($conditions);
if ($id !== NULL && count($customers) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($customers);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the customer to the database.
$request = new Request();
$customer = $request->get_body();
$this->parser->decode($customer);
if (isset($customer['id']))
{
unset($customer['id']);
}
$id = $this->customers_model->save($customer);
// Fetch the new object from the database and return it to the client.
$batch = $this->customers_model->get(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the customer record.
$batch = $this->customers_model->get(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updated_customer = $request->get_body();
$base_customer = $batch[0];
$this->parser->decode($updated_customer, $base_customer);
$updated_customer['id'] = $id;
$id = $this->customers_model->save($updated_customer);
// Fetch the updated object from the database and return it to the client.
$batch = $this->customers_model->get(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$this->customers_model->delete($id);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,207 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Customers API v1 controller.
*
* @package Controllers
*/
class Customers_api_v1 extends EA_Controller {
/**
* Customers_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('customers_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('customers_model');
}
/**
* Get a customer collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$customers = empty($keyword)
? $this->customers_model->get(NULL, $limit, $offset, $order_by)
: $this->customers_model->search($keyword, $limit, $offset, $order_by);
foreach ($customers as &$customer)
{
$this->customers_model->api_encode($customer);
if ( ! empty($fields))
{
$this->customers_model->only($customer, $fields);
}
}
json_response($customers);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single customer.
*
* @param int|null $id Customer ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$customer = $this->customers_model->find($id);
$this->customers_model->api_encode($customer);
if ( ! empty($fields))
{
$this->customers_model->only($customer, $fields);
}
if ( ! $customer)
{
response('', 404);
return;
}
json_response($customer);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create a customer.
*/
public function store()
{
try
{
$customer = request();
$this->customers_model->api_decode($customer);
if (array_key_exists('id', $customer))
{
unset($customer['id']);
}
$customer_id = $this->customers_model->save($customer);
$created_customer = $this->customers_model->find($customer_id);
$this->customers_model->api_encode($created_customer);
json_response($created_customer, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update a customer.
*
* @param int $id Customer ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->customers_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_customer = $occurrences[0];
$customer = request();
$this->customers_model->api_decode($customer, $original_customer);
$customer_id = $this->customers_model->save($customer);
$updated_customer = $this->customers_model->find($customer_id);
$this->customers_model->api_encode($updated_customer);
json_response($updated_customer);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete a customer.
*
* @param int $id Customer ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->customers_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->customers_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -1,182 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Providers Controller
*
* @package Controllers
*/
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.
*/
public function get($id = NULL)
{
try
{
$conditions = $id !== NULL ? ['id' => $id] : NULL;
$providers = $this->providers_model->get($conditions);
if ($id !== NULL && count($providers) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($providers);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the provider to the database.
$request = new Request();
$provider = $request->get_body();
$this->parser->decode($provider);
if (array_key_exists('id', $provider))
{
unset($provider['id']);
}
if ( ! array_key_exists('services', $provider))
{
throw new Exception('No services property provided.');
}
if ( ! array_key_exists('settings', $provider))
{
throw new Exception('No settings property provided.');
}
if ( ! array_key_exists('working_plan', $provider['settings']))
{
$provider['settings']['working_plan'] = setting('company_working_plan');
}
$id = $this->providers_model->save($provider);
// Fetch the new object from the database and return it to the client.
$batch = $this->providers_model->get(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the provider record.
$batch = $this->providers_model->get(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updated_provider = $request->get_body();
$base_provider = $batch[0];
$this->parser->decode($updated_provider, $base_provider);
$updated_provider['id'] = $id;
$id = $this->providers_model->save($updated_provider);
// Fetch the updated object from the database and return it to the client.
$batch = $this->providers_model->get(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$this->providers_model->delete($id);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,222 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Providers API v1 controller.
*
* @package Controllers
*/
class Providers_api_v1 extends EA_Controller {
/**
* Providers_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('providers_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('providers_model');
}
/**
* Get a provider collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$providers = empty($keyword)
? $this->providers_model->get(NULL, $limit, $offset, $order_by)
: $this->providers_model->search($keyword, $limit, $offset, $order_by);
foreach ($providers as &$provider)
{
$this->providers_model->api_encode($provider);
if ( ! empty($fields))
{
$this->providers_model->only($provider, $fields);
}
}
json_response($providers);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single provider.
*
* @param int|null $id Provider ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$provider = $this->providers_model->find($id);
$this->providers_model->api_encode($provider);
if ( ! empty($fields))
{
$this->providers_model->only($provider, $fields);
}
if ( ! $provider)
{
response('', 404);
return;
}
json_response($provider);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create a provider.
*/
public function store()
{
try
{
$provider = request();
$this->providers_model->api_decode($provider);
if (array_key_exists('id', $provider))
{
unset($provider['id']);
}
if ( ! array_key_exists('services', $provider))
{
throw new Exception('No services property provided.');
}
if ( ! array_key_exists('settings', $provider))
{
throw new Exception('No settings property provided.');
}
if ( ! array_key_exists('working_plan', $provider['settings']))
{
$provider['settings']['working_plan'] = setting('company_working_plan');
}
$provider_id = $this->providers_model->save($provider);
$created_provider = $this->providers_model->find($provider_id);
$this->providers_model->api_encode($created_provider);
json_response($created_provider, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update a provider.
*
* @param int $id Provider ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->providers_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_provider = $occurrences[0];
$provider = request();
$this->providers_model->api_decode($provider, $original_provider);
$provider_id = $this->providers_model->save($provider);
$updated_provider = $this->providers_model->find($provider_id);
$this->providers_model->api_encode($updated_provider);
json_response($updated_provider);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete a provider.
*
* @param int $id Provider ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->providers_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->providers_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -1,177 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Secretaries Controller
*
* @package Controllers
*/
class Secretaries extends API_V1_Controller {
/**
* Secretaries Resource Parser
*
* @var \EA\Engine\Api\V1\Parsers\Secretaries
*/
protected $parser;
/**
* Class Constructor
*/
public function __construct()
{
parent::__construct();
$this->load->model('secretaries_model');
$this->parser = new \EA\Engine\Api\V1\Parsers\Secretaries;
}
/**
* GET API Method
*
* @param int $id Optional (null), the record ID to be returned.
*/
public function get($id = NULL)
{
try
{
$conditions = $id !== NULL ? ['id' => $id] : NULL;
$secretaries = $this->secretaries_model->get($conditions);
if ($id !== NULL && count($secretaries) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($secretaries);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the secretary to the database.
$request = new Request();
$secretary = $request->get_body();
$this->parser->decode($secretary);
if (array_key_exists('id', $secretary))
{
unset($secretary['id']);
}
if ( ! array_key_exists('providers', $secretary))
{
throw new Exception('No providers property provided.');
}
if ( ! array_key_exists('settings', $secretary))
{
throw new Exception('No settings property provided.');
}
$id = $this->secretaries_model->save($secretary);
// Fetch the new object from the database and return it to the client.
$batch = $this->secretaries_model->get(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the secretary record.
$batch = $this->secretaries_model->get(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updated_secretary = $request->get_body();
$base_secretary = $batch[0];
$this->parser->decode($updated_secretary, $base_secretary);
$updated_secretary['id'] = $id;
$id = $this->secretaries_model->save($updated_secretary);
// Fetch the updated object from the database and return it to the client.
$batch = $this->secretaries_model->get(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$this->secretaries_model->delete($id);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,217 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Secretaries API v1 controller.
*
* @package Controllers
*/
class Secretaries_api_v1 extends EA_Controller {
/**
* Secretaries_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('secretaries_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('secretaries_model');
}
/**
* Get a secretary collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$secretaries = empty($keyword)
? $this->secretaries_model->get(NULL, $limit, $offset, $order_by)
: $this->secretaries_model->search($keyword, $limit, $offset, $order_by);
foreach ($secretaries as &$secretary)
{
$this->secretaries_model->api_encode($secretary);
if ( ! empty($fields))
{
$this->secretaries_model->only($secretary, $fields);
}
}
json_response($secretaries);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single secretary.
*
* @param int|null $id Secretary ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$secretary = $this->secretaries_model->find($id);
$this->secretaries_model->api_encode($secretary);
if ( ! empty($fields))
{
$this->secretaries_model->only($secretary, $fields);
}
if ( ! $secretary)
{
response('', 404);
return;
}
json_response($secretary);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create a secretary.
*/
public function store()
{
try
{
$secretary = request();
$this->secretaries_model->api_decode($secretary);
if (array_key_exists('id', $secretary))
{
unset($secretary['id']);
}
if ( ! array_key_exists('providers', $secretary))
{
throw new Exception('No providers property provided.');
}
if ( ! array_key_exists('settings', $secretary))
{
throw new Exception('No settings property provided.');
}
$secretary_id = $this->secretaries_model->save($secretary);
$created_secretary = $this->secretaries_model->find($secretary_id);
$this->secretaries_model->api_encode($created_secretary);
json_response($created_secretary, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update a secretary.
*
* @param int $id Secretary ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->secretaries_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_secretary = $occurrences[0];
$secretary = request();
$this->secretaries_model->api_decode($secretary, $original_secretary);
$secretary_id = $this->secretaries_model->save($secretary);
$updated_secretary = $this->secretaries_model->find($secretary_id);
$this->secretaries_model->api_encode($updated_secretary);
json_response($updated_secretary);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete a secretary.
*
* @param int $id Secretary ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->secretaries_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->secretaries_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -1,167 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Services Controller
*
* @package Controllers
*/
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.
*/
public function get($id = NULL)
{
try
{
$conditions = $id !== NULL ? ['id' => $id] : NULL;
$services = $this->services_model->get($conditions);
if ($id !== NULL && count($services) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($services);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the service to the database.
$request = new Request();
$service = $request->get_body();
$this->parser->decode($service);
if (isset($service['id']))
{
unset($service['id']);
}
$id = $this->services_model->save($service);
// Fetch the new object from the database and return it to the client.
$batch = $this->services_model->get(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the service record.
$batch = $this->services_model->get(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updated_service = $request->get_body();
$base_service = $batch[0];
$this->parser->decode($updated_service, $base_service);
$updated_service['id'] = $id;
$id = $this->services_model->save($updated_service);
// Fetch the updated object from the database and return it to the client.
$batch = $this->services_model->get(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$this->services_model->delete($id);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,207 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Services API v1 controller.
*
* @package Controllers
*/
class Services_api_v1 extends EA_Controller {
/**
* Services_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('services_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('services_model');
}
/**
* Get an service collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$services = empty($keyword)
? $this->services_model->get(NULL, $limit, $offset, $order_by)
: $this->services_model->search($keyword, $limit, $offset, $order_by);
foreach ($services as &$service)
{
$this->services_model->api_encode($service);
if ( ! empty($fields))
{
$this->services_model->only($service, $fields);
}
}
json_response($services);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single service.
*
* @param int|null $id Service ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$service = $this->services_model->find($id);
$this->services_model->api_encode($service);
if ( ! empty($fields))
{
$this->services_model->only($service, $fields);
}
if ( ! $service)
{
response('', 404);
return;
}
json_response($service);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create an service.
*/
public function store()
{
try
{
$service = request();
$this->services_model->api_decode($service);
if (array_key_exists('id', $service))
{
unset($service['id']);
}
$service_id = $this->services_model->save($service);
$created_service = $this->services_model->find($service_id);
$this->services_model->api_encode($created_service);
json_response($created_service, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update an service.
*
* @param int $id Service ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->services_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_service = $occurrences[0];
$service = request();
$this->services_model->api_decode($service, $original_service);
$service_id = $this->services_model->save($service);
$updated_service = $this->services_model->find($service_id);
$this->services_model->api_encode($updated_service);
json_response($updated_service);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete an service.
*
* @param int $id Service ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->services_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->services_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -1,162 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
/**
* Settings Controller
*
* @package Controllers
*/
class Settings extends API_V1_Controller {
/**
* Settings Resource Parser
*
* @var \EA\Engine\Api\V1\Parsers\Settings
*/
protected $parser;
/**
* Class Constructor
*/
public function __construct()
{
parent::__construct();
$this->load->model('settings_model');
$this->parser = new \EA\Engine\Api\V1\Parsers\Settings;
}
/**
* GET API Method
*
* @param string $name Optional (null), the setting name to be returned.
*/
public function get($name = NULL)
{
try
{
$settings = $this->settings_model->get();
if ($name !== NULL)
{
$setting = NULL;
foreach ($settings as $entry)
{
if ($entry['name'] === $name)
{
$setting = $entry;
break;
}
}
if (empty($setting))
{
$this->throw_record_not_found();
}
unset($setting['id']);
$settings = [
$setting
];
}
$response = new Response($settings);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($name)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param string $name The setting name to be inserted/updated.
*/
public function put($name)
{
try
{
$request = new Request();
$value = $request->get_body()['value'];
$setting = $this->settings_model->query()->get_where('name', $name)->row_array();
if ( ! empty($setting))
{
$setting['value'] = $value;
$this->settings_model->save($setting);
}
// Fetch the updated object from the database and return it to the client.
$response = new Response([
[
'name' => $name,
'value' => $value
]
]);
$response->encode($this->parser)->singleEntry($name)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param string $name The setting name to be deleted.
*/
public function delete($name)
{
try
{
$setting = $this->settings_model->query()->get_where('name', $name)->row_array();
if ( ! empty($setting))
{
$this->settings_model->delete($setting['id']);
}
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,122 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Settings API v1 controller.
*
* @package Controllers
*/
class Settings_api_v1 extends EA_Controller {
/**
* Settings_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('settings_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('settings_model');
}
/**
* Get a setting collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$settings = empty($keyword)
? $this->settings_model->get(NULL, $limit, $offset, $order_by)
: $this->settings_model->search($keyword, $limit, $offset, $order_by);
foreach ($settings as &$setting)
{
$this->settings_model->api_encode($setting);
if ( ! empty($fields))
{
$this->settings_model->only($setting, $fields);
}
}
json_response($settings);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a setting value by name.
*
* @param string $name Setting name.
*/
public function show(string $name)
{
try
{
$value = setting($name);
json_response([
'name' => $name,
'value' => $value,
]);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Set a setting value by name.
*
* @param string $name Setting name.
*/
public function update(string $name)
{
try
{
$value = request('value');
setting([$name => $value]);
json_response([
'name' => $name,
'value' => $value,
]);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -1,167 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
require_once __DIR__ . '/API_V1_Controller.php';
use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;
/**
* Unavailabilities Controller
*
* @package Controllers
*/
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.
*/
public function get($id = NULL)
{
try
{
$where = $id !== NULL ? ['id' => $id] : ['is_unavailable' => TRUE];
$unavailabilities = $this->appointments_model->get($where);
if ($id !== NULL && count($unavailabilities) === 0)
{
$this->throw_record_not_found();
}
$response = new Response($unavailabilities);
$response->encode($this->parser)
->search()
->sort()
->paginate()
->minimize()
->singleEntry($id)
->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* POST API Method
*/
public function post()
{
try
{
// Insert the appointment to the database.
$request = new Request();
$unavailability = $request->get_body();
$this->parser->decode($unavailability);
if (isset($unavailability['id']))
{
unset($unavailability['id']);
}
$id = $this->appointments_model->save_unavailable($unavailability);
// Fetch the new object from the database and return it to the client.
$batch = $this->appointments_model->get(['id' => $id]);
$response = new Response($batch);
$status = new NonEmptyText('201 Created');
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* PUT API Method
*
* @param int $id The record ID to be updated.
*/
public function put($id)
{
try
{
// Update the appointment record.
$batch = $this->appointments_model->get(['id' => $id]);
if ($id !== NULL && count($batch) === 0)
{
$this->throw_record_not_found();
}
$request = new Request();
$updatedUnavailability = $request->get_body();
$baseUnavailability = $batch[0];
$this->parser->decode($updatedUnavailability, $baseUnavailability);
$updatedUnavailability['id'] = $id;
$id = $this->appointments_model->save_unavailable($updatedUnavailability);
// Fetch the updated object from the database and return it to the client.
$batch = $this->appointments_model->get(['id' => $id]);
$response = new Response($batch);
$response->encode($this->parser)->singleEntry($id)->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
/**
* DELETE API Method
*
* @param int $id The record ID to be deleted.
*/
public function delete($id)
{
try
{
$this->appointments_model->delete($id);
$response = new Response([
'code' => 200,
'message' => 'Record was deleted successfully!'
]);
$response->output();
}
catch (Throwable $e)
{
$this->handle_exception($e);
}
}
}

View file

@ -0,0 +1,207 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Unavailabilities API v1 controller.
*
* @package Controllers
*/
class Unavailabilities_api_v1 extends EA_Controller {
/**
* Unavailabilities_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('unavailabilities_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('unavailabilities_model');
}
/**
* Get an unavailability collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$unavailabilities = empty($keyword)
? $this->unavailabilities_model->get(NULL, $limit, $offset, $order_by)
: $this->unavailabilities_model->search($keyword, $limit, $offset, $order_by);
foreach ($unavailabilities as &$unavailability)
{
$this->unavailabilities_model->api_encode($unavailability);
if ( ! empty($fields))
{
$this->unavailabilities_model->only($unavailability, $fields);
}
}
json_response($unavailabilities);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single unavailability.
*
* @param int|null $id Unavailability ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$unavailability = $this->unavailabilities_model->find($id);
$this->unavailabilities_model->api_encode($unavailability);
if ( ! empty($fields))
{
$this->unavailabilities_model->only($unavailability, $fields);
}
if ( ! $unavailability)
{
response('', 404);
return;
}
json_response($unavailability);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create an unavailability.
*/
public function store()
{
try
{
$unavailability = request();
$this->unavailabilities_model->api_decode($unavailability);
if (array_key_exists('id', $unavailability))
{
unset($unavailability['id']);
}
$unavailability_id = $this->unavailabilities_model->save($unavailability);
$created_unavailability = $this->unavailabilities_model->find($unavailability_id);
$this->unavailabilities_model->api_encode($created_unavailability);
json_response($created_unavailability, 201);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update an unavailability.
*
* @param int $id Unavailability ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->unavailabilities_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_unavailability = $occurrences[0];
$unavailability = request();
$this->unavailabilities_model->api_decode($unavailability, $original_unavailability);
$unavailability_id = $this->unavailabilities_model->save($unavailability);
$updated_unavailability = $this->unavailabilities_model->find($unavailability_id);
$this->unavailabilities_model->api_encode($updated_unavailability);
json_response($updated_unavailability);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete an unavailability.
*
* @param int $id Unavailability ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->unavailabilities_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->unavailabilities_model->delete($id);
response('', 204);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}