From b7b96fd5136acd33d39c7b9cb91d3427fc3a1ec2 Mon Sep 17 00:00:00 2001 From: paulsnz Date: Thu, 14 Jun 2018 20:52:24 +1200 Subject: [PATCH 1/3] PHP JavaScript Developer Test Solution --- README.md | 17 ++++++++++++++ data_import.sh | 37 ++++++++++++++++++++++++++++++ db/mysql.php | 49 ++++++++++++++++++++++++++++++++++++++++ scripts/data_import.sh | 37 ++++++++++++++++++++++++++++++ webservice/load_data.php | 29 ++++++++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 data_import.sh create mode 100644 db/mysql.php create mode 100644 scripts/data_import.sh create mode 100644 webservice/load_data.php diff --git a/README.md b/README.md index 9b8767a..482c9f5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ +## Instructions + +### Importing CSV data +1. Running data_import.sh + +./data_import.sh + +### PHP web service end point +webservice/load_data.php + +### Customer information web page +views/customer.php + + + +### Original Instructions Below + # PHP JavaScript Developer Test A simple test for PHP / JavaScript Developers diff --git a/data_import.sh b/data_import.sh new file mode 100644 index 0000000..20c5df9 --- /dev/null +++ b/data_import.sh @@ -0,0 +1,37 @@ +#!/bin/bash +#data file data/customers.csv + +# environment variables +DB="dev_test" +USER = "dev_test" +PASSWD="badPw0rd" + + +# create database +mysql -e "CREATE DATABASE $DB" +mysql -e "CREATE USER ${USER}@localhost IDENTIFIED BY '${PASSWD}';" +mysql -e "GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'localhost';" +mysql -e "FLUSH PRIVILEGES;" + +# create customer table +mysql -e "CREATE TABLE IF NOT EXISTS `contact_table` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `first_name` varchar(255) NOT NULL, + `last_name` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + `gender` varchar(255) NOT NULL, + `ip_address` varchar(255) NOT NULL, + `company` varchar(255) NOT NULL, + `city` varchar(255) NOT NULL, + `title` varchar(255) NOT NULL, + `website` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;" + +IFS=, +read; # skip header line +while read id first_name last_name email gender ip_address company city title website + do + echo "INSERT INTO contact_table (id,first_name,last_name,email,gender,ip_address,company,city,title,website) VALUES ('$id', '$first_name', '$last_name', '$email', '$gender', '$ip_address', '$company', '$city', '$title', '$website');" + +done < data/customers.csv | mysql -u dev_test -p badPw0rd dev_test; \ No newline at end of file diff --git a/db/mysql.php b/db/mysql.php new file mode 100644 index 0000000..e5fe672 --- /dev/null +++ b/db/mysql.php @@ -0,0 +1,49 @@ +'.mysql_error().'

'); + } + } else { + die('Error:: Unable to establish a connection

'.mysql_error().'

'); + } + } + + } + + public function query($query) { + + if ($result = mysql_query($query, self::$handle)) { + return $result; + } + + // [Nice to have] Adding Error handling, connection exception, SQL injection etc. + + } + +} + +?> \ No newline at end of file diff --git a/scripts/data_import.sh b/scripts/data_import.sh new file mode 100644 index 0000000..20c5df9 --- /dev/null +++ b/scripts/data_import.sh @@ -0,0 +1,37 @@ +#!/bin/bash +#data file data/customers.csv + +# environment variables +DB="dev_test" +USER = "dev_test" +PASSWD="badPw0rd" + + +# create database +mysql -e "CREATE DATABASE $DB" +mysql -e "CREATE USER ${USER}@localhost IDENTIFIED BY '${PASSWD}';" +mysql -e "GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'localhost';" +mysql -e "FLUSH PRIVILEGES;" + +# create customer table +mysql -e "CREATE TABLE IF NOT EXISTS `contact_table` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `first_name` varchar(255) NOT NULL, + `last_name` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + `gender` varchar(255) NOT NULL, + `ip_address` varchar(255) NOT NULL, + `company` varchar(255) NOT NULL, + `city` varchar(255) NOT NULL, + `title` varchar(255) NOT NULL, + `website` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;" + +IFS=, +read; # skip header line +while read id first_name last_name email gender ip_address company city title website + do + echo "INSERT INTO contact_table (id,first_name,last_name,email,gender,ip_address,company,city,title,website) VALUES ('$id', '$first_name', '$last_name', '$email', '$gender', '$ip_address', '$company', '$city', '$title', '$website');" + +done < data/customers.csv | mysql -u dev_test -p badPw0rd dev_test; \ No newline at end of file diff --git a/webservice/load_data.php b/webservice/load_data.php new file mode 100644 index 0000000..5b962c2 --- /dev/null +++ b/webservice/load_data.php @@ -0,0 +1,29 @@ +db->query($request); + + // [Should / nice to have] Adding CSRF, GET / POST handling, webservice exception + + header('Content-type: application/json'); + echo json_encode($result); + + } + +} + +?> \ No newline at end of file From eb39c026b832a5e408ce67895dbfb5801177d4cf Mon Sep 17 00:00:00 2001 From: paulsnz Date: Wed, 20 Jun 2018 11:35:59 +1200 Subject: [PATCH 2/3] Re-comitt missing Customer view --- views/customer.php | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 views/customer.php diff --git a/views/customer.php b/views/customer.php new file mode 100644 index 0000000..011e861 --- /dev/null +++ b/views/customer.php @@ -0,0 +1,108 @@ +title = 'Customer Information'; + $this->content = $this->request(); + + echo $this->content; + + } + + + /** + * Loading the JSON from web serice + **/ + private function request(){ + + // Calling to the web service + $curl_handle = curl_init(); + curl_setopt($curl_handle, CURLOPT_URL, '../webservice/load_data.php'); // [Should have] Loading the base URL + curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); + + // [Should have] check headers and header expection before continue + + $data = json_decode(curl_exec($curl_handle), true); + + // [Should have] check cURL error and http status, and throw execption about the erro + curl_close($curl_handle); + + return $this->getContent($data); + + } + + /** + * Loading Customer data into a simple table without CSS styling. [Should have] using DataTables.js to handle the table view + * @param array $data customer data to load to table + */ + private function getContent($data) { + + $view = '
'.$this->title.'<title></div>'; + + $tmp = ''; + + foreach ($data as $row) { + + $tmp = '<tr>'; + + foreach ($row as $column) { + + $tmp .= '<td>'.$column.'</td>'; + + } + + $tmp .= '</tr>'; + + } + + + // Customer data in a table + $view .= '<div> + <table> + <thead> + <tr> + <th>id</th> + <th>first_name</th> + <th>last_name</th> + <th>email</th> + <th>gender</th> + <th>ip_address</th> + <th>company</th> + <th>city</th> + <th>title</th> + <th>website</th> + </tr> + </thead> + <tbody>' . $tmp . '</tbody> + </table> + </div>'; + + + // Button that asynchronously reqests the customer data + $button = '<div> + <form action="customer.php"> + <input type="submit" value="Refresh Table"> + </form> + </div>'; + + + return $view . $button; + + + } + +} + +?> \ No newline at end of file From 29cf48f9aaed31064d44fa38967e6a69d3b7a1e4 Mon Sep 17 00:00:00 2001 From: paulsnz <paul.shih@nzcer.org.nz> Date: Wed, 20 Jun 2018 11:52:20 +1200 Subject: [PATCH 3/3] Missing commit --- views/customer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/views/customer.php b/views/customer.php index 011e861..5c03d86 100644 --- a/views/customer.php +++ b/views/customer.php @@ -5,6 +5,7 @@ * [Should have] Using DataTables.js to load the data */ +$customer = new Customer; class Customer {