-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fc4d7b
commit 9940f06
Showing
12 changed files
with
963 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# phprestapi | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
header('Access-Control-Allow-Origin: *'); | ||
header('Content-Type: application/json'); | ||
|
||
require_once __DIR__ . '/../../class/autoload.php'; | ||
|
||
|
||
if ( | ||
(isset($_GET['email']) || | ||
isset($_GET['state']) || | ||
isset($_GET['sex']) | ||
) && sizeof($_GET) == 2 && isset($_GET['key']) | ||
) { | ||
|
||
$dev = new Devs([ | ||
'apiKey' => $_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' | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
header('Access-Control-Allow-Origin: *'); | ||
header('Content-Type: application/json'); | ||
|
||
require_once __DIR__ . '/../../class/autoload.php'; | ||
|
||
$user = new User(); | ||
|
||
if (!isset($_GET['key'])) { | ||
echo json_encode([ | ||
'error' => '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' | ||
]); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
// Requiring The Config File | ||
require_once __DIR__ . "/../config/config.php"; | ||
|
||
// Requiring The Database Class | ||
require_once ROOT . "/config/db.php"; | ||
require_once ROOT . "/class/user.php"; | ||
require_once ROOT . "/class/dev.php"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
class Devs | ||
{ | ||
private $database; | ||
|
||
public function __construct($attributes = []) | ||
{ | ||
global $db; | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
class User | ||
{ | ||
private $table = 'users'; | ||
private $database; | ||
|
||
public function __construct($attributes = []) | ||
{ | ||
global $db; | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
const DB_NAME = 'phprestapi'; | ||
const DB_USER = 'root'; | ||
const DB_PASS = 'sijuade'; | ||
const DB_HOST = 'localhost'; | ||
|
||
// defining the root part | ||
const ROOT = __DIR__ . '/..'; | ||
|
||
const PUBLIC_KEY = 1234567; |
Oops, something went wrong.
9940f06
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
phprestapi – ./
phprestapi-git-main-abdulazeem20.vercel.app
phprestapi.vercel.app
phprestapi-abdulazeem20.vercel.app