mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-22 18:21:04 +01:00
Implemented product monking data
This commit is contained in:
parent
f51177f703
commit
0ace91c6a7
20
src/repository/AbstractRepository.php
Normal file
20
src/repository/AbstractRepository.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
51
src/repository/product/Product.php
Normal file
51
src/repository/product/Product.php
Normal 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
|
||||||
|
{}
|
||||||
|
}
|
@ -1,8 +1,9 @@
|
|||||||
CREATE TABLE `test_db`.`product` (
|
CREATE TABLE `test_db`.`product` (
|
||||||
`id` INT NOT NULL,
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
`name` VARCHAR(45) NULL,
|
`name` VARCHAR(45) NULL,
|
||||||
`color` VARCHAR(12) NULL,
|
`color` VARCHAR(45) NULL,
|
||||||
`price` INT NULL,
|
`price` INT NULL,
|
||||||
|
`tax` INT NULL,
|
||||||
`image` VARCHAR(180) NULL,
|
`image` VARCHAR(180) NULL,
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE INDEX `id_UNIQUE` (`id` ASC));
|
UNIQUE INDEX `id_UNIQUE` (`id` ASC));
|
||||||
|
21
src/setup/monking/product.csv
Normal file
21
src/setup/monking/product.csv
Normal 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,22 +1,59 @@
|
|||||||
<?php
|
<?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){
|
echo "Create databases...\n";
|
||||||
$core = new Core();
|
foreach ([
|
||||||
$pdo = $core->getDatabase();
|
'order_product',
|
||||||
echo "Create database $file...\n";
|
'order',
|
||||||
$pdo->exec(file_get_contents(__DIR__.'/database/'.$file.'.sql'));
|
'product',
|
||||||
}
|
'user'
|
||||||
|
] as $file) {
|
||||||
|
loadAndExec($file);
|
||||||
|
}
|
||||||
|
|
||||||
echo "Create databases...\n";
|
echo "Insert product dummy data...\n";
|
||||||
foreach(['order_product','order','product','user'] as $file){
|
$products = new ArrayCollection();
|
||||||
loadAndExec($file);
|
$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);
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue
Block a user