[repo.cours] fixed + added requests to create|update|delete [module.cours] 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:
xdrm-brackets 2018-03-15 19:17:28 +01:00
parent e1bdac7f5b
commit 0c0c37b0a1
3 changed files with 323 additions and 41 deletions

View File

@ -11,7 +11,37 @@ use error\core\Err;
class coursController{ class coursController{
/* (1) Get groups for a specific UE /* (1) Create a new Cours
*
* @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 cours repo */
/** @var cours $cours_repo */
$cours_repo = Repo::getRepo('cours');
/* (1) Try to create cours */
$created_id = $cours_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 * @code<String> UE code
* *
@ -43,4 +73,98 @@ class coursController{
} }
/* (3) Updates an existing Cours
*
* @idCours<int> Id of the Cours to update
* @idProf<int> [OPT] The new professor ID (-1 to unset)
* @volume<int> [OPT] The new number of hours for the Cours
* @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 Cours have been updated
*
---------------------------------------------------------*/
public static function put($args){
$idCours = -1;
$idProf = null;
$volume = null;
$add_form = [];
$rem_form = [];
extract($args);
/* Get the cours repo */
/** @var cours $cours_repo */
$cours_repo = Repo::getRepo('cours');
/* (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 = $cours_repo->updateProf($idCours, $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 = $cours_repo->updateVolume($idCours, $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)
$cours_repo->linkFormation($id_form, $idCours);
}
/* (5) If have to remove formations -> remove them */
if( count($rem_form) > 0 ){
foreach($rem_form as $id_form)
$cours_repo->unlinkFormation($id_form, $idCours);
}
return ['updated' => true];
}
/* (4) Deletes an existing Cours
*
* @idCours<int> Id of the Cours
*
* @return deleted<bool> Whether it has been deleted
*
---------------------------------------------------------*/
public static function delete($args){
$idCours = 0;
extract($args);
/* Get the cours repo */
/** @var cours $cours_repo */
$cours_repo = Repo::getRepo('cours');
/* (1) Dispatch response from repo */
return ['deleted' => $cours_repo->delete($idCours)];
}
} }

View File

@ -13,75 +13,191 @@ use database\core\Repo_i;
class cours extends Repo_i { class cours extends Repo_i {
public function create(string $codeUE, ?int $idProf, float $volume, array $formations) : int{ /* (1) Create a new Cours
//create the group *
$st = $this->pdo->prepare("INSERT INTO Cours(UE_code, Professeur_idProfesseur, volume) * @code<String> The code of the UE containing the Cours
VALUE(:UE, :idProf, :vol)"); * @idProf<String> [OPT] The ID of the prof who teaches the Cours
$st->execute([ * @volume<float> The number of hours (float)
"UE" => $codeUE, * @formations<array> The list of formations for that Cours (list of int)
"idProf" => $idProf, * @return created_code<String> Code of the created UE (NULL on error)
"vol" => $volume *
* @return created_id<int> UID of the created Cours (NULL on error)
*
---------------------------------------------------------*/
public function create(string $code, ?int $idProf, float $volume, array $formations) : int{
/* (1) Prepare statement */
$st = $this->pdo->prepare('INSERT INTO Cours(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
]); ]);
$idCours = $this->pdo->lastInsertId(); /* (4) Manage error */
if( !$success )
return NULL;
//if there is formations, link them with the group /* (5) Store @created_id */
if(count($formations) > 0){ $created_id = (int) $this->pdo->lastInsertId();
$linkSt = $this->pdo->prepare("INSERT INTO GroupeCours(Formation_idFormation, Cours_idCours)
VALUE (:form, :cours)");
foreach ($formations as $form){ /* (6) We are done if there is no formations */
$linkSt->execute([ if( count($formations) <= 0)
"form" => $form, return $created_id;
"cours" => $idCours
]); /* (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 Cours
$this->linkFormation($form_id, $created_id);
} }
return $idCours; /* (7) Return @created_id */
return $created_id;
} }
/* (2) Unlink a formation from a Cours
*
* @idFormation<int> Id of the formation
* @idCours<int> Id of the Cours
*
* @return unlinked<bool> Whether it has been unlinked
*
---------------------------------------------------------*/
public function unlinkFormation(int $idFormation, int $idCours) : bool{ public function unlinkFormation(int $idFormation, int $idCours) : bool{
$st = $this->pdo->prepare("DELETE FROM GroupeCours WHERE Cours_idCours = :cours AND Formation_idFormation = :form");
/* (1) Prepare statement */
$st = $this->pdo->prepare('DELETE FROM GroupeCours WHERE Cours_idCours = :cours AND Formation_idFormation = :form');
/* (2) Manage statement error */
if( is_bool($st) )
return false;
/* (3) EXecute and dispatch status */
return $st->execute([ return $st->execute([
"cours" => $idCours, ':cours' => $idCours,
"form" => $idFormation ':form' => $idFormation
]); ]);
} }
/* (3) Link a formation to a Cours
*
* @idFormation<int> Id of the formation
* @idCours<int> Id of the Cours
*
* @return linked<bool> Whether it has been linked
*
---------------------------------------------------------*/
public function linkFormation(int $idFormation, int $idCours) : bool{ public function linkFormation(int $idFormation, int $idCours) : bool{
$st = $this->pdo->prepare("INSERT INTO GroupeCours(Cours_idCours,Formation_idFormation)
VALUE(:cours, :form)");
/* (1) Try to remove first if it already exists */
$this->unlinkFormation($idFormation, $idCours);
/* (2) Prepare statement */
$st = $this->pdo->prepare('INSERT INTO GroupeCours(Cours_idCours,Formation_idFormation)
VALUE(:cours, :form)');
/* (3) Manage statement error */
if( is_bool($st) )
return false;
/* (4) EXecute and dispatch status */
return $st->execute([ return $st->execute([
"cours" => $idCours, ':cours' => $idCours,
"form" => $idFormation ':form' => $idFormation
]); ]);
} }
public function updateProf(?int $prof) : bool {
$st = $this->pdo->prepare("UPDATE Cours SET Professeur_idProfesseur = :prof");
return $st->execute([
"prof" => $prof
]);
/* (4.1) Updates an existing Cours
*
* @idCours<int> Id of the Cours
* @idProf<int> [OPT] Id of the prof (NULL to set to NULL)
*
* @return updated<bool> Whether it has been updated
*
---------------------------------------------------------*/
public function updateProf(int $idCours, ?int $idProf) : bool {
/* (1) Prepare statement */
$st = $this->pdo->prepare('UPDATE Cours
SET Professeur_idProfesseur = :idProf
WHERE idCours = :idCours');
/* (2) Manage statement error */
if( is_bool($st) )
return false;
/* (3) Execute and dispatch status */
return $st->execute([ ':idCours' => $idCours, ':idProf' => $idProf ]);
} }
public function updateVolume(float $volume) : bool { /* (4.2) Updates an existing Cours
$st = $this->pdo->prepare("UPDATE Cours SET volume = :vol"); *
* @idCours<int> Id of the Cours
* @volume<float> [OPT] The new number of hours
*
* @return updated<bool> Whether it has been updated
*
---------------------------------------------------------*/
public function updateVolume(int $idCours, float $volume) : bool {
/* (1) Prepare statement */
$st = $this->pdo->prepare('UPDATE Cours
SET volume = :volume
WHERE idCours = :idCours');
/* (2) Manage statement error */
if( is_bool($st) )
return false;
/* (3) Execute and dispatch status */
return $st->execute([ ':idCours' => $idCours, ':volume' => $volume ]);
return $st->execute([
"vol" => $volume
]);
} }
/* (5) Deletes an existing cours
*
* @idCours<int> Id of the Cours
*
* @return deleted<bool> Whether it has been deleted
*
---------------------------------------------------------*/
public function delete(int $id) :bool { public function delete(int $id) :bool {
/* (1) Prepare statement */
$st = $this->pdo->prepare("DELETE FROM Cours WHERE idCours = :id"); $st = $this->pdo->prepare("DELETE FROM Cours WHERE idCours = :id");
return $st->execute([ /* (2) Manage statement error */
"id" => $id if( is_bool($st) )
]); return false;
/* (3) Execute and dispatch status */
return $st->execute([ ':id' => $id ]);
} }

View File

@ -1,4 +1,6 @@
{ {
"POST": { "POST": {
"des": "Returns the API documentation", "des": "Returns the API documentation",
"per": [], "per": [],
@ -251,12 +253,52 @@
"cours": { "cours": {
"POST": {
"des" : "Creates a new Cours 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 Cours", "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 Cours", "typ": "id" }
}
},
"GET": { "GET": {
"des" : "Get all cours data about a given UE", "des" : "Get all cours data about a given UE",
"per": [], "per": [],
"par": { "par": {
"URL0": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)", "ren": "code" } "URL0": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)", "ren": "code" }
} }
},
"PUT": {
"des" : "Updates an existing Cours",
"per": [],
"par": {
"URL0": { "des": "Id of the Cours", "typ": "id", "ren": "idCours" },
"idProf": { "des": "Id of the professor (-1 to unset)", "typ": "int", "opt": true },
"volume": { "des": "Number of hours for Cours", "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 Cours",
"per": [],
"par": {
"URL0": { "des": "Id of the Cours", "typ": "id", "ren": "idCours" }
},
"output": {
"deleted" : { "des": "Whether it has been deleted", "typ": "bool" }
}
} }
}, },