Skip to content

Commit

Permalink
Make sure the API returns 404 on show if the ID was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
alextselegidis committed Jan 5, 2024
1 parent aee7bb8 commit cd6280b
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 54 deletions.
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Admins_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public function index(): void
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();
Expand All @@ -98,12 +106,6 @@ public function show(int $id = null): void
$this->admins_model->load($admin, $with);
}

if (!$admin) {
response('', 404);

return;
}

json_response($admin);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Appointments_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ private function aggregates(array &$appointment): void
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();
Expand All @@ -190,12 +198,6 @@ public function show(int $id = null): void
$this->appointments_model->load($appointment, $with);
}

if (!$appointment) {
response('Not Found', 404);

return;
}

json_response($appointment);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Customers_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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);
Expand All @@ -90,12 +98,6 @@ public function show(int $id = null): void
$this->customers_model->only($customer, $fields);
}

if (!$customer) {
response('', 404);

return;
}

json_response($customer);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Providers_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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();
Expand All @@ -96,12 +104,6 @@ public function show(int $id = null): void
$this->providers_model->load($provider, $with);
}

if (!$provider) {
response('', 404);

return;
}

json_response($provider);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Secretaries_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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);
Expand All @@ -90,12 +98,6 @@ public function show(int $id = null): void
$this->secretaries_model->only($secretary, $fields);
}

if (!$secretary) {
response('', 404);

return;
}

json_response($secretary);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Service_categories_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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();
Expand All @@ -96,12 +104,6 @@ public function show(int $id = null): void
$this->service_categories_model->load($service_category, $with);
}

if (!$service_category) {
response('', 404);

return;
}

json_response($service_category);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Services_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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();
Expand All @@ -96,12 +104,6 @@ public function show(int $id = null): void
$this->services_model->load($service, $with);
}

if (!$service) {
response('', 404);

return;
}

json_response($service);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Unavailabilities_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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();
Expand All @@ -96,12 +104,6 @@ public function show(int $id = null): void
$this->unavailabilities_model->load($unavailability, $with);
}

if (!$unavailability) {
response('', 404);

return;
}

json_response($unavailability);
} catch (Throwable $e) {
json_exception($e);
Expand Down
14 changes: 8 additions & 6 deletions application/controllers/api/v1/Webhooks_api_v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function index(): void
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();
Expand All @@ -96,12 +104,6 @@ public function show(int $id = null): void
$this->webhooks_model->load($webhook, $with);
}

if (!$webhook) {
response('', 404);

return;
}

json_response($webhook);
} catch (Throwable $e) {
json_exception($e);
Expand Down

0 comments on commit cd6280b

Please sign in to comment.