Implemented product monking data

This commit is contained in:
Kevin Frantz 2018-07-14 21:58:36 +02:00
parent f51177f703
commit 0ace91c6a7
5 changed files with 153 additions and 23 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace repository;
/**
*
* @author kevinfrantz
*
*/
abstract class AbstractRepository
{
/**
* @var \PDO
*/
protected $database;
public function __construct(\PDO $database){
$this->database = $database;
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace repository\product;
use Doctrine\Common\Collections\ArrayCollection;
use repository\AbstractRepository;
use entity\product\ProductInterface as ProductEntityInterface;
use core\Core;
/**
*
* @author kevinfrantz
*
*/
final class Product extends AbstractRepository implements ProductInterface
{
const TABLE = 'product';
/**
* Out of time reasons the values are not escaped!
*
* {@inheritdoc}
* @see \repository\product\ProductInterface::addProducts()
*/
public function addProducts(ArrayCollection $products): void
{
/**
*
* @var ProductEntityInterface $product
*/
foreach ($products->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
{}
}

View File

@ -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));

View File

@ -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
1 name color price_eur tax image
2 Continental Goldenrod 73.19 19 https://picsum.photos/200/300/?random
3 Mustang Goldenrod 49.75 20 https://picsum.photos/200/300/?random
4 Grand Vitara Turquoise 19.06 21 https://picsum.photos/200/300/?random
5 Pajero Puce 35.7 22 https://picsum.photos/200/300/?random
6 EXP Aquamarine 26.13 23 https://picsum.photos/200/300/?random
7 Golf Fuscia 50.64 24 https://picsum.photos/200/300/?random
8 Explorer Sport Trac Puce 76.08 25 https://picsum.photos/200/300/?random
9 Rally Wagon 2500 Red 56.14 26 https://picsum.photos/200/300/?random
10 J Turquoise 44.02 27 https://picsum.photos/200/300/?random
11 Astro Turquoise 33.65 28 https://picsum.photos/200/300/?random
12 Sierra 2500 Purple 26.97 29 https://picsum.photos/200/300/?random
13 Continental GT Fuscia 30.26 30 https://picsum.photos/200/300/?random
14 Continental GT Mauv 63.78 31 https://picsum.photos/200/300/?random
15 Esteem Goldenrod 61.47 32 https://picsum.photos/200/300/?random
16 LS Aquamarine 23.38 33 https://picsum.photos/200/300/?random
17 Outlook Teal 66.99 34 https://picsum.photos/200/300/?random
18 911 Orange 25.83 35 https://picsum.photos/200/300/?random
19 Jetta Green 42.49 36 https://picsum.photos/200/300/?random
20 Bronco Pink 60.81 37 https://picsum.photos/200/300/?random
21 Savana Yellow 63.5 38 https://picsum.photos/200/300/?random

View File

@ -1,22 +1,59 @@
<?php
use core\Core;
use core\Core;
use Doctrine\Common\Collections\ArrayCollection;
use entity\product\Product;
use entity\currency\Euro;
use entity\price\Price;
use entity\image\UrlImage;
use repository\product\Product as ProductRepository;
require __DIR__. '/../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';
/**
* Maintainance script - Not written nice ;)
**/
/**
* Maintainance script - Not written nice ;)
*/
function loadAndExec(string $file): void
{
$core = new Core();
$pdo = $core->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'));
}
function loadAndExec(string $file){
$core = new Core();
$pdo = $core->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);
}
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);
?>