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(); $fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$admins = empty($keyword) $admins = empty($keyword)
? $this->admins_model->get(NULL, $limit, $offset, $order_by) ? $this->admins_model->get(NULL, $limit, $offset, $order_by)
: $this->admins_model->search($keyword, $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); $this->admins_model->only($admin, $fields);
} }
if ( ! empty($attach))
{
$this->admins_model->attach($admin, $attach);
}
} }
json_response($admins); json_response($admins);
@ -85,6 +92,8 @@ class Admins_api_v1 extends EA_Controller {
{ {
$fields = $this->api->request_fields(); $fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$admin = $this->admins_model->find($id); $admin = $this->admins_model->find($id);
$this->admins_model->api_encode($admin); $this->admins_model->api_encode($admin);
@ -94,6 +103,11 @@ class Admins_api_v1 extends EA_Controller {
$this->admins_model->only($admin, $fields); $this->admins_model->only($admin, $fields);
} }
if ( ! empty($attach))
{
$this->admins_model->attach($admin, $attach);
}
if ( ! $admin) if ( ! $admin)
{ {
response('', 404); response('', 404);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -52,6 +52,8 @@ class Unavailabilities_api_v1 extends EA_Controller {
$fields = $this->api->request_fields(); $fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$unavailabilities = empty($keyword) $unavailabilities = empty($keyword)
? $this->unavailabilities_model->get(NULL, $limit, $offset, $order_by) ? $this->unavailabilities_model->get(NULL, $limit, $offset, $order_by)
: $this->unavailabilities_model->search($keyword, $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); $this->unavailabilities_model->only($unavailability, $fields);
} }
if ( ! empty($attach))
{
$this->unavailabilities_model->attach($unavailability, $attach);
}
} }
json_response($unavailabilities); json_response($unavailabilities);
@ -85,6 +92,8 @@ class Unavailabilities_api_v1 extends EA_Controller {
{ {
$fields = $this->api->request_fields(); $fields = $this->api->request_fields();
$attach = $this->api->request_attach();
$unavailability = $this->unavailabilities_model->find($id); $unavailability = $this->unavailabilities_model->find($id);
$this->unavailabilities_model->api_encode($unavailability); $this->unavailabilities_model->api_encode($unavailability);
@ -94,6 +103,11 @@ class Unavailabilities_api_v1 extends EA_Controller {
$this->unavailabilities_model->only($unavailability, $fields); $this->unavailabilities_model->only($unavailability, $fields);
} }
if ( ! empty($attach))
{
$this->unavailabilities_model->attach($unavailability, $attach);
}
if ( ! $unavailability) if ( ! $unavailability)
{ {
response('', 404); 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 * @return array|null
*/ */
@ -279,4 +279,21 @@ class Api {
return array_map('trim', explode(',', $fields)); 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));
}
} }