Last active
September 28, 2022 12:44
-
-
Save Oluwarufus/95d53d5601f36c5907d6a06db221fcb6 to your computer and use it in GitHub Desktop.
Enistic Task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Enistic Dev Task</title> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-md-6 mx-auto"> | |
<div class="card p-5 bg-light"> | |
<h2 class="header-title text-center">Enistic Dev Task</h2> | |
<p class="text-center">by Rufus Oyemade</p> | |
<?php | |
/** | |
* | |
*/ | |
class EnisticTask { | |
public $db_host; | |
public $db_user; | |
public $db_password; | |
public $db_name; | |
function __construct($db_host, $db_user, $db_password, $db_name) { | |
try { | |
$this->connection = new mysqli($db_host, $db_user, $db_password, $db_name); | |
$this->db_table = 'prices'; | |
} catch (Exception $e) { | |
echo $this->errorAlert("Failed to connect to MySQL, please check the config"); | |
die('Stopped.'); | |
} | |
} | |
public function init() { | |
echo $this->createTable(); | |
echo $this->seedDatabase(); | |
echo $this->renderData(); | |
} | |
private function createTable() { | |
$sql = "SHOW TABLES LIKE '" . $this->db_table . "'"; | |
$result = $this->connection->query($sql); | |
if ($result->num_rows !== 1) { | |
$sql = "CREATE TABLE " . $this->db_table . " ( | |
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
description VARCHAR(255) NOT NULL, | |
price DECIMAL(6,4) NOT NULL | |
)"; | |
try { | |
$this->connection->query($sql); | |
return $this->successAlert("Table created successfully"); | |
} catch (Exception $e) { | |
return $this->errorAlert("Error creating table: " . $this->connection->error); | |
} | |
} else { | |
return $this->errorAlert("Table already exists"); | |
} | |
} | |
private function seedDatabase() { | |
$sql = "SELECT * FROM " . $this->db_table; | |
$result = $this->connection->query($sql); | |
if ($result->num_rows === 0) { | |
$sql = "INSERT INTO " . $this->db_table . " (description, price) | |
VALUES ('Apples', 27), ('Oranges', 83), ('Bananas', 12)"; | |
try { | |
$this->connection->query($sql); | |
return $this->successAlert("Table seeded successfully"); | |
} catch (Exception $e) { | |
return $this->errorAlert("Error seeding table with sample data: " . $this->connection->error); | |
} | |
} else { | |
return $this->errorAlert("Table already seeded."); | |
} | |
} | |
private function renderData() { | |
$sql = "SELECT * FROM " . $this->db_table; | |
$result = $this->connection->query($sql); | |
echo "<table class='table'> | |
<thead> | |
<tr> | |
<th scope='col'>Description</th> | |
<th scope='col'>Price</th> | |
</tr> | |
</thead> | |
<tbody>"; | |
if ($result->num_rows > 0) { | |
while ($item = $result->fetch_assoc()) { | |
echo "<tr> | |
<td>" . $item["description"] . "</td> | |
<td>" . $item["price"] . "</td> | |
</tr>"; | |
} | |
} else { | |
echo "<tr><td>0 results</td></td>"; | |
} | |
echo "</tbody></table"; | |
} | |
private function successAlert($message) { | |
echo "<div class='alert alert-success' role='alert'> $message </div>"; | |
} | |
private function errorAlert($message) { | |
echo "<div class='alert alert-warning' role='alert'> $message </div>"; | |
} | |
function __destruct() { | |
$this->connection->close(); | |
} | |
} | |
$run = new EnisticTask('localhost', 'root', 'pass', 'enistic'); | |
$run->init(); | |
?> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course, this is not a live project, but if it were, I will do the following (to mention a few):
Thank you for the consideration.