Skip to content

Commit

Permalink
Configuration to deploy to heroku
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanehab98 committed Jan 1, 2023
1 parent 8272ccc commit 9e982f8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 92 deletions.
99 changes: 54 additions & 45 deletions server/DbConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,69 @@
*/
class DbConnect
{
private $server = 'localhost';
private $dbname = 'scandiweb_task';
private $user = 'root';
private $pass = '';
private $server = 'https://databases.000webhost.com';
private $dbname = 'id20087235_scandiweb_task';
private $user = 'id20087235_root';
private $pass = 'WpG)fM7WHlk%oEOX';
// private $server = 'localhost';
// private $dbname = 'scandiweb_task';
// private $user = 'root';
// private $pass = '';



// public function __construct()
// {
// $sql =
// "CREATE DATABASE IF NOT EXISTS " . $this->dbname . ";" .
// "CREATE TABLE IF NOT EXISTS products (
// SKU varchar(255) PRIMARY KEY,
// name varchar(255),
// price varchar(255),
// type varchar(255)
// ) ENGINE=innoDB;" .
// "CREATE TABLE IF NOT EXISTS furniture (
// id int AUTO_INCREMENT PRIMARY KEY,
// SKU varchar(255) UNIQUE,
// length varchar(255),
// width varchar(255),
// height varchar(255),
// CONSTRAINT furniture_products FOREIGN KEY (SKU) REFERENCES products(SKU) ON DELETE CASCADE
// ) ENGINE=innoDB;" .
// "CREATE TABLE IF NOT EXISTS books (
// id int AUTO_INCREMENT PRIMARY KEY,
// SKU varchar(255) UNIQUE,
// weight varchar(255),
// CONSTRAINT books_products FOREIGN KEY (SKU) REFERENCES products(SKU) ON DELETE CASCADE
// ) ENGINE=innoDB;" .
// "CREATE TABLE IF NOT EXISTS dvds (
// id int AUTO_INCREMENT PRIMARY KEY,
// SKU varchar(255) UNIQUE,
// size varchar(255),
// CONSTRAINT dvds_products FOREIGN KEY (SKU) REFERENCES products(SKU) ON DELETE CASCADE
// ) ENGINE=innoDB;";
// $conn = new mysqli($this->server, $this->user, $this->pass);
// if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
// }
// if ($conn->query($sql) === TRUE) {
// echo "Database created successfully";
// } else {
// echo "Error creating database: " . $conn->error;
// }
// $conn->close();
// }
public function __construct()
{
$sql =
"CREATE DATABASE IF NOT EXISTS " . $this->dbname . ";";
$conn = new mysqli($this->server, $this->user, $this->pass);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($conn->query($sql) !== TRUE) {
echo "Error creating database: " . $conn->error;
}
$conn->close();
}

public function connect()
{
try {
$conn = new PDO('mysql:host=' . $this->server . ';dbname=' . $this->dbname, $this->user, $this->pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql =
"CREATE TABLE IF NOT EXISTS products (
SKU varchar(255) PRIMARY KEY,
name varchar(255) NOT NULL,
price double NOT NULL,
type varchar(255) NOT NULL
) ENGINE=INNODB; " .
"CREATE TABLE IF NOT EXISTS furniture (
id int AUTO_INCREMENT PRIMARY KEY,
SKU varchar(255) UNIQUE NOT NULL,
length double NOT NULL,
width double NOT NULL,
height double NOT NULL,
CONSTRAINT furniture_products FOREIGN KEY (SKU) REFERENCES products(SKU) ON DELETE CASCADE
) ENGINE=INNODB; " .
"CREATE TABLE IF NOT EXISTS books (
id int AUTO_INCREMENT PRIMARY KEY,
SKU varchar(255) UNIQUE NOT NULL,
weight double NOT NULL,
CONSTRAINT books_products FOREIGN KEY (SKU) REFERENCES products(SKU) ON DELETE CASCADE
) ENGINE=INNODB; " .
"CREATE TABLE IF NOT EXISTS dvds (
id int AUTO_INCREMENT PRIMARY KEY,
SKU varchar(255) UNIQUE NOT NULL,
size double NOT NULL,
CONSTRAINT dvds_products FOREIGN KEY (SKU) REFERENCES products(SKU) ON DELETE CASCADE
) ENGINE=INNODB; ";
$stmt = $conn->prepare($sql);
try {
$stmt->execute();
} catch (\Throwable $error) {
echo "Failed to create tables: " . $error->getMessage();
}
return $conn;
} catch (\Exception $e) {
echo "Database Error: " . $e->getMessage();
Expand Down
1 change: 0 additions & 1 deletion server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
$type = $product->type;
$class = $type;
$product_object = new $class($product, true);
// $products_list = new ProductList();
$product_object->addProduct($conn, $type);
break;
case "DELETE":
Expand Down
83 changes: 40 additions & 43 deletions server/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ public function addProduct($conn, $type)
}

if ($valid) {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type)" .
"INSERT INTO dvds(SKU, size) VALUES(:SKU, :size)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':size', $this->size);

try {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type)" .
"INSERT INTO dvds(SKU, size) VALUES(:SKU, :size)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':size', $this->size);
$stmt->execute();
$response = ['status' => '200', 'message' => 'Record created successfully.'];
} catch (\Throwable $error) {
Expand Down Expand Up @@ -116,17 +115,16 @@ public function addProduct($conn, $type)
$valid = true;
}
if ($valid) {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type);" .
"INSERT INTO books(SKU, weight) VALUES(:SKU, :weight)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':weight', $this->weight);

try {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type);" .
"INSERT INTO books(SKU, weight) VALUES(:SKU, :weight)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':weight', $this->weight);
$stmt->execute();
$response = ['status' => '200', 'message' => 'Record created successfully.'];
} catch (\Throwable $error) {
Expand Down Expand Up @@ -188,19 +186,18 @@ public function addProduct($conn, $type)
}

if ($valid) {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type)" .
"INSERT INTO furniture(SKU, length, width, height) VALUES(:SKU, :length, :width, :height)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':length', $this->length);
$stmt->bindParam(':width', $this->width);
$stmt->bindParam(':height', $this->height);

try {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type)" .
"INSERT INTO furniture(SKU, length, width, height) VALUES(:SKU, :length, :width, :height)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':length', $this->length);
$stmt->bindParam(':width', $this->width);
$stmt->bindParam(':height', $this->height);
$stmt->execute();
$response = ['status' => '200', 'message' => 'Record created successfully.'];
} catch (\Throwable $error) {
Expand All @@ -222,14 +219,14 @@ public function addProduct($product)

public function getProducts($conn)
{
$sql = "SELECT P.SKU, P.name, P.price, P.type, B.weight, D.size, F.height, F.width, F.length FROM products P
LEFT JOIN books B on B.SKU = P.SKU
LEFT JOIN dvds D on D.SKU = P.SKU
LEFT JOIN furniture F on F.SKU = P.SKU
ORDER BY p.SKU";
$path = explode('/', $_SERVER['REQUEST_URI']);
$stmt = $conn->prepare($sql);
try {
$sql =
"SELECT P.SKU, P.name, P.price, P.type, B.weight, D.size, F.height, F.width, F.length FROM products P
LEFT JOIN books B on B.SKU = P.SKU
LEFT JOIN dvds D on D.SKU = P.SKU
LEFT JOIN furniture F on F.SKU = P.SKU
ORDER BY P.SKU";
$stmt = $conn->prepare($sql);
$stmt->execute();
while ($product = $stmt->fetch(PDO::FETCH_ASSOC)) {
$type = $product['type'];
Expand All @@ -246,11 +243,11 @@ public function getProducts($conn)

public function deleteProduct($conn)
{
$sql = "DELETE FROM products WHERE SKU = :id";
$path = explode('/', $_SERVER['REQUEST_URI']);
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[4]);
try {
$sql = "DELETE FROM products WHERE SKU = :id";
$path = explode('/', $_SERVER['REQUEST_URI']);
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[4]);
$stmt->execute();
$response = ['status' => '200', 'message' => 'Record deleted successfully.'];
} catch (\Throwable $error) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/CreateProduct/CreateProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function CreateProduct() {
return true
});
if (valid) {
axios.post('http://localhost/scandiweb-task/server/index.php/save', product).then(function (response) {
axios.post('https://scandiweb-task-marwan-elsheikh.000webhostapp.com//save', product).then(function (response) {
console.log(response.data);
if (response.data.status === '200') {
navigate('/');
Expand Down
4 changes: 2 additions & 2 deletions src/components/ProductsList/ProductsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function ProductsList() {


function getProducts() {
axios.get('http://localhost/scandiweb-task/server/index.php').then(function (response) {
axios.get('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/').then(function (response) {
setProducts(response.data);
});
}
Expand All @@ -40,7 +40,7 @@ export default function ProductsList() {
const deleteHelper = (SKU) => {
let tempSelected = [];
Object.assign(tempSelected, selected);
axios.delete('http://localhost/scandiweb-task/server/index.php/' + SKU + '/delete').then(function (response) {
axios.delete('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/' + SKU + '/delete').then(function (response) {
console.log(response.data);
tempSelected = tempSelected.filter(product => {
// eslint-disable-next-line eqeqeq
Expand Down

0 comments on commit 9e982f8

Please sign in to comment.