From 85e826a8a1b8a0d019f71244d86d18378db936eb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 20 Feb 2017 12:07:44 +0100 Subject: [PATCH] =?UTF-8?q?[Done]=20Managed=20anti-duplication=20name=20po?= =?UTF-8?q?ur=20`machineDefault/create`=20et=20`machineDefault/edit`=20(in?= =?UTF-8?q?terne=20=C3=A0=20warehouse)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/api/module/machineDefault.php | 29 +++++++++++++++++++++++++---- build/api/module/userDefault.php | 8 ++++---- build/database/repo/machine.php | 10 +++++++--- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/build/api/module/machineDefault.php b/build/api/module/machineDefault.php index 7bdea17..3fc54f7 100755 --- a/build/api/module/machineDefault.php +++ b/build/api/module/machineDefault.php @@ -24,7 +24,18 @@ public function create($params){ extract($params); - /* [1] Creation de la machine + /* [1] Vérification unicité (name) + =========================================================*/ + /* (1) Check if unique : name */ + $byName_r = new Repo('machine/getByName', [$_SESSION['WAREHOUSE']['id'], $name]); + $byName = $byName_r->answer(); + + /* (2) If already used -> error */ + if( $byName != false ) + return ['error' => new Error(Err::AlreadyExists, 'name')]; + + + /* [2] Creation de la machine =========================================================*/ $create_machine = new Repo('machine/create', [ $_SESSION['WAREHOUSE']['id'], @@ -38,7 +49,7 @@ - /* [2] Gestion du retour + /* [3] Gestion du retour =========================================================*/ return [ 'id_machine' => $id_machine @@ -256,8 +267,18 @@ + /* [2] Vérification unicité (name) + =========================================================*/ + /* (1) Check if unique : name */ + $byName_r = new Repo('machine/getByName', [$_SESSION['WAREHOUSE']['id'], $name]); + $byName = $byName_r->answer(); - /* [2] Normalisation + verification des donnees + /* (2) If already used (not current machine) -> error */ + if( $byName != false && $byName['id_machine'] != $id_machine ) + return ['error' => new Error(Err::AlreadyExists, 'name')]; + + + /* [3] Normalisation + verification des donnees =========================================================*/ /* (1) Verification des parametres (si correct et different)*/ @@ -271,7 +292,7 @@ ]; - /* [3] Modification de la machine + /* [4] Modification de la machine =========================================================*/ $request = new Repo('machine/edit', [ $_SESSION['WAREHOUSE']['id'], diff --git a/build/api/module/userDefault.php b/build/api/module/userDefault.php index 8b42ed8..cbdf1a6 100755 --- a/build/api/module/userDefault.php +++ b/build/api/module/userDefault.php @@ -46,7 +46,7 @@ return ['error' => new Error(Err::AlreadyExists, 'code')]; - /* [1] Creation de l'utilisateur + /* [2] Creation de l'utilisateur =========================================================*/ $create_user = new Repo('user/create', [ $_SESSION['WAREHOUSE']['id'], @@ -63,7 +63,7 @@ return ['error' => new Error(Err::RepoError)]; - /* [2] Gestion du retour + /* [3] Gestion du retour =========================================================*/ return [ 'id_user' => $id_user @@ -332,7 +332,7 @@ if( $byCode != false && $byCode['id_user'] != $id_user ) return ['error' => new Error(Err::AlreadyExists, 'code')]; - /* [2] Normalisation + verification des donnees + /* [3] Normalisation + verification des donnees =========================================================*/ /* (1) Verification des parametres (si non nul + differents)*/ $diff_param = [ @@ -355,7 +355,7 @@ - /* [3] Modification de l'utilisateur + /* [4] Modification de l'utilisateur =========================================================*/ $request = new Repo('user/edit', [ $_SESSION['WAREHOUSE']['id'], diff --git a/build/database/repo/machine.php b/build/database/repo/machine.php index c2f432a..4f1ad85 100755 --- a/build/database/repo/machine.php +++ b/build/database/repo/machine.php @@ -4,6 +4,8 @@ use \database\core\DatabaseDriver; use \orm\core\Table; use \orm\core\Rows; + use \error\core\Error; + use \error\core\Err; use \api\core\Checker; use \manager\repo\cluster as clusterRepo; @@ -232,14 +234,15 @@ ->select('token') ->select('unlock_code') ->orderby('name', Rows::ORDER_ASC) - ->unique(); + ->unique() + ->fetch(); /* (2) Manage error */ if( $machine === false ) return false; /* (3) On remplace les valeurs 'unlock_code' et 'token' */ - $machine['token'] = strlen($machine['token']) > 0; + $machine['token'] = strlen($machine['token']) > 0; $machine['unlock_code'] = strlen($machine['unlock_code']) > 0; /* (4) On retourne la réponse */ @@ -275,7 +278,8 @@ ->select('token') ->select('unlock_code') ->orderby('name', Rows::ORDER_ASC) - ->unique(); + ->unique() + ->fetch(); /* (2) Manage error */ if( $machine === false )