From f3085ec4dbff65d79e7602555d10cea78c148882 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 13 Mar 2018 11:26:08 +0100 Subject: [PATCH] [module.professor] PUT now checks for @casLogin to be unique --- build/api/module/professorController.php | 17 +++++++++++--- build/database/repo/professor.php | 29 +++++++++++++----------- webpack/data/teacher.js | 13 ++++++++--- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/build/api/module/professorController.php b/build/api/module/professorController.php index 43e2e02..4f35962 100644 --- a/build/api/module/professorController.php +++ b/build/api/module/professorController.php @@ -85,7 +85,7 @@ class professorController{ $prof_repo = Repo::getRepo('professor'); /* (1) Check if professor already exists */ - $exists = $prof_repo->exists($lastName, $firstName); + $exists = $prof_repo->exists($lastName, $firstName, $casLogin); /* (2) If found -> already exists */ if( !is_null($exists) ) @@ -159,14 +159,25 @@ class professorController{ $initials = null; $isAdmin = null; $casLogin = null; - $remCas = false; + $remCas = true; extract($args); /* Get the professor repo */ /** @var professor $prof_repo */ $prof_repo = Repo::getRepo('professor'); - /* (1) Try to update */ + if( !is_null($casLogin) ){ + + /* (1) Check if @casLogin already exists */ + $exists = $prof_repo->getByLogin($casLogin); + + /* (2) If found -> already exists */ + if( !is_null($exists) ) + return ['error' => new Error(Err::AlreadyExists)]; + + } + + /* (3) Try to update */ return ['updated' => $prof_repo->update( $prof_id, $lastName, diff --git a/build/database/repo/professor.php b/build/database/repo/professor.php index b161ede..00e2e16 100644 --- a/build/database/repo/professor.php +++ b/build/database/repo/professor.php @@ -66,30 +66,33 @@ class professor extends Repo_i { ---------------------------------------------------------*/ public function exists(string $lastName, string $firstName, ?string $casLogin = null) : ?int{ - /* (1) Prepare Statement */ + /* (1) Manage if @casLogin given/ignored */ + $cond = is_null($casLogin) ? '' : 'OR `casLogin` = :casLogin'; + $parm = is_null($casLogin) ? [] : [':casLogin' => $casLogin]; + + /* (2) Prepare Statement */ $st = $this->pdo->prepare("SELECT idProfesseur FROM Professeur - WHERE firstName = :firstName - AND lastName = :lastName - ".(is_string($casLogin) ? "AND casLogin = :casLogin" : "")); + WHERE ( firstName = :firstName AND lastName = :lastName ) + $cond;"); - /* (2) Bind params and execute */ - $params = [ + /* (3) Statement eror */ + if( is_bool($st) ) + return NULL; + + /* (4) Bind params and execute */ + $params = array_merge([ ':firstName' => $firstName, ':lastName' => $lastName - ]; - - if(is_string($casLogin)){ - $params[":casLogin"] = $casLogin; - } + ], $parm); $success = $st->execute(); - /* (3) Return NULL on error */ + /* (5) Return NULL on error */ if( !$success ) return NULL; - /* (4) Return @prof_id or NULL if nothing found */ + /* (7) Return @prof_id or NULL if nothing found */ return $st->fetch()['idProfesseur'] ?: NULL; } diff --git a/webpack/data/teacher.js b/webpack/data/teacher.js index d735895..c1c0782 100644 --- a/webpack/data/teacher.js +++ b/webpack/data/teacher.js @@ -345,7 +345,7 @@ gstore.add('ic_handler', function(prof_id){ /* (4.5.2.1) Manage 'already exist' error */ if( rs.error == 29 ){ - gstore.get.create_err = 'Le couple Nom-Prénom est déja utilisé.'; + gstore.get.create_err = 'Le couple Nom-Prénom ou l\'identifiant sont déja utilisés.'; return setTimeout(() => gstore.add('create_err', ''), 2000); } @@ -572,11 +572,18 @@ gstore.add('ie_handler', function(prof_i){ /* (5.8.1) Delete professor */ api.call('PUT professor/'+prof.idProfesseur, rq, function(rs){ - /* (5.8.1.1) Abort on error */ + + /* (5.8.1.1) Manage 'already exist' error */ + if( rs.error == 29 ){ + gstore.get.edit_err = 'Le couple Nom-Prénom ou l\'identifiant sont déja utilisés.'; + return setTimeout(() => gstore.add('edit_err', ''), 2000); + } + + /* (5.8.1.2) Abort on error */ if( rs.error !== 0 || rs.updated !== true ) return reject(rs.error); - /* (5.8.1.2) Success */ + /* (5.8.1.3) Success */ resolve(); });