diff --git a/README.md b/README.md index 47b727f..c09d655 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# phprestapi \ No newline at end of file +This is a practice on REST API in PHP. + +THE SQL used for this project is attached to the file + +The Autoload Consist of all the part to the classes used for the project + +The config consists of Constant Used as well as the database details. \ No newline at end of file diff --git a/api/user/getuser.php b/api/user/getuser.php new file mode 100644 index 0000000..cedc764 --- /dev/null +++ b/api/user/getuser.php @@ -0,0 +1,72 @@ + $_GET['key'] + ]); + if ($_GET['key'] == PUBLIC_KEY) { + if (isset($_GET['email'])) { + $user = new User([ + 'email' => $_GET['email'] + ]); + echo json_encode($user->getUserForPublic()); + } else if (isset($_GET['state'])) { + $user = new User([ + 'state' => $_GET['state'] + ]); + echo json_encode($user->getUserByStateForPublic()); + } else if (isset($_GET['sex'])) { + $user = new User([ + 'sex' => $_GET['sex'] + ]); + echo json_encode($user->getUserBySexForPublic()); + } else { + echo json_encode([ + 'status' => 'error', + 'message' => 'Invalid Request' + ]); + } + } else if ($dev->countKey()['exist'] > 0) { + if (isset($_GET['email'])) { + $user = new User([ + 'email' => $_GET['email'] + ]); + echo json_encode($user->getUser()); + } else if (isset($_GET['state'])) { + $user = new User([ + 'state' => $_GET['state'] + ]); + echo json_encode($user->getUserByState()); + } else if (isset($_GET['sex'])) { + $user = new User([ + 'sex' => $_GET['sex'] + ]); + echo json_encode($user->getUserBySex()); + } else { + echo json_encode([ + 'status' => 'error', + 'message' => 'Invalid Request' + ]); + } + } else { + echo json_encode([ + 'status' => 'error', + 'message' => 'Invalid Key' + ]); + } +} else { + echo json_encode([ + 'error' => 'Inavlid Request' + ]); +} diff --git a/api/user/getusers.php b/api/user/getusers.php new file mode 100644 index 0000000..a6e0ada --- /dev/null +++ b/api/user/getusers.php @@ -0,0 +1,28 @@ + 'Inavlid Request' + ]); +} else { + $dev = new Devs([ + 'apiKey' => $_GET['key'] + ]); + if ($_GET['key'] == PUBLIC_KEY) { + echo json_encode($user->getAllUsersForPublic()); + } else if ($dev->countKey()['exist'] > 0) { + echo json_encode($user->getAllUsers()); + } else { + echo json_encode([ + 'status' => 'error', + 'message' => 'Invalid Key' + ]); + } +} + diff --git a/class/autoload.php b/class/autoload.php new file mode 100644 index 0000000..229368f --- /dev/null +++ b/class/autoload.php @@ -0,0 +1,9 @@ +database = $db; + foreach ($attributes as $key => $item) { + $this->$key = $item; + } + } + + + //geting a key + public function getDev() + { + $query = "SELECT * FROM devs WHERE email = :email"; + $data = [ + 'email' => $this->email + ]; + return $this->database->fetch($query, $data); + } + + //Check If exists + public function countDev() + { + $query = "SELECT count(*) exist FROM devs WHERE email = :email"; + $data = [ + 'email' => $this->email + ]; + return $this->database->fetch($query, $data); + } + + //Check If exists + public function countKey() + { + $query = "SELECT count(*) exist FROM devs WHERE api_key = :apiKey"; + $data = [ + 'apiKey' => $this->apiKey + ]; + return $this->database->fetch($query, $data); + } + + //Upadting a dev + public function upadteDev() + { + $query = "UPDATE devs SET api_key = :apiKey WHERE email = :email"; + $data = [ + 'email' => $this->email, + 'apiKey' => $this->apiKey + ]; + return $this->database->insert($query, $data); + } + + //inserting a dev + public function insertDev() + { + $query = "INSERT INTO devs( api_key, email) VALUES( :apiKey, :email)"; + $data = [ + 'email' => $this->email, + 'apiKey' => $this->apiKey + ]; + return $this->database->insert($query, $data); + } +} diff --git a/class/user.php b/class/user.php new file mode 100644 index 0000000..ef58baa --- /dev/null +++ b/class/user.php @@ -0,0 +1,87 @@ +database = $db; + foreach ($attributes as $key => $item) { + $this->$key = $item; + } + } + + //getting all users + public function getAllUsers() + { + $query = "SELECT * FROM users"; + return $this->database->fetchAll($query); + } + + //geting a user + public function getUser() + { + $query = "SELECT * FROM users WHERE email = :email"; + $data = [ + 'email' => $this->email + ]; + return $this->database->fetch($query, $data); + } + + //geting users by state + public function getUserByState() + { + $query = "SELECT * FROM users WHERE state = :state"; + $data = [ + 'state' => $this->state + ]; + return $this->database->fetchAll($query, $data); + } + //geting a user + public function getUserBySex() + { + $query = "SELECT * FROM users WHERE sex = :sex"; + $data = [ + 'sex' => $this->sex + ]; + return $this->database->fetchAll($query, $data); + } + + //getting all users for public key + public function getAllUsersForPublic() + { + $query = "SELECT firstname, lastname FROM users"; + return $this->database->fetchAll($query); + } + + //geting a user + public function getUserForPublic() + { + $query = "SELECT firstname, lastname FROM users WHERE email = :email"; + $data = [ + 'email' => $this->email + ]; + return $this->database->fetch($query, $data); + } + + //geting users by state + public function getUserByStateForPublic() + { + $query = "SELECT firstname, lastname FROM users WHERE state = :state"; + $data = [ + 'state' => $this->state + ]; + return $this->database->fetchAll($query, $data); + } + //geting a user + public function getUserBySexForPublic() + { + $query = "SELECT firstname, lastname FROM users WHERE sex = :sex"; + $data = [ + 'sex' => $this->sex + ]; + return $this->database->fetchAll($query, $data); + } +} diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..a0d140b --- /dev/null +++ b/config/config.php @@ -0,0 +1,10 @@ +conn = $this->connect(); + } + + /** + * connect + * + * This is the method used in making connection to the database + * + */ + private function connect() + { + try { + // instantiating the PDO class for database connection + $conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS); + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + // echo "connected"; + return $conn; + } catch (PDOException $e) { + echo "Error When Connecting to database " . $e->getMessage(); + } + } + + + /** + * insert + * + * @param mixed $query + * @param $data = [] + * + */ + public function insert($query, $data = []) + { + + try { + $stmt = $this->conn->prepare($query); + $array = []; + foreach ($data as $key => $item) { + $array[':' . $key] = $item; + } + $stmt->execute($array); + return true; + } catch (PDOException $e) { + echo "Error When Fetching " . $e->getMessage(); + } + } + + + /** + * fetch + * + * @param mixed $query + * @param $data = [] + * + */ + public function fetch($query, $data = []) + { + try { + $stmt = $this->conn->prepare($query); + $array = []; + foreach ($data as $key => $item) { + $array[':' . $key] = $item; + } + $stmt->execute($array); + $result = $stmt->fetch(PDO::FETCH_ASSOC); + return $result; + } catch (PDOException $e) { + echo "Error When Fetching " . $e->getMessage(); + } + } + + + /** + * Method fetchAll + * + * @param $query $query [explicite description] + * @param $data $data [explicite description] + * + */ + public function fetchAll($query, $data = []) + { + try { + $stmt = $this->conn->prepare($query); + $array = []; + foreach ($data as $key => $item) { + $array[':' . $key] = $item; + } + $stmt->execute($array); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + return $result; + } catch (PDOException $e) { + echo "Error When Fetching All " . $e->getMessage(); + } + } + + /** + * count + * + * @param mixed $query + * @param $data = [] + * + */ + public function countItem($query, $data = []) + { + try { + $stmt = $this->conn->prepare($query); + $array = []; + foreach ($data as $key => $item) { + $array[':' . $key] = $item; + } + $stmt->execute($array); + $result = $stmt->fetch(PDO::FETCH_COLUMN); + return $result; + } catch (PDOException $e) { + echo "Error When Counting " . $e->getMessage(); + } + } + + /** + * Method __destruct + * + */ + public function __destruct() + { + $this->conn = ""; + } +} + +//instantiating the database class +$db = new Database(); diff --git a/index.php b/index.php new file mode 100644 index 0000000..7ee7e20 --- /dev/null +++ b/index.php @@ -0,0 +1,232 @@ + + + + + + + + PHP REST API + + + + + +
+ +
+ + +
+ + +
+
+

Your Public Key to test The Api

+

1234567

+
+
+ +
+ +
+ +
+
+
+
+
+

Generate Your Api Key

+
+ + +
+
+
+
+
+

Get Your Api Key

+
+ + +
+
+
+
+ + +
+ +
+

API DOCS

+
+
+ +

Basic

+ +
+
+

+ What the api does is to give you a list of user base on what you want, you can get all users, you can get a particular user and you can get a user based on the following: +

    +
  1. State
  2. +
  3. Sex
  4. +
+
+ You just need to get either the public or the private key to start using the api +
Nb: The Public Key won't give you the full list of the users. +

+ +
+
+
+ +

+ + The email, state, sex will be the email, state, and sex of the user you want. + +
+ e.g
+ + if the email of the user is someone@gmail.com or the state is Oyo or the sex is M +
+
+ + http://localhost:8080/api/user/getUser.php?key=12345&email=someone@gmail.com +
+
+ http://localhost:8080/api/user/getUser.php?key=12345&state=Oyo +
+
+ http://localhost:8080/api/user/getUser.php?key=12345&sex=M + +
+
+
+
+ + Generating Your own Api key (PRIVATE)
+ Input Your Email and click on generate, +
+
+ + If you want to get the key when next you want to use the api, + you just input your email at the get api key form and hit the get button it would provide you with your key. +

+
+
+
+
+ +

Getting ALL USERS

+ +
+
+

+ Since This Project hasn't been hosted yet, assuming the SERVER ADDRESS is localhost:8080: +
+
+
+ To get All users using the public key: 12345 + use the address: http://localhost:8080/api/user/getUsers.php?key=12345 +
+
+
+ To get All users using the private key + use the address: http://localhost:8080/api/user/getUsers.php?key=Your_Private_key +

+
+
+
+
+ +

Getting a USER

+ +
+
+

+ Since This Project hasn't been hosted yet, assuming the SERVER ADDRESS is localhost:8080: +
+
+
+ To get All users using the public key: 12345 + use the address:
+ http://localhost:8080/api/user/getUser.php?key=12345&email=email +
+
+
+ To get All users using the private key + use the address:
+ http://localhost:8080/api/user/getUser.php?key=Your_Private_key&email=email +

+
+
+
+
+ +

Getting USERS By State

+ +
+
+

+ Since This Project hasn't been hosted yet, assuming the SERVER ADDRESS is localhost:8080: +
+
+
+ To get All users using the public key: 12345 + use the address:
+ http://localhost:8080/api/user/getUser.php?key=12345&state=state +
+
+
+ To get All users using the private key + use the address:
+ http://localhost:8080/api/user/getUser.php?key=Your_Private_key&state=state +

+
+
+
+
+ +

Getting USERS By sex

+ +
+
+

+ Since This Project hasn't been hosted yet, assuming the SERVER ADDRESS is localhost:8080: +
+
+ The sex is M for Male and F for female +
+
+ To get All users using the public key: 12345 + use the address:
+ http://localhost:8080/api/user/getUser.php?key=12345&sex=sex +
+
+
+ To get All users using the private key + use the address:
+ http://localhost:8080/api/user/getUser.php?key=Your_Private_key&sex=sex +

+
+
+
+
+ + +
+ + + + + \ No newline at end of file diff --git a/request.php b/request.php new file mode 100644 index 0000000..27fa386 --- /dev/null +++ b/request.php @@ -0,0 +1,70 @@ + 'error', + 'message' => 'Opps Field Empty' + ]); + } else if (isset($_POST["generate"])) { + $data = [ + 'email' => $_POST['email'], + 'apiKey' => generateToken() + ]; + + $dev = new Devs($data); + if ($dev->countDev()['exist'] == 0) { + $register = $dev->insertDev(); + if ($register) { + $get = $dev->getDev(); + echo json_encode([ + 'status' => 'success', + 'message' => $get['api_key'] + ]); + } + } else if ($dev->countDev()['exist'] > 0) { + $register = $dev->upadteDev(); + if ($register) { + $get = $dev->getDev(); + echo json_encode([ + 'status' => 'success', + 'message' => $get['api_key'] + ]); + } + } + } else if (isset($_POST["get"])) { + $data = [ + 'email' => $_POST['email'] + ]; + $dev = new Devs($data); + if ($dev->countDev()['exist'] == 0) { + echo json_encode([ + 'status' => 'error', + 'message' => "Opps Sorry Invalid Email" + ]); + } else if ($dev->countDev()['exist'] > 0) { + $data = [ + 'email' => $_POST['email'] + ]; + $dev = new Devs($data); + $get = $dev->getDev(); + echo json_encode([ + 'status' => 'success', + 'message' => $get['api_key'] + ]); + } + } +} diff --git a/script.js b/script.js new file mode 100644 index 0000000..3bfef92 --- /dev/null +++ b/script.js @@ -0,0 +1,43 @@ +let generate = document.querySelector("#generate_key"); +let get = document.querySelector("#get_key"); +let feedback = document.querySelector("#response"); + +function loadDoc(data) { + feedback.style.height = 0; + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function () { + if (this.readyState == 4 && this.status == 200) { + handleRequest(this.responseText); + } + }; + xhttp.open("POST", "request.php", true); + xhttp.send(data); +} + +function handleRequest(response) { + feedback.style.height = "50px"; + response = JSON.parse(response); + switch (response.status) { + case "success": + feedback.style.background = "#44c559"; + feedback.innerHTML = `YOUR API KEY: ${response.message}`; + break; + case "error": + feedback.style.background = " #ec2e2e"; + feedback.innerHTML = response.message; + break; + } +} + +generate.addEventListener("submit", function (e) { + e.preventDefault(); + let data = new FormData(this); + data.append("generate", true); + loadDoc(data); +}); +get.addEventListener("submit", function (e) { + e.preventDefault(); + let data = new FormData(this); + data.append("get", true); + loadDoc(data); +}); diff --git a/style.css b/style.css new file mode 100644 index 0000000..99bc056 --- /dev/null +++ b/style.css @@ -0,0 +1,194 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; + text-decoration: none; + list-style: none; +} + +.wrapper { + position: fixed; + height: 100%; + width: 100%; + overflow-y: auto; + padding-bottom: 40px; +} + +header { + width: 100%; + height: 10%; + display: flex; + padding: 20px; + box-shadow: 0 0 4px 2px #d4d4d4; + align-items: center; + position: fixed; + background: #fff; +} + +header div { + width: 50%; + font-size: 25px; + cursor: pointer; + text-shadow: 4px 2px #d4d4d4; +} + +header nav { + width: 50%; + display: flex; + justify-content: end; +} + +header nav ul { + display: flex; + align-items: center; +} + +header nav ul li { + margin-right: 20px; +} + +header nav ul li a { + font-size: 20px; + color: #4a0909; +} + +header nav ul li a:hover { + font-size: 20px; + font-weight: bolder; +} + +.container { + width: 80%; + margin: auto; +} + +#public { + margin-top: 100px; + height: 40%; + background: linear-gradient(to left, #e53838, #540b0b); + color: #fff; + display: flex; + align-items: center; + justify-content: center; +} + +#public h3 { + text-align: center; + font-size: 30px; + cursor: pointer; +} + +#public h2 { + text-align: center; + font-size: 40px; + cursor: pointer; +} + +#private { + min-height: 4px; + margin-top: 15px; +} + +#private .container { + display: flex; +} + +#private .container>div { + width: 50%; + display: inline flex; + justify-content: center; + align-items: center; + margin-left: 10px; + box-shadow: 0 0 8px 4px #d4d4d4; +} + +form { + width: 100%; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + padding: 20px 0; + padding-bottom: 40px; +} + +form>div:first-child { + margin-bottom: 20px; + text-align: center; +} + +input, button { + width: 80%; + padding: 10px 4px; + outline: none; + font-size: 20px; +} + +button { + margin-top: 30px; + cursor: pointer; + background: #055252; + border: none; + color: #fff; +} + +button:hover { + background: #33a3a3; +} + +#response { + width: 50%; + height: 0; + display: flex; + justify-content: center; + align-items: center; + margin: auto; + margin-top: 10px; + color: white; + text-align: center; + transition: all 2s ease-in; +} + +#doc { + width: 100%; + height: 230px; + margin-top: 30px; +} + +#doc .container { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; +} +#doc .container > h1 { + width: 100%; + padding: 50px; + font-size: 30px; + text-align: center; +} + +#doc .container>div { + display: flex; + width: 100%; + margin-bottom: 30px; + box-shadow: 0 0 8px 4px grey; + padding: 30px; +} +#doc .container>div >div { + width: 50%; + align-items: center; + justify-content: center; + margin-bottom: 10px; +} + +#doc .container>div >div:first-child { + display: flex; + align-items: center; + justify-content: center; + text-transform: uppercase; +} +#doc .container>div >div:last-child { + text-transform: capitalize; + word-wrap: break-word; +} \ No newline at end of file