Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CSVImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/db.php');
// helper class to export CSV into mysql ...
/**
* CSV Importer class
*/
class CSVImporter extends db{

function __construct(){
parent::__construct();
}

// imports the csv file into given table ...
/**
* imports the given csv file into given table
* @param {?string} file - file path o CSV
* @param {?string} tableName - name of table into which data to be imported
*/
public function importCSV($file, $tableName) {
if($this->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...!!");
}
}
}
?>
6 changes: 6 additions & 0 deletions CustomerService/CustomerImportService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/controller/CustomerController.php');

$customerCntrl = new CustomerController();
$customerCntrl->importCSV();
echo 'success';
5 changes: 5 additions & 0 deletions CustomerService/CustomerService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/CustomerService/CustomerServiceHandler.php');
$csHandler = new CustomerServiceHandler();
$csHandler->getCustomers();
?>
30 changes: 30 additions & 0 deletions CustomerService/CustomerServiceHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/model/CustomerModel.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/CustomerService/RESTService.php');

// customer service handler class ...
/**
* handler class for CustomerService
*/
class CustomerServiceHandler extends RESTService {

function getCustomers() {
$this->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;
}
}
?>
27 changes: 27 additions & 0 deletions CustomerService/RESTService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
// The RESTService base class ...
/**
* The RESTService base class
*/
class RESTService {

private $httpVersion = "HTTP/1.1";

public function setHttpHeaders($contentType, $statusCode){

$statusMessage = $this -> 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];
}
}
?>
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"isInstalled":false,"db_host":"","db_username":"","db_password":"","db_name":""}
33 changes: 33 additions & 0 deletions controller/CustomerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/model/CustomerModel.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/CSVImporter.php');

// controller class for customer
/**
* Controller class for Customer
*/
class CustomerController{
public $cModel;
private $file = "";

function __construct(){
$this->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');
}
}
?>
23 changes: 23 additions & 0 deletions controller/CustomerListController.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
66 changes: 66 additions & 0 deletions db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/install/config.php');
// load constants, populate in html, then run ...
/**
* db class extending from config class
*/
class db extends config{

public $isConnected = false;

public function __construct() {
parent::__construct();
$this->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;
}
}
}
}
?>
11 changes: 11 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/controller/CustomerController.php');

//checking if database installed and redirecting
$config = new config();
if(!$config->isInstalled){
header('Location: ./install/index.php');
}

$controller = new CustomerController();
$controller->render();
54 changes: 54 additions & 0 deletions install/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

// config class ...
/**
* config class to save get data from config file
*/
class config {
public $isInstalled = false;
public $db_host = "";
public $db_username = "";
public $db_password = "";
public $db_name = "";

public function __construct(){
$this->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);
}
}
?>
27 changes: 27 additions & 0 deletions install/form_submit.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
26 changes: 26 additions & 0 deletions install/form_submit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/install/config.php');
$config = new config();
$config->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);
}
}
Loading