From cd6280b97d13c35802fa760e45eada5ff0eeb818 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 5 Jan 2024 19:20:00 +0100 Subject: [PATCH] Make sure the API returns 404 on show if the ID was not found --- application/controllers/api/v1/Admins_api_v1.php | 14 ++++++++------ .../controllers/api/v1/Appointments_api_v1.php | 14 ++++++++------ .../controllers/api/v1/Customers_api_v1.php | 14 ++++++++------ .../controllers/api/v1/Providers_api_v1.php | 14 ++++++++------ .../controllers/api/v1/Secretaries_api_v1.php | 14 ++++++++------ .../api/v1/Service_categories_api_v1.php | 14 ++++++++------ application/controllers/api/v1/Services_api_v1.php | 14 ++++++++------ .../controllers/api/v1/Unavailabilities_api_v1.php | 14 ++++++++------ application/controllers/api/v1/Webhooks_api_v1.php | 14 ++++++++------ 9 files changed, 72 insertions(+), 54 deletions(-) diff --git a/application/controllers/api/v1/Admins_api_v1.php b/application/controllers/api/v1/Admins_api_v1.php index 5f15216b..6faa6584 100644 --- a/application/controllers/api/v1/Admins_api_v1.php +++ b/application/controllers/api/v1/Admins_api_v1.php @@ -82,6 +82,14 @@ class Admins_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->admins_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -98,12 +106,6 @@ class Admins_api_v1 extends EA_Controller $this->admins_model->load($admin, $with); } - if (!$admin) { - response('', 404); - - return; - } - json_response($admin); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Appointments_api_v1.php b/application/controllers/api/v1/Appointments_api_v1.php index 54e3c2d2..d3914e8d 100644 --- a/application/controllers/api/v1/Appointments_api_v1.php +++ b/application/controllers/api/v1/Appointments_api_v1.php @@ -174,6 +174,14 @@ class Appointments_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->appointments_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -190,12 +198,6 @@ class Appointments_api_v1 extends EA_Controller $this->appointments_model->load($appointment, $with); } - if (!$appointment) { - response('Not Found', 404); - - return; - } - json_response($appointment); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Customers_api_v1.php b/application/controllers/api/v1/Customers_api_v1.php index 8f0b5fdd..9141776d 100644 --- a/application/controllers/api/v1/Customers_api_v1.php +++ b/application/controllers/api/v1/Customers_api_v1.php @@ -80,6 +80,14 @@ class Customers_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->customers_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $customer = $this->customers_model->find($id); @@ -90,12 +98,6 @@ class Customers_api_v1 extends EA_Controller $this->customers_model->only($customer, $fields); } - if (!$customer) { - response('', 404); - - return; - } - json_response($customer); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Providers_api_v1.php b/application/controllers/api/v1/Providers_api_v1.php index 5613fa40..f9ba2062 100644 --- a/application/controllers/api/v1/Providers_api_v1.php +++ b/application/controllers/api/v1/Providers_api_v1.php @@ -80,6 +80,14 @@ class Providers_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->providers_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -96,12 +104,6 @@ class Providers_api_v1 extends EA_Controller $this->providers_model->load($provider, $with); } - if (!$provider) { - response('', 404); - - return; - } - json_response($provider); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Secretaries_api_v1.php b/application/controllers/api/v1/Secretaries_api_v1.php index 7ff4c8c4..fec32f32 100644 --- a/application/controllers/api/v1/Secretaries_api_v1.php +++ b/application/controllers/api/v1/Secretaries_api_v1.php @@ -80,6 +80,14 @@ class Secretaries_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->secretaries_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $secretary = $this->secretaries_model->find($id); @@ -90,12 +98,6 @@ class Secretaries_api_v1 extends EA_Controller $this->secretaries_model->only($secretary, $fields); } - if (!$secretary) { - response('', 404); - - return; - } - json_response($secretary); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Service_categories_api_v1.php b/application/controllers/api/v1/Service_categories_api_v1.php index c0b288f9..7a1d2dec 100644 --- a/application/controllers/api/v1/Service_categories_api_v1.php +++ b/application/controllers/api/v1/Service_categories_api_v1.php @@ -80,6 +80,14 @@ class Service_categories_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->service_categories_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -96,12 +104,6 @@ class Service_categories_api_v1 extends EA_Controller $this->service_categories_model->load($service_category, $with); } - if (!$service_category) { - response('', 404); - - return; - } - json_response($service_category); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Services_api_v1.php b/application/controllers/api/v1/Services_api_v1.php index 47eaa7cd..397550da 100644 --- a/application/controllers/api/v1/Services_api_v1.php +++ b/application/controllers/api/v1/Services_api_v1.php @@ -80,6 +80,14 @@ class Services_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->services_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -96,12 +104,6 @@ class Services_api_v1 extends EA_Controller $this->services_model->load($service, $with); } - if (!$service) { - response('', 404); - - return; - } - json_response($service); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Unavailabilities_api_v1.php b/application/controllers/api/v1/Unavailabilities_api_v1.php index 0c6d7bc8..016c1964 100644 --- a/application/controllers/api/v1/Unavailabilities_api_v1.php +++ b/application/controllers/api/v1/Unavailabilities_api_v1.php @@ -80,6 +80,14 @@ class Unavailabilities_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->unavailabilities_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -96,12 +104,6 @@ class Unavailabilities_api_v1 extends EA_Controller $this->unavailabilities_model->load($unavailability, $with); } - if (!$unavailability) { - response('', 404); - - return; - } - json_response($unavailability); } catch (Throwable $e) { json_exception($e); diff --git a/application/controllers/api/v1/Webhooks_api_v1.php b/application/controllers/api/v1/Webhooks_api_v1.php index e1e019f6..75e71032 100644 --- a/application/controllers/api/v1/Webhooks_api_v1.php +++ b/application/controllers/api/v1/Webhooks_api_v1.php @@ -80,6 +80,14 @@ class Webhooks_api_v1 extends EA_Controller public function show(int $id = null): void { try { + $occurrences = $this->webhooks_model->get(['id' => $id]); + + if (empty($occurrences)) { + response('', 404); + + return; + } + $fields = $this->api->request_fields(); $with = $this->api->request_with(); @@ -96,12 +104,6 @@ class Webhooks_api_v1 extends EA_Controller $this->webhooks_model->load($webhook, $with); } - if (!$webhook) { - response('', 404); - - return; - } - json_response($webhook); } catch (Throwable $e) { json_exception($e);