diff --git a/CSVImporter.php b/CSVImporter.php new file mode 100644 index 0000000..18ef17f --- /dev/null +++ b/CSVImporter.php @@ -0,0 +1,33 @@ +isTableExists($tableName)) { + mysql_query( ' + LOAD DATA LOCAL INFILE "'.$file.'" + INTO TABLE `' . $tableName . '` + FIELDS TERMINATED by \',\' + LINES TERMINATED BY \'\n\' + IGNORE 1 LINES + ', $this->conn)or die(mysql_error()); + } else { + die("Internal Error in imporing CSV...!!"); + } + } +} +?> \ No newline at end of file diff --git a/CustomerService/CustomerImportService.php b/CustomerService/CustomerImportService.php new file mode 100644 index 0000000..ffd9159 --- /dev/null +++ b/CustomerService/CustomerImportService.php @@ -0,0 +1,6 @@ +importCSV(); +echo 'success'; diff --git a/CustomerService/CustomerService.php b/CustomerService/CustomerService.php new file mode 100644 index 0000000..4bba8e6 --- /dev/null +++ b/CustomerService/CustomerService.php @@ -0,0 +1,5 @@ +getCustomers(); +?> \ No newline at end of file diff --git a/CustomerService/CustomerServiceHandler.php b/CustomerService/CustomerServiceHandler.php new file mode 100644 index 0000000..152bafe --- /dev/null +++ b/CustomerService/CustomerServiceHandler.php @@ -0,0 +1,30 @@ +model = new CustomerModel(); + $this->model->select('customer'); + $rawData = $this->model->result; + + if(empty($rawData)) { + $statusCode = 404; + $rawData = array('error' => 'No results found!'); + } else { + $statusCode = 200; + } + + $requestContentType = $_SERVER['HTTP_ACCEPT']; + $this ->setHttpHeaders($requestContentType, $statusCode); + + $response = json_encode($rawData); + echo $response; + } +} +?> \ No newline at end of file diff --git a/CustomerService/RESTService.php b/CustomerService/RESTService.php new file mode 100644 index 0000000..8ea005e --- /dev/null +++ b/CustomerService/RESTService.php @@ -0,0 +1,27 @@ + getHttpStatusMessage($statusCode); + + header($this->httpVersion. " ". $statusCode ." ". $statusMessage); + header("Content-Type:". $contentType); + } + + public function getHttpStatusMessage($statusCode){ + $httpStatus = array( + 200 => 'OK', + 204 => 'No Content', + 404 => 'Not Found', + 500 => 'Internal Server Error'); + return ($httpStatus[$statusCode]) ? $httpStatus[$statusCode] : $status[500]; + } +} +?> \ No newline at end of file diff --git a/README.md b/README.md index 9b8767a..9dfb366 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,15 @@ A simple test for PHP / JavaScript Developers +## Instruction +1. The project files should be inside catchnz-test directory +2. Navigate to http://domain-name/catchnz_test/index.php after uploading to server +3. Enter database host, username, password and database name in the form when asked +4. Once the installation is finished, it will be redirected to the home page +5. click on "Import CSV Data" button to import data from CSV file to database +6. Click on "Show Customers" button to load imported data from database +7. I used MySQL DB + ## Instructions 1. Fork or clone this repo diff --git a/config.json b/config.json new file mode 100644 index 0000000..9008ca2 --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +{"isInstalled":false,"db_host":"","db_username":"","db_password":"","db_name":""} \ No newline at end of file diff --git a/controller/CustomerController.php b/controller/CustomerController.php new file mode 100644 index 0000000..8526474 --- /dev/null +++ b/controller/CustomerController.php @@ -0,0 +1,33 @@ +cModel = new CustomerModel(); + $this->file = $_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/data/customers.csv'; + } + + public function render(){ + include ($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/view/CustomerView.php'); + } + + public function getCustomers() { + $cList = $this->cModel->getAllCustomers(); + return cList; + } + + public function importCSV(){ + $this->cModel->createTable(); + $csv = new CSVImporter(); + $csv->importCSV($this->file, 'customer'); + } +} +?> \ No newline at end of file diff --git a/controller/CustomerListController.js b/controller/CustomerListController.js new file mode 100644 index 0000000..b948b2f --- /dev/null +++ b/controller/CustomerListController.js @@ -0,0 +1,23 @@ +import {renderCustomerList} from '../view/CustomerListView.js'; +$(document).ready(function(){ + $('#btnLoad').click(function(){ + $(this).val('Loading...'); + $.post(window.location.href.match(/^.*\//) + '/CustomerService/CustomerService.php',) + .done(function(data) { + var jsonObj = JSON.parse(data); + renderCustomerList(jsonObj, '#table'); + }).fail(function(jqxhr, settings, ex) { + console.log('failed, ' , jqxhr); + $('#table').html("No data found..!!"); + }); + }); + $('#buttonImport').click(function(){ + $(this).val('Loading...'); + $.post(window.location.href.match(/^.*\//) + '/CustomerService/CustomerImportService.php',) + .done(function(data) { + alert("Successfully Imported data..!!"); + }).fail(function(jqxhr, settings, ex) { + alert(failed, + jqxhr); + }); + }); +}); \ No newline at end of file diff --git a/db.php b/db.php new file mode 100644 index 0000000..062728b --- /dev/null +++ b/db.php @@ -0,0 +1,66 @@ +connect(); + } + + // mysql connecting method + /** + * connect to mysql db with the given parameters + */ + public function connect(){ + if(!$this->isConnected) + { + $this->conn = @mysql_connect($this->db_host,$this->db_username,$this->db_password); + if($this->conn) + { + $selectdb = @mysql_select_db($this->db_name,$this->conn); + if($selectdb) + { + $this->isConnected = true; + return true; + } else + { + return false; + } + } else + { + return false; + } + } else + { + return true; + } + } + + // table existance checking ... + /** + * Lchecks if table exists in database + * @param {?string} tableName - name of table to check + */ + public function isTableExists($tableName){ + $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$tableName.'"'); + if($tablesInDb) + { + if(mysql_num_rows($tablesInDb) == 1) + { + return true; + } + else + { + return false; + } + } + } +} +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..c1107d7 --- /dev/null +++ b/index.php @@ -0,0 +1,11 @@ +isInstalled){ + header('Location: ./install/index.php'); +} + +$controller = new CustomerController(); +$controller->render(); diff --git a/install/config.php b/install/config.php new file mode 100644 index 0000000..b985a8e --- /dev/null +++ b/install/config.php @@ -0,0 +1,54 @@ +loadConfig(); + } + + // loads config from file ... + /** + * loads config from file + */ + public function loadConfig() { + $string = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/config.json'); + $json_a = json_decode($string, true); + + $this->isInstalled = $json_a['isInstalled']; + $this->db_host = $json_a['db_host']; + $this->db_username = $json_a['db_username']; + $this->db_password = $json_a['db_password']; + $this->db_name = $json_a['db_name']; + } + + public function toJson(){ + return json_encode( + array( + 'isInstalled'=> $this->isInstalled, + 'db_host'=> $this->db_host, + 'db_username'=> $this->db_username, + 'db_password'=> $this->db_password, + 'db_name'=> $this->db_name + )); + } + + // saves cofig into file ... + /** + * saves config into file + */ + public function saveConfig(){ + $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/config.json', 'w'); + fwrite($fp, $this->toJson()); + fclose($fp); + } +} +?> \ No newline at end of file diff --git a/install/form_submit.js b/install/form_submit.js new file mode 100644 index 0000000..6353711 --- /dev/null +++ b/install/form_submit.js @@ -0,0 +1,27 @@ + // ... + /** + * calls on submit button click + */ +$(document).ready(function () { + $('#btnSubmit').click(function () { + if($('#inputHost').val().lenght <= 0 || $('#inputUsername').val().length <= 0 || + $('#inputPassword').val().length <= 0 || $('#inputDBname').val().length <= 0) { + alert("Please fill all fields..!!"); + return; + } + var json = { + 'isInstalled': false, + 'db_host': $('#inputHost').val(), + 'db_username': $('#inputUsername').val(), + 'db_password': $('#inputPassword').val(), + 'db_name': $('#inputDBname').val() + } + $.post('form_submit.php', json) + .done(function (data) { + console.log(data); + window.location = '../index.php' + }).fail(function (jqxhr, settings, ex) { + console.log('failed, ', jqxhr); + }); + }); +}); \ No newline at end of file diff --git a/install/form_submit.php b/install/form_submit.php new file mode 100644 index 0000000..31cd623 --- /dev/null +++ b/install/form_submit.php @@ -0,0 +1,26 @@ +isInstalled = false;//$_POST['isInstalled']; +$config->db_host = $_POST['db_host']; +$config->db_username = $_POST['db_username']; +$config->db_password = $_POST['db_password']; +$config->db_name = $_POST['db_name']; + +$config->saveConfig(); + +if(!$config->isInstalled){ + $conn = mysqli_connect($config->db_host, $config->db_username, $config->db_password); + if (!$conn) { + die("Connection failed: " . mysqli_connect_error()); + } + $sql = "CREATE DATABASE " . $config->db_name; + if (mysqli_query($conn, $sql)) { + mysqli_close($conn); + $config->isInstalled = true; + $config->saveConfig(); + echo 'success'; + } else { + echo "Error creating database: " . mysqli_error($conn); + } + } \ No newline at end of file diff --git a/install/index.php b/install/index.php new file mode 100644 index 0000000..2ebd1fc --- /dev/null +++ b/install/index.php @@ -0,0 +1,64 @@ +isInstalled){ + header('Location: ../index.php'); +} +?> + + + + + + + + + +
+
+

Enter Database Details

+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+ +
+ + + + + + + + \ No newline at end of file diff --git a/model/CustomerModel.php b/model/CustomerModel.php new file mode 100644 index 0000000..033535b --- /dev/null +++ b/model/CustomerModel.php @@ -0,0 +1,44 @@ +db = $db; + } + + // creates table for CustomerModel + /** + * creates table for customer model + */ + public function createTable(){ + $query = "CREATE TABLE IF NOT EXISTS `" . $this->table . "` (\n" + . " `id` int(11) NOT NULL,\n" + . " `first_name` varchar(20) NOT NULL,\n" + . " `last_name` varchar(20) NOT NULL,\n" + . " `email` varchar(100) NOT NULL,\n" + . " `gender` varchar(7) NOT NULL,\n" + . " `ip_address` varchar(23) NOT NULL,\n" + . " `company` varchar(100) NOT NULL,\n" + . " `city` varchar(50) NOT NULL,\n" + . " `title` varchar(100) NOT NULL,\n" + . " `website` text NOT NULL\n" + . ")"; + + $res = @mysql_query($query, $this->conn) or die(mysql_error()); + if($this->isTableExists($this->table)){ + return true; + } else { + return false; + } + } +} +?> \ No newline at end of file diff --git a/model/Model.php b/model/Model.php new file mode 100644 index 0000000..832e27e --- /dev/null +++ b/model/Model.php @@ -0,0 +1,64 @@ +result = null; + $q = 'SELECT '.$rows.' FROM '.$table; + if($where != null) + $q .= ' WHERE '.$where; + if($order != null) + $q .= ' ORDER BY '.$order; + if($this->isTableExists($table)) + { + $query = @mysql_query($q); + if($query) + { + $this->numResults = mysql_num_rows($query); + for($i = 0; $i < $this->numResults; $i++) + { + $r = mysql_fetch_array($query); + $key = array_keys($r); + for($x = 0; $x < count($key); $x++) + { + if(!is_int($key[$x])) + { + if(mysql_num_rows($query) > 1) + $this->result[$i][$key[$x]] = $r[$key[$x]]; + else if(mysql_num_rows($query) < 1) + $this->result = null; + else + $this->result[$key[$x]] = $r[$key[$x]]; + } + } + } + return true; + } + else + { + return false; + } + } + else + return false; + } +} +?> \ No newline at end of file diff --git a/view/CustomerListView.js b/view/CustomerListView.js new file mode 100644 index 0000000..5d5a8f0 --- /dev/null +++ b/view/CustomerListView.js @@ -0,0 +1,25 @@ + // customer list rendering method ... + /** + * renders the customer list into given html table container + * @param {?json object} c_json_list - the customer list json object array + * @param {?string} render_container - the container id or class + */ + export function renderCustomerList (c_json_list, render_container) { + var customer = c_json_list[0]; + var columns = []; + for(var property in customer){ + columns.push({ + data: property + }) + } + if ($.fn.dataTable.isDataTable(render_container)) { + table = $(render_container).DataTable(); + } + else { + $(render_container).DataTable({ + data: c_json_list, + "processing": true, + columns: columns, + }); + } + } \ No newline at end of file diff --git a/view/CustomerView.php b/view/CustomerView.php new file mode 100644 index 0000000..ee28bf3 --- /dev/null +++ b/view/CustomerView.php @@ -0,0 +1,41 @@ + + + + + + + +
+
+
+ + +
+
+ + + + + + + + + + + + + + + +
IDFirst NameLast NameEmailGenderIP AddressCompanyCityTitleWebsite
+
+ + + + + + + + + + \ No newline at end of file