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 @@ + + + +
+ + + ++ 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: +
+
+ 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.
+
+ 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
+
+ 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
+
+ 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
+
+ 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
+