Added support for the "attach" query parameter to all API controllers

This commit is contained in:
Alex Tselegidis 2021-11-06 15:34:54 +01:00
parent bd8144f7cb
commit f36c49819c
9 changed files with 120 additions and 5 deletions

View file

@ -52,6 +52,8 @@ class Admins_api_v1 extends EA_Controller {
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$admins = empty($keyword)
? $this->admins_model->get(NULL, $limit, $offset, $order_by)
: $this->admins_model->search($keyword, $limit, $offset, $order_by);
@ -64,6 +66,11 @@ class Admins_api_v1 extends EA_Controller {
{
$this->admins_model->only($admin, $fields);
}
if ( ! empty($attach))
{
$this->admins_model->attach($admin, $attach);
}
}
json_response($admins);
@ -85,6 +92,8 @@ class Admins_api_v1 extends EA_Controller {
{
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$admin = $this->admins_model->find($id);
$this->admins_model->api_encode($admin);
@ -94,6 +103,11 @@ class Admins_api_v1 extends EA_Controller {
$this->admins_model->only($admin, $fields);
}
if ( ! empty($attach))
{
$this->admins_model->attach($admin, $attach);
}
if ( ! $admin)
{
response('', 404);
@ -201,7 +215,7 @@ class Admins_api_v1 extends EA_Controller {
}
$this->admins_model->delete($id);
response('', 204);
}
catch (Throwable $e)

View file

@ -58,6 +58,8 @@ class Appointments_api_v1 extends EA_Controller {
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$appointments = empty($keyword)
? $this->appointments_model->get(NULL, $limit, $offset, $order_by)
: $this->appointments_model->search($keyword, $limit, $offset, $order_by);
@ -72,6 +74,11 @@ class Appointments_api_v1 extends EA_Controller {
{
$this->appointments_model->only($appointment, $fields);
}
if ( ! empty($attach))
{
$this->appointments_model->attach($appointment, $attach);
}
}
json_response($appointments);
@ -92,6 +99,8 @@ class Appointments_api_v1 extends EA_Controller {
try
{
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$appointment = $this->appointments_model->find($id);
@ -102,6 +111,11 @@ class Appointments_api_v1 extends EA_Controller {
$this->appointments_model->only($appointment, $fields);
}
if ( ! empty($attach))
{
$this->appointments_model->attach($appointment, $attach);
}
if ( ! $appointment)
{
response('Not Found', 404);
@ -308,9 +322,9 @@ class Appointments_api_v1 extends EA_Controller {
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']);
$appointment['service'] = $this->services_model->find($appointment['id_services'] ?? $appointment['serviceId'] ?? NULL);
$appointment['provider'] = $this->providers_model->find($appointment['id_users_provider'] ?? $appointment['providerId'] ?? NULL);
$appointment['customer'] = $this->customers_model->find($appointment['id_users_customer'] ?? $appointment['customerId']) ?? NULL;
$this->services_model->api_encode($appointment['service']);
$this->providers_model->api_encode($appointment['provider']);
$this->customers_model->api_encode($appointment['customer']);

View file

@ -51,6 +51,8 @@ class Categories_api_v1 extends EA_Controller {
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$service_categories = empty($keyword)
? $this->service_categories_model->get(NULL, $limit, $offset, $order_by)
@ -64,6 +66,11 @@ class Categories_api_v1 extends EA_Controller {
{
$this->service_categories_model->only($service_category, $fields);
}
if ( ! empty($attach))
{
$this->service_categories_model->attach($service_category, $attach);
}
}
json_response($service_categories);
@ -84,6 +91,8 @@ class Categories_api_v1 extends EA_Controller {
try
{
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$service_category = $this->service_categories_model->find($id);
@ -94,6 +103,11 @@ class Categories_api_v1 extends EA_Controller {
$this->service_categories_model->only($service_category, $fields);
}
if ( ! empty($attach))
{
$this->service_categories_model->attach($service_category, $attach);
}
if ( ! $service_category)
{
response('', 404);

View file

@ -52,6 +52,8 @@ class Customers_api_v1 extends EA_Controller {
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$customers = empty($keyword)
? $this->customers_model->get(NULL, $limit, $offset, $order_by)
: $this->customers_model->search($keyword, $limit, $offset, $order_by);
@ -64,6 +66,11 @@ class Customers_api_v1 extends EA_Controller {
{
$this->customers_model->only($customer, $fields);
}
if ( ! empty($attach))
{
$this->customers_model->attach($customer, $attach);
}
}
json_response($customers);

View file

@ -51,6 +51,8 @@ class Providers_api_v1 extends EA_Controller {
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$providers = empty($keyword)
? $this->providers_model->get(NULL, $limit, $offset, $order_by)
@ -64,6 +66,11 @@ class Providers_api_v1 extends EA_Controller {
{
$this->providers_model->only($provider, $fields);
}
if ( ! empty($attach))
{
$this->providers_model->attach($provider, $attach);
}
}
json_response($providers);
@ -84,6 +91,8 @@ class Providers_api_v1 extends EA_Controller {
try
{
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$provider = $this->providers_model->find($id);
@ -94,6 +103,11 @@ class Providers_api_v1 extends EA_Controller {
$this->providers_model->only($provider, $fields);
}
if ( ! empty($attach))
{
$this->providers_model->attach($provider, $attach);
}
if ( ! $provider)
{
response('', 404);

View file

@ -51,6 +51,8 @@ class Secretaries_api_v1 extends EA_Controller {
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$secretaries = empty($keyword)
? $this->secretaries_model->get(NULL, $limit, $offset, $order_by)
@ -64,6 +66,11 @@ class Secretaries_api_v1 extends EA_Controller {
{
$this->secretaries_model->only($secretary, $fields);
}
if ( ! empty($attach))
{
$this->secretaries_model->attach($secretary, $attach);
}
}
json_response($secretaries);

View file

@ -51,6 +51,8 @@ class Services_api_v1 extends EA_Controller {
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$services = empty($keyword)
? $this->services_model->get(NULL, $limit, $offset, $order_by)
@ -64,6 +66,11 @@ class Services_api_v1 extends EA_Controller {
{
$this->services_model->only($service, $fields);
}
if ( ! empty($attach))
{
$this->services_model->attach($service, $attach);
}
}
json_response($services);
@ -84,6 +91,8 @@ class Services_api_v1 extends EA_Controller {
try
{
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$service = $this->services_model->find($id);
@ -93,6 +102,11 @@ class Services_api_v1 extends EA_Controller {
{
$this->services_model->only($service, $fields);
}
if ( ! empty($attach))
{
$this->services_model->attach($service, $attach);
}
if ( ! $service)
{

View file

@ -52,6 +52,8 @@ class Unavailabilities_api_v1 extends EA_Controller {
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$unavailabilities = empty($keyword)
? $this->unavailabilities_model->get(NULL, $limit, $offset, $order_by)
: $this->unavailabilities_model->search($keyword, $limit, $offset, $order_by);
@ -64,6 +66,11 @@ class Unavailabilities_api_v1 extends EA_Controller {
{
$this->unavailabilities_model->only($unavailability, $fields);
}
if ( ! empty($attach))
{
$this->unavailabilities_model->attach($unavailability, $attach);
}
}
json_response($unavailabilities);
@ -85,6 +92,8 @@ class Unavailabilities_api_v1 extends EA_Controller {
{
$fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$unavailability = $this->unavailabilities_model->find($id);
$this->unavailabilities_model->api_encode($unavailability);
@ -94,6 +103,11 @@ class Unavailabilities_api_v1 extends EA_Controller {
$this->unavailabilities_model->only($unavailability, $fields);
}
if ( ! empty($attach))
{
$this->unavailabilities_model->attach($unavailability, $attach);
}
if ( ! $unavailability)
{
response('', 404);

View file

@ -264,7 +264,7 @@ class Api {
}
/**
* Get the chosen fields array of the current request.
* Get the chosen "fields" array of the current request.
*
* @return array|null
*/
@ -279,4 +279,21 @@ class Api {
return array_map('trim', explode(',', $fields));
}
/**
* Get the provided "attach" array of the current request.
*
* @return array|null
*/
public function request_attach(): ?array
{
$attach = request('attach');
if ( ! $attach)
{
return NULL;
}
return array_map('trim', explode(',', $attach));
}
}