From 5cdc2f9945323b795a01fce8c42b4e6ec511f622 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 10 Dec 2017 22:33:23 +0100 Subject: [PATCH] GET api.module.admin (added get by id + get all) --- build/api/module/admin.php | 30 ++++++++++++++++++++++++++++-- build/database/repo/admin.php | 10 +++++----- config/modules.json | 6 ++++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/build/api/module/admin.php b/build/api/module/admin.php index 66cd973..97d3168 100644 --- a/build/api/module/admin.php +++ b/build/api/module/admin.php @@ -3,15 +3,41 @@ namespace api\module; use \error\core\Error; + use \database\core\Repo; class admin{ - public static function post($args){ + /* (1) Return an admin data + * + * @id_admin [OPT] UID de l'administrateur + * + * @return data Administrateurs correspondants + * + ---------------------------------------------------------*/ + public static function get($args){ extract($args); - return [ 'admin' => 'post' ]; + /* (1) If @id_admin is set -> get by id + ---------------------------------------------------------*/ + if( is_numeric($id_admin) ){ + + /* (1) Search admin by id */ + $fetch_admin = Repo::request('admin', 'getById', $id_admin); + + /* (2) If not found -> return empty data */ + if( !$fetch_admin ) + return [ 'data' => [] ]; + + /* (3) Return fetched admin */ + return [ 'data' => [$fetch_admin] ]; + + + /* (2) Else -> get all + ---------------------------------------------------------*/ + }else + return [ 'data' => Repo::request('admin', 'getAll') ]; } } diff --git a/build/database/repo/admin.php b/build/database/repo/admin.php index 0053474..48e56dc 100644 --- a/build/database/repo/admin.php +++ b/build/database/repo/admin.php @@ -15,7 +15,7 @@ public function getAll(){ /* (1) Statement */ - $st = $this->pdo->query("SELECT * FROM `admin` ORDER BY `username` ASC"); + $st = $this->pdo->query("SELECT `id_admin`, `username`, `mail` FROM `admin` ORDER BY `username` ASC"); /* (2) Fetched data */ return $st->fetchAll(); @@ -34,7 +34,7 @@ public function getById(int $id_admin){ /* (1) Prepare Statement */ - $pst = $this->pdo->prepare("SELECT * FROM `admin` WHERE `id_admin` = :id_admin LIMIT 1"); + $pst = $this->pdo->prepare("SELECT `id_admin`, `username`, `mail` FROM `admin` WHERE `id_admin` = :id_admin LIMIT 1"); /* (2) Bind variables */ $pst->bindParam(':id_admin', $id_admin, \PDO::PARAM_INT); @@ -59,7 +59,7 @@ public function getByMail(String $mail){ /* (1) Prepare Statement */ - $pst = $this->pdo->prepare("SELECT * FROM `admin` WHERE `mail` = :mail LIMIT 1"); + $pst = $this->pdo->prepare("SELECT `id_admin`, `username`, `mail` FROM `admin` WHERE `mail` = :mail LIMIT 1"); /* (2) Bind variables */ $pst->bindParam(':mail', $mail, \PDO::PARAM_STR, 50); @@ -84,7 +84,7 @@ public function getByUsername(String $username){ /* (1) Prepare Statement */ - $pst = $this->pdo->prepare("SELECT * FROM `admin` WHERE `username` = :username LIMIT 1"); + $pst = $this->pdo->prepare("SELECT `id_admin`, `username`, `mail` FROM `admin` WHERE `username` = :username LIMIT 1"); /* (2) Bind variables */ $pst->bindParam(':username', $username, \PDO::PARAM_STR, 20); @@ -109,7 +109,7 @@ public function getByToken(String $token){ /* (1) Prepare Statement */ - $pst = $this->pdo->prepare("SELECT * FROM `admin` WHERE `token` is not NULL AND `token` = :token LIMIT 1"); + $pst = $this->pdo->prepare("SELECT `id_admin`, `username`, `mail` FROM `admin` WHERE `token` is not NULL AND `token` = :token LIMIT 1"); /* (2) Bind variables */ $pst->bindParam(':token', $token, \PDO::PARAM_STR, 128); diff --git a/config/modules.json b/config/modules.json index 3145330..146ef2a 100755 --- a/config/modules.json +++ b/config/modules.json @@ -28,9 +28,11 @@ }, "GET": { - "description": "Deletes an administrator", + "description": "Gets an administrator | Gets all administrators if no id defined", "permissions": [["admin"]], - "parameters": {} + "parameters": { + "URL0": { "description": "The UID of the wanted administrator.", "type": "id", "optional": true, "rename": "id_admin" } + } } },