From 79bdb1c33c5bc9f5379c3eec4369cd50fd1e2847 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 9 May 2018 17:44:40 +0200 Subject: [PATCH] Implemented Department creation --- build/api/module/departmentController.php | 51 +++++ build/database/core/Repo.php | 2 +- build/database/repo/database.php | 266 ++++++++++++++++++++++ build/database/repo/meta.php | 8 + config/modules.json | 8 + 5 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 build/database/repo/database.php diff --git a/build/api/module/departmentController.php b/build/api/module/departmentController.php index 12f9d08..9989428 100644 --- a/build/api/module/departmentController.php +++ b/build/api/module/departmentController.php @@ -10,7 +10,10 @@ namespace api\module; use database\core\Repo; +use database\repo\database; +use database\repo\department; use database\repo\meta; +use database\repo\professor; class departmentController { @@ -110,4 +113,52 @@ class departmentController } + + public function post($args){ + $name = null; + extract($args); + + /** @var meta $metaRepo */ + $metaRepo = Repo::getRepo("meta"); + /** @var professor $profRep */ + $profRep = Repo::getRepo("professor"); + /** @var database $dbRepo */ + $dbRepo = Repo::getRepo("database"); + + //create the department in the meta database + $depId = $metaRepo->createDepartment($name); + + //link the current user to that department + $metaRepo->link($_SESSION['CAS']['login'],$depId); + + //create the database and init the structure + $dbName = $dbRepo->init($depId); + + //link the new database to the department + $metaRepo->createVersion("Version 1",$dbName,$depId,true); + + //get the current user data from current department + $user = $profRep->get($_SESSION['CAS']['id'])[0]; + + //switch our connexion to that database + Repo::switchDatabase($dbName); + + //create the default admin in the new database (categories are common to all department) + $profRep->create($user["lastName"], + $user["firstName"], + $user["Categorie_idCategorie"], + $user["hoursToDo"], + $user["abreviation"], + true, + $user["casLogin"]); + + //update user session + $departments = $metaRepo->get_prof_departments($user["casLogin"]); + $_SESSION['AvailableDepartments'] = $departments; + + //we are good now + return ["success" => true]; + + } + } \ No newline at end of file diff --git a/build/database/core/Repo.php b/build/database/core/Repo.php index 3d2568e..287a5ee 100644 --- a/build/database/core/Repo.php +++ b/build/database/core/Repo.php @@ -78,7 +78,7 @@ } public static function switchDatabase(string $dbName){ - return static::$driver->pdo()->prepare("USE $dbName")->execute(); + return static::$driver->pdo()->exec("USE $dbName;SET autocommit=1;"); } public static function getDBConfig() : array{ diff --git a/build/database/repo/database.php b/build/database/repo/database.php new file mode 100644 index 0000000..4dc944a --- /dev/null +++ b/build/database/repo/database.php @@ -0,0 +1,266 @@ +createVersionDatabase($depId,$SQL); + + } + +} \ No newline at end of file diff --git a/build/database/repo/meta.php b/build/database/repo/meta.php index 87fe610..5fdb278 100644 --- a/build/database/repo/meta.php +++ b/build/database/repo/meta.php @@ -326,4 +326,12 @@ class meta extends Repo_i { return $st->execute($execute); } + + public function createDepartment(String $name) : int{ + $st = $this->pdo->prepare("INSERT INTO meta_vhost.departement(label) VALUE (:label)"); + + $st->execute(["label" => $name]); + + return $this->pdo->lastInsertId(); + } } \ No newline at end of file diff --git a/config/modules.json b/config/modules.json index 8f46e51..2538077 100644 --- a/config/modules.json +++ b/config/modules.json @@ -82,6 +82,14 @@ } }, + "POST": { + "des": "Create a new Department", + "per": [["cas_user"]], + "par": { + "name": { "des": "Name of the department", "typ": "text"} + } + }, + "PUT":{ "des": "Switch the user on another department database", "per": [["cas_user"]],