From 48555669eecfa94c827d9df56f2db4eab8323c57 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 17 Feb 2018 19:02:00 +0100 Subject: [PATCH] fix: +api.response (cannot change header.. see nginx) + add: example modules [admin, root] --- build/api/core/Response.php | 2 +- build/api/module/admin.php | 133 ++++++++++++++++++++++++++++++++++++ build/api/module/root.php | 20 ++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 build/api/module/admin.php create mode 100644 build/api/module/root.php diff --git a/build/api/core/Response.php b/build/api/core/Response.php index 5cd3e82..f579a6d 100755 --- a/build/api/core/Response.php +++ b/build/api/core/Response.php @@ -114,7 +114,7 @@ $this->error->setHttpCode(); // Type de contenu - header('Content-Type: application/json; charset=utf-8'); + // header('Content-Type: application/json; charset=utf-8'); // On rajoute l'erreur au message $returnData = array_merge([ diff --git a/build/api/module/admin.php b/build/api/module/admin.php new file mode 100644 index 0000000..09265b0 --- /dev/null +++ b/build/api/module/admin.php @@ -0,0 +1,133 @@ + [OPT] UID de l'administrateur + * + * @return data Administrateurs correspondants + * + ---------------------------------------------------------*/ + public static function get($args){ + extract($args); + + /* (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') ]; + + } + + + + /* (2) Creates a new administrator + * + * @username Identifiant de l'administrateur + * @mail Adresse mail de l'administrateur + * @password Mot de passe de l'administrateur + * + * @return admin Données de l'administrateur crée + * + ---------------------------------------------------------*/ + public static function post($args){ + extract($args); + + /* (1) Création admin */ + $id_created = Repo::request('admin', 'create', $username, $mail, $password); + + /* (2) Gestion erreur */ + if( $id_created === false ) + return [ 'error' => new Error(Err::RepoError) ]; + + /* (3) Renvoi @id_admin */ + return [ 'id_admin' => Repo::request('admin', 'getById', $id_created) ]; + + } + + + + /* (3) Updates an existing administrator + * + * @id_admin UID de l'administrateur + * @mail [OPT] Adresse mail de l'administrateur + * @password [OPT] Mot de passe de l'administrateur + * + * @return admin The new administrator data + * + ---------------------------------------------------------*/ + public static function put($args){ + extract($args); + + /* (1) If @mail given + ---------------------------------------------------------*/ + if( !is_null($mail) ){ + + /* (1) Update mail address */ + $updated = Repo::request('admin', 'setMail', $id_admin, $mail); + + /* (2) Gestion erreur */ + if( $updated === false ) + return [ 'error' => new Error(Err::RepoError) ]; + + } + + /* (2) If @password given + ---------------------------------------------------------*/ + if( !is_null($password) ){ + + /* (1) Update password */ + $updated = Repo::request('admin', 'setPassword', $id_admin, $password); + + /* (2) Gestion erreur */ + if( $updated === false ) + return [ 'error' => new Error(Err::RepoError) ]; + + } + + /* (3) Renvoi @id_admin */ + return [ 'id_admin' => Repo::request('admin', 'getById', $id_admin) ]; + + } + + + + /* (4) Deletes an existing administrator + * + * @id_admin UID de l'administrateur + * + * @return removed Whether the admin has been removed + * + ---------------------------------------------------------*/ + public static function delete($args){ + extract($args); + + /* (1) Dispatch du status */ + return [ 'removed' => Repo::request('admin', 'delete', $id_admin) ]; + + } + + } diff --git a/build/api/module/root.php b/build/api/module/root.php new file mode 100644 index 0000000..4534cb4 --- /dev/null +++ b/build/api/module/root.php @@ -0,0 +1,20 @@ + $args ]; + + } + + }