From 38600994c08a28e16db868b787994e10943ba1cb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 19 Mar 2018 23:32:09 +0100 Subject: [PATCH] [repo.ue] UPDATE manage 'new_code' [module.ue] PUT manages optional argument 'new_code' [webpack.data.ue] display input for 'code' and send 'new_code' to API if set --- build/api/module/ueController.php | 24 +++++++++++++++++++++++- build/database/repo/ue.php | 4 +++- config/modules.json | 1 + webpack/component/ue/view.vue | 4 ++-- webpack/data/ue.js | 25 +++++++++++++++---------- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/build/api/module/ueController.php b/build/api/module/ueController.php index c3047ca..601eafd 100644 --- a/build/api/module/ueController.php +++ b/build/api/module/ueController.php @@ -130,6 +130,7 @@ class ueController{ /* (4) Edits an existing UE * * @code The code of the UE + * @new_code [OPT] The new code of the UE * @label [OPT] The UE label (name) * @required [OPT] If the UE is required * @volumeCours [OPT] The UE required volume of COURSES @@ -143,6 +144,7 @@ class ueController{ ---------------------------------------------------------*/ public static function put($args){ $code = ""; + $new_code = ""; $label = ""; $required = false; $volumeCours = 0; @@ -156,9 +158,29 @@ class ueController{ /** @var ue $ue_repo */ $ue_repo = Repo::getRepo('ue'); - /* (1) Try to update */ + + + /* (1) Check for @new_code to be unique (not already used) + ---------------------------------------------------------*/ + if( !is_null($new_code) ){ + + /* (1) Check @new_code */ + $exists = $ue_repo->get($new_code); + + /* (2) If found -> already exists */ + if( count($exists) > 0 ) + return ['error' => new Error(Err::AlreadyExists)]; + + + } + + + + /* (2) Try to update + ---------------------------------------------------------*/ return ['updated' => $ue_repo->update( $code, + $new_code, $label, $required, $volumeCours, diff --git a/build/database/repo/ue.php b/build/database/repo/ue.php index 86cd956..f717516 100644 --- a/build/database/repo/ue.php +++ b/build/database/repo/ue.php @@ -63,6 +63,7 @@ class ue extends Repo_i { /* (2) Updates an UE data * * @code The UE code + * @new_code [OPT] The new UE code * @label [OPT] The UE's new label * @required [OPT] Whether the UE is required * @volumeCours [OPT] The UE's new volume of COURSES @@ -74,12 +75,13 @@ class ue extends Repo_i { * @return updated Whether the update have been successful * ---------------------------------------------------------*/ - public function update(string $code, ?String $label, ?bool $required, ?float $volumeCours, ?float $volumeTD, ?float $volumeTP, ?bool $disabled, ?int $defaultFormation) : bool{ + public function update(string $code, ?String $new_code, ?String $label, ?bool $required, ?float $volumeCours, ?float $volumeTD, ?float $volumeTP, ?bool $disabled, ?int $defaultFormation) : bool{ /* (1) Build request */ $build_rq = []; $bind_param = [ ':code' => $code ]; + if( !is_null($new_code) ){ $build_rq[] = '`code` = :new_code'; $bind_param[':new_code'] = $new_code; } if( !is_null($label) ){ $build_rq[] = '`label` = :label'; $bind_param[':label'] = $label; } if( !is_null($required) ){ $build_rq[] = '`required` = :required'; $bind_param[':required'] = $required?1:0; } if( !is_null($volumeCours) ){ $build_rq[] = '`volumeCours` = :volumeCours'; $bind_param[':volumeCours'] = $volumeCours; } diff --git a/config/modules.json b/config/modules.json index 81ab578..72a3d94 100644 --- a/config/modules.json +++ b/config/modules.json @@ -270,6 +270,7 @@ "per": [], "par": { "URL0": { "des": "UE code.", "typ": "varchar(4,20,alphanumeric)", "ren": "code" }, + "new_code": { "des": "UE new code", "typ": "varchar(4,20,alphanumeric)", "opt": true }, "label": { "des": "UE label", "typ": "varchar(4,30,alphanumeric)", "opt": true }, "required": { "des": "If UE is required", "typ": "bool", "opt": true }, "volumeCours": { "des": "Number of course hours for UE", "typ": "float", "opt": true }, diff --git a/webpack/component/ue/view.vue b/webpack/component/ue/view.vue index 4cd2182..38a5f25 100644 --- a/webpack/component/ue/view.vue +++ b/webpack/component/ue/view.vue @@ -65,8 +65,8 @@

- ({{ ue.code }}) - + + ()

diff --git a/webpack/data/ue.js b/webpack/data/ue.js index ce7be8b..789528b 100644 --- a/webpack/data/ue.js +++ b/webpack/data/ue.js @@ -329,7 +329,7 @@ gstore.add('edit_i', -1); /* (2) Initialize inputs */ gstore.add('edit_form', '-'); gstore.add('edit_label', ''); -// gstore.add('edit_code', ''); +gstore.add('edit_code', ''); gstore.add('edit_vol', { c: '', td: '', tp: ''}); /* (3) Initialize error message */ @@ -372,7 +372,7 @@ gstore.add('ie_handler', function(ue_i){ /* (5.3) Trim text input */ gstore.get.edit_label = gstore.get.edit_label.trim(); - // gstore.get.edit_code = gstore.get.edit_code.toString().trim().toUpperCase(); + gstore.get.edit_code = gstore.get.edit_code.toString().trim().toUpperCase(); gstore.get.edit_vol.c = gstore.get.edit_vol.c.toString().trim(); gstore.get.edit_vol.td = gstore.get.edit_vol.td.toString().trim(); gstore.get.edit_vol.tp = gstore.get.edit_vol.tp.toString().trim(); @@ -380,7 +380,7 @@ gstore.add('ie_handler', function(ue_i){ /* (5.4) Store values locally */ var form = gstore.get.edit_form; var label = gstore.get.edit_label; - // var code = gstore.get.edit_code; + var code = gstore.get.edit_code; var vco = gstore.get.edit_vol.c; var vtd = gstore.get.edit_vol.td; var vtp = gstore.get.edit_vol.tp; @@ -396,8 +396,8 @@ gstore.add('ie_handler', function(ue_i){ errors.push('Le label doit comprendre faire au moins 4 caractères'); /* (5.5.3) Check code */ - // if( !/^[A-Z0-9]{4,20}$/.test(code) ) - // errors.push('Le code doit comprendre de 4 à 20 lettres/chiffres'); + if( !/^[A-Z0-9]{4,20}$/.test(code) ) + errors.push('Le code doit comprendre de 4 à 20 lettres/chiffres'); /* (5.5.4) Check volumes */ if( vco === '' || isNaN(vco) || vco < 0 ) @@ -416,7 +416,7 @@ gstore.add('ie_handler', function(ue_i){ /* (5.7) Création de la requête */ var rq = {}; ( label != ue.label ) && ( rq.label = label ); - // ( code != ue.code ) && ( rq.code = code ); + ( code != ue.code ) && ( rq.new_code = code ); ( vco != ue.volumeCours ) && ( rq.volumeCours = parseInt(vco) ); ( vtd != ue.volumeTD ) && ( rq.volumeTD = parseInt(vtd) ); ( vtp != ue.volumeTP ) && ( rq.volumeTP = parseInt(vtp) ); @@ -439,12 +439,17 @@ gstore.add('ie_handler', function(ue_i){ /* (5.8.1) Update UE */ api.call(`PUT ue/${ue.code}`, rq, function(rs){ - /* (5.8.1.1) Abort on error */ - console.log(rq, rs.error != 0, rs.updated != true); + /* (5.8.1.1) Manage 'already exist' error */ + if( rs.error == 29 ){ + gstore.get.edit_err = 'Le nouveau code est déja utilisé par une autre UE.'; + 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(); }); @@ -456,7 +461,7 @@ gstore.add('ie_handler', function(ue_i){ /* (5.9.1) update VueJS element */ gstore.get.ues[ue_i].label = label; - // gstore.get.ues[ue_i].code = code; + gstore.get.ues[ue_i].code = code; gstore.get.ues[ue_i].idForm = parseInt(form); gstore.get.ues[ue_i].volumeCours = parseInt(vco); gstore.get.ues[ue_i].volumeTD = parseInt(vtd);