GET api.module.admin (added get by id + get all)

This commit is contained in:
xdrm-brackets 2017-12-10 22:33:23 +01:00
parent 13f4aba25d
commit 5cdc2f9945
3 changed files with 37 additions and 9 deletions

View File

@ -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<id> [OPT] UID de l'administrateur
*
* @return data<Array> 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') ];
}
}

View File

@ -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);

View File

@ -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" }
}
}
},