[module.professor] PUT now checks for @casLogin to be unique

This commit is contained in:
xdrm-brackets 2018-03-13 11:26:08 +01:00
parent 3e0ccea7df
commit f3085ec4db
3 changed files with 40 additions and 19 deletions

View File

@ -85,7 +85,7 @@ class professorController{
$prof_repo = Repo::getRepo('professor'); $prof_repo = Repo::getRepo('professor');
/* (1) Check if professor already exists */ /* (1) Check if professor already exists */
$exists = $prof_repo->exists($lastName, $firstName); $exists = $prof_repo->exists($lastName, $firstName, $casLogin);
/* (2) If found -> already exists */ /* (2) If found -> already exists */
if( !is_null($exists) ) if( !is_null($exists) )
@ -159,14 +159,25 @@ class professorController{
$initials = null; $initials = null;
$isAdmin = null; $isAdmin = null;
$casLogin = null; $casLogin = null;
$remCas = false; $remCas = true;
extract($args); extract($args);
/* Get the professor repo */ /* Get the professor repo */
/** @var professor $prof_repo */ /** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor'); $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( return ['updated' => $prof_repo->update(
$prof_id, $prof_id,
$lastName, $lastName,

View File

@ -66,30 +66,33 @@ class professor extends Repo_i {
---------------------------------------------------------*/ ---------------------------------------------------------*/
public function exists(string $lastName, string $firstName, ?string $casLogin = null) : ?int{ 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 $st = $this->pdo->prepare("SELECT idProfesseur
FROM Professeur FROM Professeur
WHERE firstName = :firstName WHERE ( firstName = :firstName AND lastName = :lastName )
AND lastName = :lastName $cond;");
".(is_string($casLogin) ? "AND casLogin = :casLogin" : ""));
/* (2) Bind params and execute */ /* (3) Statement eror */
$params = [ if( is_bool($st) )
return NULL;
/* (4) Bind params and execute */
$params = array_merge([
':firstName' => $firstName, ':firstName' => $firstName,
':lastName' => $lastName ':lastName' => $lastName
]; ], $parm);
if(is_string($casLogin)){
$params[":casLogin"] = $casLogin;
}
$success = $st->execute(); $success = $st->execute();
/* (3) Return NULL on error */ /* (5) Return NULL on error */
if( !$success ) if( !$success )
return NULL; 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; return $st->fetch()['idProfesseur'] ?: NULL;
} }

View File

@ -345,7 +345,7 @@ gstore.add('ic_handler', function(prof_id){
/* (4.5.2.1) Manage 'already exist' error */ /* (4.5.2.1) Manage 'already exist' error */
if( rs.error == 29 ){ 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); return setTimeout(() => gstore.add('create_err', ''), 2000);
} }
@ -572,11 +572,18 @@ gstore.add('ie_handler', function(prof_i){
/* (5.8.1) Delete professor */ /* (5.8.1) Delete professor */
api.call('PUT professor/'+prof.idProfesseur, rq, function(rs){ 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 ) if( rs.error !== 0 || rs.updated !== true )
return reject(rs.error); return reject(rs.error);
/* (5.8.1.2) Success */ /* (5.8.1.3) Success */
resolve(); resolve();
}); });