[repo.td] fixed + added requests to create|update|delete [module.td] POST to create + PUT to update (can remove a list of formations, or add a list .., update idProf (-1 to unset), volume)
This commit is contained in:
parent
428054e007
commit
45e3b005ec
|
@ -11,7 +11,37 @@ use error\core\Err;
|
|||
class tdController{
|
||||
|
||||
|
||||
/* (1) Get groups for a specific UE
|
||||
/* (1) Create a new TD
|
||||
*
|
||||
* @code<String> UE code
|
||||
*
|
||||
* @return groups<array> The list of groups for this UE
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public static function post($args){
|
||||
$code = "";
|
||||
$idProf = null;
|
||||
$volume = 0;
|
||||
$formations = [];
|
||||
extract($args);
|
||||
|
||||
/* Get the td repo */
|
||||
/** @var td $td_repo */
|
||||
$td_repo = Repo::getRepo('td');
|
||||
|
||||
/* (1) Try to create td */
|
||||
$created_id = $td_repo->create($code, $idProf, $volume, $formations);
|
||||
|
||||
/* (2) Manage error */
|
||||
if( is_null($created_id) || !is_int($created_id) )
|
||||
return ['error' => new Error(Err::RepoError)];
|
||||
|
||||
return ['created_id' => $created_id];
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (2) Get groups for a specific UE
|
||||
*
|
||||
* @code<String> UE code
|
||||
*
|
||||
|
@ -43,4 +73,98 @@ class tdController{
|
|||
}
|
||||
|
||||
|
||||
/* (3) Updates an existing TD
|
||||
*
|
||||
* @idTD<int> Id of the TD to update
|
||||
* @idProf<int> [OPT] The new professor ID (-1 to unset)
|
||||
* @volume<int> [OPT] The new number of hours for the TD
|
||||
* @add_form<array> List of ids of formations to add
|
||||
* @rem_form<array> List of ids of formations to remove
|
||||
*
|
||||
* @return updated<bool> Whether the TD have been updated
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public static function put($args){
|
||||
$idTD = -1;
|
||||
$idProf = null;
|
||||
$volume = null;
|
||||
$add_form = [];
|
||||
$rem_form = [];
|
||||
extract($args);
|
||||
|
||||
/* Get the td repo */
|
||||
/** @var td $td_repo */
|
||||
$td_repo = Repo::getRepo('td');
|
||||
|
||||
/* (1) If nothing to do -> error */
|
||||
if( is_null($idProf) && is_null($volume) && count($add_form) + count($rem_form) == 0)
|
||||
return ['error' => new Error(Err::MissingParam)];
|
||||
|
||||
/* (2) If have to update @idProf */
|
||||
if( !is_null($idProf) ){
|
||||
|
||||
// call repo
|
||||
$updated = $td_repo->updateProf($idTD, $idProf < 0 ? NULL : $idProf );
|
||||
|
||||
// if error -> dispatch
|
||||
if( !$updated )
|
||||
return ['error' => new Error(Err::RepoError)];
|
||||
|
||||
}
|
||||
|
||||
/* (3) If have to update @volume */
|
||||
if( !is_null($volume) ){
|
||||
|
||||
// call repo
|
||||
$updated = $td_repo->updateVolume($idTD, $volume);
|
||||
|
||||
// if error -> dispatch
|
||||
if( !$updated )
|
||||
return ['error' => new Error(Err::RepoError)];
|
||||
|
||||
}
|
||||
|
||||
/* (4) If have to add formations -> add them */
|
||||
if( count($add_form) > 0 ){
|
||||
|
||||
foreach($add_form as $id_form)
|
||||
$td_repo->linkFormation($id_form, $idTD);
|
||||
|
||||
}
|
||||
|
||||
/* (5) If have to remove formations -> remove them */
|
||||
if( count($rem_form) > 0 ){
|
||||
|
||||
foreach($rem_form as $id_form)
|
||||
$td_repo->unlinkFormation($id_form, $idTD);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return ['updated' => true];
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (4) Deletes an existing TD
|
||||
*
|
||||
* @idTD<int> Id of the TD
|
||||
*
|
||||
* @return deleted<bool> Whether it has been deleted
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public static function delete($args){
|
||||
$idTD = 0;
|
||||
extract($args);
|
||||
|
||||
/* Get the td repo */
|
||||
/** @var td $td_repo */
|
||||
$td_repo = Repo::getRepo('td');
|
||||
|
||||
/* (1) Dispatch response from repo */
|
||||
return ['deleted' => $td_repo->delete($idTD)];
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -13,76 +13,191 @@ use database\core\Repo_i;
|
|||
|
||||
class td extends Repo_i {
|
||||
|
||||
public function create(string $codeUE, ?int $idProf, float $volume, array $formations) : int{
|
||||
//create the group
|
||||
$st = $this->pdo->prepare("INSERT INTO TD(UE_code, Professeur_idProfesseur, volume)
|
||||
VALUE(:UE, :idProf, :vol)");
|
||||
/* (1) Create a new TD
|
||||
*
|
||||
* @code<String> The code of the UE containing the TD
|
||||
* @idProf<String> [OPT] The ID of the prof who teaches the TD
|
||||
* @volume<float> The number of hours (float)
|
||||
* @formations<array> The list of formations for that TD (list of int)
|
||||
* @return created_code<String> Code of the created UE (NULL on error)
|
||||
*
|
||||
* @return created_id<int> UID of the created TD (NULL on error)
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public function create(string $code, ?int $idProf, float $volume, array $formations) : int{
|
||||
|
||||
$st->execute([
|
||||
"UE" => $codeUE,
|
||||
"idProf" => $idProf,
|
||||
"vol" => $volume
|
||||
/* (1) Prepare statement */
|
||||
$st = $this->pdo->prepare('INSERT INTO TD(UE_code, Professeur_idProfesseur, volume)
|
||||
VALUE(:UE, :idProf, :vol)');
|
||||
|
||||
/* (2) Manage statement error */
|
||||
if( is_bool($st) )
|
||||
return NULL;
|
||||
|
||||
/* (3) Try to execute request */
|
||||
$success = $st->execute([
|
||||
':UE' => $code,
|
||||
':idProf' => $idProf,
|
||||
':vol' => $volume
|
||||
]);
|
||||
|
||||
$idTD = $this->pdo->lastInsertId();
|
||||
/* (4) Manage error */
|
||||
if( !$success )
|
||||
return NULL;
|
||||
|
||||
//if there is formations, link them with the group
|
||||
if(count($formations) > 0){
|
||||
$linkSt = $this->pdo->prepare("INSERT INTO GroupeTD(Formation_idFormation, TD_idTD)
|
||||
VALUE (:form, :TD)");
|
||||
foreach ($formations as $form){
|
||||
$linkSt->execute([
|
||||
"form" => $form,
|
||||
"TD" => $idTD
|
||||
]);
|
||||
}
|
||||
/* (5) Store @created_id */
|
||||
$created_id = (int) $this->pdo->lastInsertId();
|
||||
|
||||
|
||||
/* (6) We are done if there is no formations */
|
||||
if( count($formations) <= 0)
|
||||
return $created_id;
|
||||
|
||||
/* (7) Else -> create each formation */
|
||||
foreach($formations as $form_id){
|
||||
|
||||
// 1. Ignore if wrong format
|
||||
if( !is_int($form_id) || $form_id < 0 )
|
||||
continue;
|
||||
|
||||
// 2. Link formation to created TD
|
||||
$this->linkFormation($form_id, $created_id);
|
||||
}
|
||||
|
||||
return $idTD;
|
||||
/* (7) Return @created_id */
|
||||
return $created_id;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (2) Unlink a formation from a TD
|
||||
*
|
||||
* @idFormation<int> Id of the formation
|
||||
* @idTD<int> Id of the TD
|
||||
*
|
||||
* @return unlinked<bool> Whether it has been unlinked
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public function unlinkFormation(int $idFormation, int $idTD) : bool{
|
||||
$st = $this->pdo->prepare("DELETE FROM GroupeTD WHERE TD_idTD = :TD AND Formation_idFormation = :form");
|
||||
|
||||
/* (1) Prepare statement */
|
||||
$st = $this->pdo->prepare('DELETE FROM GroupeTD WHERE TD_idTD = :td AND Formation_idFormation = :form');
|
||||
|
||||
/* (2) Manage statement error */
|
||||
if( is_bool($st) )
|
||||
return false;
|
||||
|
||||
/* (3) EXecute and dispatch status */
|
||||
return $st->execute([
|
||||
"TD" => $idTD,
|
||||
"form" => $idFormation
|
||||
':td' => $idTD,
|
||||
':form' => $idFormation
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (3) Link a formation to a TD
|
||||
*
|
||||
* @idFormation<int> Id of the formation
|
||||
* @idTD<int> Id of the TD
|
||||
*
|
||||
* @return linked<bool> Whether it has been linked
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public function linkFormation(int $idFormation, int $idTD) : bool{
|
||||
$st = $this->pdo->prepare("INSERT INTO GroupeTD(TD_idTD,Formation_idFormation)
|
||||
VALUE(:TD, :form)");
|
||||
|
||||
/* (1) Try to remove first if it already exists */
|
||||
$this->unlinkFormation($idFormation, $idTD);
|
||||
|
||||
/* (2) Prepare statement */
|
||||
$st = $this->pdo->prepare('INSERT INTO GroupeTD(TD_idTD,Formation_idFormation)
|
||||
VALUE(:td, :form)');
|
||||
|
||||
/* (3) Manage statement error */
|
||||
if( is_bool($st) )
|
||||
return false;
|
||||
|
||||
/* (4) EXecute and dispatch status */
|
||||
return $st->execute([
|
||||
"TD" => $idTD,
|
||||
"form" => $idFormation
|
||||
':td' => $idTD,
|
||||
':form' => $idFormation
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function updateProf(?int $prof) : bool {
|
||||
$st = $this->pdo->prepare("UPDATE TD SET Professeur_idProfesseur = :prof");
|
||||
|
||||
return $st->execute([
|
||||
"prof" => $prof
|
||||
]);
|
||||
|
||||
|
||||
|
||||
/* (4.1) Updates an existing TD
|
||||
*
|
||||
* @idTD<int> Id of the TD
|
||||
* @idProf<int> [OPT] Id of the prof (NULL to set to NULL)
|
||||
*
|
||||
* @return updated<bool> Whether it has been updated
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public function updateProf(int $idTD, ?int $idProf) : bool {
|
||||
|
||||
/* (1) Prepare statement */
|
||||
$st = $this->pdo->prepare('UPDATE TD
|
||||
SET Professeur_idProfesseur = :idProf
|
||||
WHERE idTD = :idTD');
|
||||
|
||||
/* (2) Manage statement error */
|
||||
if( is_bool($st) )
|
||||
return false;
|
||||
|
||||
/* (3) Execute and dispatch status */
|
||||
return $st->execute([ ':idTD' => $idTD, ':idProf' => $idProf ]);
|
||||
|
||||
}
|
||||
|
||||
public function updateVolume(float $volume) : bool {
|
||||
$st = $this->pdo->prepare("UPDATE TD SET volume = :vol");
|
||||
/* (4.2) Updates an existing TD
|
||||
*
|
||||
* @idTD<int> Id of the TD
|
||||
* @volume<float> [OPT] The new number of hours
|
||||
*
|
||||
* @return updated<bool> Whether it has been updated
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public function updateVolume(int $idTD, float $volume) : bool {
|
||||
|
||||
/* (1) Prepare statement */
|
||||
$st = $this->pdo->prepare('UPDATE TD
|
||||
SET volume = :volume
|
||||
WHERE idTD = :idTD');
|
||||
|
||||
/* (2) Manage statement error */
|
||||
if( is_bool($st) )
|
||||
return false;
|
||||
|
||||
/* (3) Execute and dispatch status */
|
||||
return $st->execute([ ':idTD' => $idTD, ':volume' => $volume ]);
|
||||
|
||||
return $st->execute([
|
||||
"vol" => $volume
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* (5) Deletes an existing td
|
||||
*
|
||||
* @idTD<int> Id of the TD
|
||||
*
|
||||
* @return deleted<bool> Whether it has been deleted
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
public function delete(int $id) :bool {
|
||||
|
||||
/* (1) Prepare statement */
|
||||
$st = $this->pdo->prepare("DELETE FROM TD WHERE idTD = :id");
|
||||
|
||||
return $st->execute([
|
||||
"id" => $id
|
||||
]);
|
||||
/* (2) Manage statement error */
|
||||
if( is_bool($st) )
|
||||
return false;
|
||||
|
||||
/* (3) Execute and dispatch status */
|
||||
return $st->execute([ ':id' => $id ]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -98,21 +213,21 @@ class td extends Repo_i {
|
|||
|
||||
/* (1) Prepare statement */
|
||||
$st = $this->pdo->prepare("SELECT
|
||||
td.UE_code code,
|
||||
td.idTD,
|
||||
c.UE_code code,
|
||||
c.idTD,
|
||||
p.idProfesseur idProf,
|
||||
p.firstName,
|
||||
p.lastName,
|
||||
IFNULL(lform.flist, '[]') formations,
|
||||
td.volume
|
||||
FROM TD td
|
||||
LEFT JOIN ( SELECT gtd.TD_idTD idTD, CONCAT('[', GROUP_CONCAT(DISTINCT gtd.Formation_idFormation),']') flist
|
||||
FROM GroupeTD gtd
|
||||
GROUP BY gtd.TD_idTD
|
||||
ORDER BY gtd.Formation_idFormation DESC
|
||||
) lform ON td.idTD = lform.idTD
|
||||
LEFT JOIN Professeur p ON p.idProfesseur = td.Professeur_idProfesseur
|
||||
WHERE td.UE_code = :code;");
|
||||
c.volume
|
||||
FROM TD c
|
||||
LEFT JOIN ( SELECT gc.TD_idTD idTD, CONCAT('[', GROUP_CONCAT(DISTINCT gc.Formation_idFormation),']') flist
|
||||
FROM GroupeTD gc
|
||||
GROUP BY gc.TD_idTD
|
||||
ORDER BY gc.Formation_idFormation DESC
|
||||
) lform ON c.idTD = lform.idTD
|
||||
LEFT JOIN Professeur p ON p.idProfesseur = c.Professeur_idProfesseur
|
||||
WHERE c.UE_code = :code;");
|
||||
|
||||
/* (2) Check statement */
|
||||
if( is_bool($st) )
|
||||
|
|
|
@ -320,12 +320,52 @@
|
|||
|
||||
"td": {
|
||||
|
||||
"POST": {
|
||||
"des" : "Creates a new TD for an UE",
|
||||
"per": [],
|
||||
"par": {
|
||||
"code": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)" },
|
||||
"idProf": { "des": "Id of the professor", "typ": "id", "opt": true },
|
||||
"volume": { "des": "Number of hours for TD", "typ": "id", "opt": true, "def": 0 },
|
||||
"formations": { "des": "List of formations (ids)", "typ": "array<id>", "opt": true, "def": [] }
|
||||
},
|
||||
"output": {
|
||||
"created_id" : { "des": "The id of the created TD", "typ": "id" }
|
||||
}
|
||||
},
|
||||
|
||||
"GET": {
|
||||
"des" : "Get all TD data about a given UE",
|
||||
"per": [],
|
||||
"par": {
|
||||
"URL0": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)", "ren": "code" }
|
||||
}
|
||||
},
|
||||
|
||||
"PUT": {
|
||||
"des" : "Updates an existing TD",
|
||||
"per": [],
|
||||
"par": {
|
||||
"URL0": { "des": "Id of the TD", "typ": "id", "ren": "idTD" },
|
||||
"idProf": { "des": "Id of the professor (-1 to unset)", "typ": "int", "opt": true },
|
||||
"volume": { "des": "Number of hours for TD", "typ": "float", "opt": true },
|
||||
"add_form": { "des": "Id of formations to add", "typ": "array<id>", "opt": true, "def": [] },
|
||||
"rem_form": { "des": "Id of formations to remove", "typ": "array<id>", "opt": true, "def": [] }
|
||||
},
|
||||
"output": {
|
||||
"updated" : { "des": "Whether it has been updated", "typ": "bool" }
|
||||
}
|
||||
},
|
||||
|
||||
"DELETE": {
|
||||
"des" : "Deletes an existing TD",
|
||||
"per": [],
|
||||
"par": {
|
||||
"URL0": { "des": "Id of the TD", "typ": "id", "ren": "idTD" }
|
||||
},
|
||||
"output": {
|
||||
"deleted" : { "des": "Whether it has been deleted", "typ": "bool" }
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue