From 9e982f87a96ab7f29486808cabb547ba5905a7bd Mon Sep 17 00:00:00 2001 From: Marwan Ehab Date: Sun, 1 Jan 2023 16:37:34 +0200 Subject: [PATCH] Configuration to deploy to heroku --- server/DbConnect.php | 99 ++++++++++--------- server/index.php | 1 - server/product.php | 83 ++++++++-------- src/components/CreateProduct/CreateProduct.js | 2 +- src/components/ProductsList/ProductsList.js | 4 +- 5 files changed, 97 insertions(+), 92 deletions(-) diff --git a/server/DbConnect.php b/server/DbConnect.php index cf52d4d..234d87c 100644 --- a/server/DbConnect.php +++ b/server/DbConnect.php @@ -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(); diff --git a/server/index.php b/server/index.php index 36c7651..0e04cfd 100644 --- a/server/index.php +++ b/server/index.php @@ -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": diff --git a/server/product.php b/server/product.php index f6a89b6..3749128 100644 --- a/server/product.php +++ b/server/product.php @@ -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) { @@ -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) { @@ -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) { @@ -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']; @@ -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) { diff --git a/src/components/CreateProduct/CreateProduct.js b/src/components/CreateProduct/CreateProduct.js index d553492..9d637b1 100644 --- a/src/components/CreateProduct/CreateProduct.js +++ b/src/components/CreateProduct/CreateProduct.js @@ -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('/'); diff --git a/src/components/ProductsList/ProductsList.js b/src/components/ProductsList/ProductsList.js index 1d211db..2f55042 100644 --- a/src/components/ProductsList/ProductsList.js +++ b/src/components/ProductsList/ProductsList.js @@ -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); }); } @@ -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