From a7cd262bb02d4f5e2ea554fc7eb790bb941a9301 Mon Sep 17 00:00:00 2001 From: Mohammed-Aymen Benadra Date: Sun, 15 May 2022 01:38:36 +0100 Subject: [PATCH] Moved to using Exists and Unique in scaffolding Examples --- app/Models/Example.php | 2 +- app/controllers/Api.php | 31 +------------------------------ 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/app/Models/Example.php b/app/Models/Example.php index d62a4f7..e1b9b44 100644 --- a/app/Models/Example.php +++ b/app/Models/Example.php @@ -16,7 +16,7 @@ class Example extends Model public function __construct() { parent::__construct([ - 'exampleField' => 'required|numeric|min:1|max:10|int|float|bool|array|object|email|file|image|url|date|date_format|same|matches|ip' + 'exampleField' => 'required|numeric|min:1|max:10|int|float|bool|array|object|email|file|image|url|date|date_format|same|matches|ip|exists:table,field|unique:table,field|regex:pattern' ]); $this->table = 'Examples'; } diff --git a/app/controllers/Api.php b/app/controllers/Api.php index 1146b75..9519e32 100644 --- a/app/controllers/Api.php +++ b/app/controllers/Api.php @@ -59,18 +59,9 @@ public function index() */ public function show($data = []) { - $example = $this->model->get($data['id']); - - if ($example === false) { - Router::abort(404, json_encode([ - 'status' => 'error', - 'message' => 'example not found' - ])); - } - Response::send([ 'status' => 'success', - 'data' => $example + 'data' => $this->model->get($data['id']) ]); } @@ -113,16 +104,6 @@ public function update($data = []) $id = $data['id']; unset($data['id']); - // check if example exists - $example = $this->model->get($id); - - if (!$example) { - Router::abort(404, json_encode([ - 'status' => 'error', - 'message' => 'example not found' - ])); - } - if (!$this->model->update($id, $data)) { Router::abort(500, json_encode([ 'status' => 'error', @@ -146,16 +127,6 @@ public function update($data = []) */ public function delete($data = []) { - // check if example exists - $example = $this->model->get($data['id']); - - if (!$example) { - Router::abort(404, json_encode([ - 'status' => 'error', - 'message' => 'example not found' - ])); - } - if (!$this->model->delete($data['id'])) { Router::abort(500, json_encode([ 'status' => 'error',