Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticCoder100 authored Jun 22, 2022
1 parent 5fc4d7b commit 9940f06
Show file tree
Hide file tree
Showing 12 changed files with 963 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
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.
72 changes: 72 additions & 0 deletions api/user/getuser.php
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'
]);
}
28 changes: 28 additions & 0 deletions api/user/getusers.php
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'
]);
}
}

9 changes: 9 additions & 0 deletions class/autoload.php
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";
67 changes: 67 additions & 0 deletions class/dev.php
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);
}
}
87 changes: 87 additions & 0 deletions class/user.php
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);
}
}
10 changes: 10 additions & 0 deletions config/config.php
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;
Loading

1 comment on commit 9940f06

@vercel
Copy link

@vercel vercel bot commented on 9940f06 Oct 21, 2022

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

Please sign in to comment.