From 35ba517894855fd8e192de164f1d4d69448a365a Mon Sep 17 00:00:00 2001 From: Ngoc Nguyen Date: Tue, 19 May 2020 18:42:17 -0400 Subject: [PATCH] Current APIs --- API/class/Contacts.php | 158 +++++++++++++++++++++++++++++++++++++ API/class/DBConnection.php | 30 +++++++ API/contact/.htaccess | 6 ++ API/contact/create.php | 35 ++++++++ API/contact/delete.php | 27 +++++++ API/contact/read.php | 30 +++++++ API/contact/update.php | 37 +++++++++ API/table | 34 ++++++++ 8 files changed, 357 insertions(+) create mode 100644 API/class/Contacts.php create mode 100644 API/class/DBConnection.php create mode 100644 API/contact/.htaccess create mode 100644 API/contact/create.php create mode 100644 API/contact/delete.php create mode 100644 API/contact/read.php create mode 100644 API/contact/update.php create mode 100644 API/table diff --git a/API/class/Contacts.php b/API/class/Contacts.php new file mode 100644 index 0000000..e61a6a6 --- /dev/null +++ b/API/class/Contacts.php @@ -0,0 +1,158 @@ +_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!"); + } + } +} +?> \ No newline at end of file diff --git a/API/class/DBConnection.php b/API/class/DBConnection.php new file mode 100644 index 0000000..58f460b --- /dev/null +++ b/API/class/DBConnection.php @@ -0,0 +1,30 @@ +_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; + } +} +?> \ No newline at end of file diff --git a/API/contact/.htaccess b/API/contact/.htaccess new file mode 100644 index 0000000..32810a5 --- /dev/null +++ b/API/contact/.htaccess @@ -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] \ No newline at end of file diff --git a/API/contact/create.php b/API/contact/create.php new file mode 100644 index 0000000..ffe2fff --- /dev/null +++ b/API/contact/create.php @@ -0,0 +1,35 @@ +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; +} +?> \ No newline at end of file diff --git a/API/contact/delete.php b/API/contact/delete.php new file mode 100644 index 0000000..955643c --- /dev/null +++ b/API/contact/delete.php @@ -0,0 +1,27 @@ +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; +} +?> \ No newline at end of file diff --git a/API/contact/read.php b/API/contact/read.php new file mode 100644 index 0000000..ccb67ea --- /dev/null +++ b/API/contact/read.php @@ -0,0 +1,30 @@ +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; +} +?> \ No newline at end of file diff --git a/API/contact/update.php b/API/contact/update.php new file mode 100644 index 0000000..abdea9f --- /dev/null +++ b/API/contact/update.php @@ -0,0 +1,37 @@ +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; +} +?> \ No newline at end of file diff --git a/API/table b/API/table new file mode 100644 index 0000000..bb5e84f --- /dev/null +++ b/API/table @@ -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; + + + + + \ No newline at end of file