From 0ace91c6a7bede4c3c0f6ec110313ef1b7e9a627 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 14 Jul 2018 21:58:36 +0200 Subject: [PATCH] Implemented product monking data --- src/repository/AbstractRepository.php | 20 +++++++ src/repository/product/Product.php | 51 +++++++++++++++++ src/setup/database/product.sql | 5 +- src/setup/monking/product.csv | 21 +++++++ src/setup/setup.php | 79 ++++++++++++++++++++------- 5 files changed, 153 insertions(+), 23 deletions(-) create mode 100644 src/repository/AbstractRepository.php create mode 100644 src/repository/product/Product.php create mode 100644 src/setup/monking/product.csv diff --git a/src/repository/AbstractRepository.php b/src/repository/AbstractRepository.php new file mode 100644 index 0000000..4deca72 --- /dev/null +++ b/src/repository/AbstractRepository.php @@ -0,0 +1,20 @@ +database = $database; + } +} + diff --git a/src/repository/product/Product.php b/src/repository/product/Product.php new file mode 100644 index 0000000..8b51506 --- /dev/null +++ b/src/repository/product/Product.php @@ -0,0 +1,51 @@ +toArray() as $product) { + $statement = $this->database->prepare("INSERT INTO `" . Core::DATABASE_NAME . "`.`" . self::TABLE . "` (`name`, `color`, `price`,`tax`, `image`) VALUES (?, ?, ?,?,?);"); + $statement->execute([ + $product->getName(), + $product->getColor(), + $product->getPrice()->getNetto()->getCents(), + $product->getPrice()->getTax(), + $product->getImage()->getImage(), + ]); + } + } + + public function getAllProducts(): ArrayCollection + {} + + public function deleteAllProducts(): void + {} + + public function getProductById(int $id): ProductEntityInterface + {} +} \ No newline at end of file diff --git a/src/setup/database/product.sql b/src/setup/database/product.sql index e9ab6e0..c58e9bb 100644 --- a/src/setup/database/product.sql +++ b/src/setup/database/product.sql @@ -1,8 +1,9 @@ CREATE TABLE `test_db`.`product` ( - `id` INT NOT NULL, + `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, - `color` VARCHAR(12) NULL, + `color` VARCHAR(45) NULL, `price` INT NULL, + `tax` INT NULL, `image` VARCHAR(180) NULL, PRIMARY KEY (`id`), UNIQUE INDEX `id_UNIQUE` (`id` ASC)); diff --git a/src/setup/monking/product.csv b/src/setup/monking/product.csv new file mode 100644 index 0000000..f315e49 --- /dev/null +++ b/src/setup/monking/product.csv @@ -0,0 +1,21 @@ +name,color,price_eur,tax,image +Continental,Goldenrod,73.19,19,https://picsum.photos/200/300/?random +Mustang,Goldenrod,49.75,20,https://picsum.photos/200/300/?random +Grand Vitara,Turquoise,19.06,21,https://picsum.photos/200/300/?random +Pajero,Puce,35.7,22,https://picsum.photos/200/300/?random +EXP,Aquamarine,26.13,23,https://picsum.photos/200/300/?random +Golf,Fuscia,50.64,24,https://picsum.photos/200/300/?random +Explorer Sport Trac,Puce,76.08,25,https://picsum.photos/200/300/?random +Rally Wagon 2500,Red,56.14,26,https://picsum.photos/200/300/?random +J,Turquoise,44.02,27,https://picsum.photos/200/300/?random +Astro,Turquoise,33.65,28,https://picsum.photos/200/300/?random +Sierra 2500,Purple,26.97,29,https://picsum.photos/200/300/?random +Continental GT,Fuscia,30.26,30,https://picsum.photos/200/300/?random +Continental GT,Mauv,63.78,31,https://picsum.photos/200/300/?random +Esteem,Goldenrod,61.47,32,https://picsum.photos/200/300/?random +LS,Aquamarine,23.38,33,https://picsum.photos/200/300/?random +Outlook,Teal,66.99,34,https://picsum.photos/200/300/?random +911,Orange,25.83,35,https://picsum.photos/200/300/?random +Jetta,Green,42.49,36,https://picsum.photos/200/300/?random +Bronco,Pink,60.81,37,https://picsum.photos/200/300/?random +Savana,Yellow,63.5,38,https://picsum.photos/200/300/?random diff --git a/src/setup/setup.php b/src/setup/setup.php index b6509d7..5dbff29 100644 --- a/src/setup/setup.php +++ b/src/setup/setup.php @@ -1,22 +1,59 @@ -getDatabase(); - echo "Create database $file...\n"; - $pdo->exec(file_get_contents(__DIR__.'/database/'.$file.'.sql')); - } - - echo "Create databases...\n"; - foreach(['order_product','order','product','user'] as $file){ - loadAndExec($file); - } - +getDatabase(); + echo "Drop table $file...\n"; + $pdo->exec("DROP TABLE $file;"); + echo "Create table $file...\n"; + $pdo->exec(file_get_contents(__DIR__ . '/database/' . $file . '.sql')); +} + +echo "Create databases...\n"; +foreach ([ + 'order_product', + 'order', + 'product', + 'user' +] as $file) { + loadAndExec($file); +} + +echo "Insert product dummy data...\n"; +$products = new ArrayCollection(); +$lines = explode("\n", trim(file_get_contents(__DIR__ . '/monking/product.csv'))); +unset($lines[0]); +foreach ($lines as $number=>$line) { + $colums = explode(',', $line); + $product = new Product(); + $product->setName($colums[0]); + $product->setColor($colums[1]); + $euro = new Euro(); + $euro->setCents((int) (floatval($colums[2]) * 100)); + $price = new Price(); + $price->setPrice($euro); + $price->setTax((int)$colums[3]); + $product->setPrice($price); + $image = new UrlImage(); + $image->setImage($colums[4]); + $product->setImage($image); + echo $number.'. product '.$product->getName()." added to collection...\n"; + $products->add($product); +} + +$productRepository = new ProductRepository((new Core())->getDatabase()); +$productRepository->addProducts($products); ?> \ No newline at end of file