Skip to content

Commit

Permalink
Merge pull request COP4331Group10#1 from COP4331Group10/api
Browse files Browse the repository at this point in the history
api
  • Loading branch information
nnguyen83 authored May 19, 2020
2 parents 1fb314c + 65f437b commit 681abe8
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 0 deletions.
158 changes: 158 additions & 0 deletions API/class/Contacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/**
* @package Contact class
*
* @author Ngoc Nguyen
*
*/

include("DBConnection.php");
class Contacts
{
protected $db;
private $_id;
private $_firstName;
private $_lastName;
private $_userID;
private $_phoneNumber;
private $_address;
private $_email;
private $_dateAdded;
private $_dateUpdated;
private $_additionalNotes;

public function setId($id) {
$this->_id = $id;
}
public function setUserID($userID) {
$this->_userID = $userID;
}
public function setContactID($contactID) {
$this->_id = $contactID;
}
public function setFirstName($firstName) {
$this->_firstName = $firstName;
}
public function setLastName($lastName) {
$this->_lastName = $lastName;
}
public function setPhoneNumber($phoneNumber) {
$this->_phoneNumber = $phoneNumber;
}
public function setAddress($address) {
$this->_address = $address;
}
public function setEmail($email) {
$this->_email = $email;
}
public function setAdditionalNotes($additionalNotes) {
$this->_additionalNotes = $additionalNotes;
}
public function setDateUpdated(){
$this->dateUpdated = 'CURRENT_TIMESTAMP()';
}

public function __construct() {
$this->db = new DBConnection();
$this->db = $this->db->returnConnection();
}



// create contact
public function createContact() {
try {
$sql = "INSERT INTO Contacts (firstName, lastName, phoneNumber, email, address, additionalNotes, userID)
VALUES (:firstName, :lastName, :phoneNumber, :email, :address, :additionalNotes, :userID)";
$data = [
'firstName' => $this->_firstName,
'lastName' => $this->_lastName,
'phoneNumber' => $this->_phoneNumber,
'email' => $this->_email,
'address' => $this->_address,
'additionalNotes' => $this->_additionalNotes,
'userID' => $this->_userID
];
$stmt = $this->db->prepare($sql);
$stmt->execute($data);
$status = $stmt->rowCount();
return $status;

} catch (Exception $e) {
die("There's an error in the query!");
}
}

// update contact
public function updateContact() {
try {
$sql = "UPDATE Contacts SET firstName=:firstName,
lastName=:lastName,
phoneNumber=:phoneNumber,
email=:email,
address=:address,
additionalNotes=:additionalNotes
WHERE id=:contact_id";
$data = [
'firstName' => $this->_firstName,
'lastName' => $this->_lastName,
'phoneNumber' => $this->_phoneNumber,
'email' => $this->_email,
'address' => $this->_address,
'additionalNotes' => $this->_additionalNotes,
'contact_id' => $this->_id
];
$stmt = $this->db->prepare($sql);
$stmt->execute($data);
$status = $stmt->rowCount();
return $status;
} catch (Exception $e) {
die("There's an error in the query!");
}
}

// get contact
public function getContact() {
try {
$sql = "SELECT * FROM Contacts WHERE id=:contact_id";
$stmt = $this->db->prepare($sql);
$data = ['contact_id' => $this->_id];
$stmt->execute($data);
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
return $result;
} catch (Exception $e) {
die("There's an error in the query!");
}
}

// getAll contacts
public function getAllStudent() {
try {
$sql = "SELECT * FROM Contacts";
$stmt = $this->db->prepare($sql);

$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $result;
} catch (Exception $e) {
die("There's an error in the query!");
}
}

// delete contact
public function deleteContact() {
try {
$sql = "DELETE FROM Contacts WHERE id=:contact_id";
$stmt = $this->db->prepare($sql);
$data = [
'contact_id' => $this->_id
];
$stmt->execute($data);
$status = $stmt->rowCount();
return $status;
} catch (Exception $e) {
die("There's an error in the query!");
}
}
}
?>
30 changes: 30 additions & 0 deletions API/class/DBConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @package PHP Rest API(DBConnection)
*
* @author Ngoc Nguyen
*
*/

// Database Connection
class DBConnection {
private $_dbHostname = "localhost";
private $_dbName = "minlubon";
private $_dbUsername = "luadmin";
private $_dbPassword = "luadmin";
private $_con;

public function __construct() {
try {
$this->_con = new PDO("mysql:host=$this->_dbHostname;dbname=$this->_dbName", $this->_dbUsername, $this->_dbPassword);
$this->_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
// return Connection
public function returnConnection() {
return $this->_con;
}
}
?>
6 changes: 6 additions & 0 deletions API/contact/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RewriteEngine On
# Turn on the rewriting engine
RewriteRule ^read/([0-9a-zA-Z_-]*)$ read.php?id=$1 [NC,L]
RewriteRule ^delete/([0-9]*)$ delete.php?id=$1 [NC,L]
RewriteRule ^create create.php [NC,L]
RewriteRule ^update update.php [NC,L]
35 changes: 35 additions & 0 deletions API/contact/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");

$requestMethod = $_SERVER["REQUEST_METHOD"];
include('../class/Contacts.php');
$contact = new Contacts();

// get posted data
$data = json_decode(file_get_contents("php://input"));

switch($requestMethod) {
case 'POST':
$contact->setUserID($data->userId);
$contact->setFirstName($data->firstName);
$contact->setLastName($data->lastName);
$contact->setPhoneNumber($data->phoneNumber);
$contact->setEmail($data->email);
$contact->setAddress($data->address);
$contact->setAdditionalNotes($data->additionalNotes);
$contactInfo = $contact->createContact();

if(!empty($contactInfo)) {
header("HTTP/1.0 200 OK");
} else {
header("HTTP/1.0 409 Conflict");
}
header('Content-Type: application/json');
echo $js_encode;
break;
default:
header("HTTP/1.0 405 Method Not Allowed");
break;
}
?>
27 changes: 27 additions & 0 deletions API/contact/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$requestMethod = $_SERVER["REQUEST_METHOD"];
include('../class/Contacts.php');
$contact = new Contacts();
switch($requestMethod) {
case 'DELETE':
$empId = '';
if($_GET['id']) {
$contactID = $_GET['id'];
$contact->setContactID($contactID);
}
$contactInfo = $contact->deleteContact();
if(!empty($contactInfo)) {
// $js_encode = json_encode(array('status'=>TRUE, 'message'=>'Contact deleted Successfully.'), true);
header("HTTP/1.1 200 OK");
} else {
// $js_encode = json_encode(array('status'=>FALSE, 'message'=>'Contact delete failed.'), true);
header("HTTP/1.1 404 Not Found");
}
header('Content-Type: application/json');
echo $js_encode;
break;
default:
header("HTTP/1.0 405 Method Not Allowed");
break;
}
?>
30 changes: 30 additions & 0 deletions API/contact/read.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
header("Access-Control-Allow-Methods: GET");

$requestMethod = $_SERVER["REQUEST_METHOD"];
include('../class/Contacts.php');
$contact = new Contacts();
switch($requestMethod) {
case 'GET':
$contactID = '';

if($_GET['id']) {
$contactID = $_GET['id'];
$contact->setContactID($contactID);
$contactInfo = $contact->getContact();
} else {
$contactInfo = $contact->getAllContact();
}
if(!empty($contactInfo)) {
$js_encode = json_encode(array('status'=>TRUE, 'contactInfo'=>$contactInfo), true);
} else {
header("HTTP/1.1 404 Not Found");
}
header('Content-Type: application/json');
echo $js_encode;
break;
default:
header("HTTP/1.0 405 Method Not Allowed");
break;
}
?>
37 changes: 37 additions & 0 deletions API/contact/update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
$requestMethod = $_SERVER["REQUEST_METHOD"];

include('../class/Contacts.php');

$contact = new Contacts();

// get put data
$data = json_decode(file_get_contents("php://input"));

switch($requestMethod) {
case 'PUT':
$contact->setId($data->id);
$contact->setFirstName($data->firstName);
$contact->setLastName($data->lastName);
$contact->setPhoneNumber($data->phoneNumber);
$contact->setEmail($data->email);
$contact->setAddress($data->address);
$contact->setAdditionalNotes($data->additionalNotes);
$contact->setDateUpdated();


$contactInfo = $contact->updateContact();

if(!empty($contactInfo)) {
header("HTTP/1.0 200 OK");
} else {
header("HTTP/1.1 404 Not Found");
}
header('Content-Type: application/json');
echo $js_encode;
break;
default:
header("HTTP/1.0 405 Method Not Allowed");
break;
}
?>
34 changes: 34 additions & 0 deletions API/table
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


CREATE TABLE `Users` ( `ID` INT NOT NULL AUTO_INCREMENT ,
`DateCreated` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`DateLastLoggedIn` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`FirstName` VARCHAR(50) NOT NULL DEFAULT '' ,
`LastName` VARCHAR(50) NOT NULL DEFAULT '' ,
`Login` VARCHAR(50) NOT NULL DEFAULT '' ,
`Password` VARCHAR(50) NOT NULL DEFAULT '' ,
PRIMARY KEY (`ID`)) ENGINE = InnoDB;


ALTER TABLE Contacts ADD CONSTRAINT fk_user_id FOREIGN KEY (UserID) REFERENCES Users(ID);

CREATE TABLE `Contacts` ( `id` INT NOT NULL AUTO_INCREMENT ,
`userID` INT NOT NULL DEFAULT '0' ,
`firstName` VARCHAR(50) NOT NULL DEFAULT '' ,
`lastName` VARCHAR(50) NOT NULL DEFAULT '' ,
`phoneNumber` VARCHAR(50) DEFAULT '' ,
`address` VARCHAR(128) DEFAULT ' ',
`email` VARCHAR(50) NOT NULL DEFAULT '' ,
`dateAdded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`dateUpdated` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`additionalNotes` TEXT,
PRIMARY KEY (`id`),

INDEX (userID),
FOREIGN KEY (userID) REFERENCES Users(ID) ON DELETE CASCADE
) ENGINE = InnoDB;





0 comments on commit 681abe8

Please sign in to comment.