diff --git a/src/controller/order/Order.php b/src/controller/order/Order.php index ef82423..898ea4f 100644 --- a/src/controller/order/Order.php +++ b/src/controller/order/Order.php @@ -2,6 +2,10 @@ namespace controller\order; use controller\AbstractDefaultController; +use core\CoreInterface; +use repository\order\OrderInterface as OrderRepositoryInterface; +use repository\product\ProductInterface as ProductRepositoryInterface; +use repository\product\Product as ProductRepository; /** * @@ -10,15 +14,48 @@ use controller\AbstractDefaultController; */ final class Order extends AbstractDefaultController implements OrderInterface { - public function addProduct(): void - {} - public function store(): void + /** + * + * @var OrderRepositoryInterface + */ + protected $orderRepository; + + /** + * + * @var ProductRepositoryInterface + */ + protected $productRepository; + + public function __construct(CoreInterface $core) + { + parent::__construct($core); + $this->productRepository = new ProductRepository($this->core); + } + + private function addProduct(): void + { + $this->core->getBasket()->addProduct($this->productRepository->getById($this->post['add'])); + } + + private function store(): void {} public function basket(): void { - $this->render('order/basket.html.twig',['basket'=>$this->core->getBasket()]); + if ($this->post) { + $this->postRoutine(); + } + $this->render('order/basket.html.twig', [ + 'basket' => $this->core->getBasket() + ]); + } + + private function postRoutine(): void + { + if ($this->post['add']) { + $this->addProduct(); + } } public function selectPaymentMethod(): void diff --git a/src/controller/order/OrderInterface.php b/src/controller/order/OrderInterface.php index d67f91f..c9ad257 100644 --- a/src/controller/order/OrderInterface.php +++ b/src/controller/order/OrderInterface.php @@ -7,13 +7,6 @@ namespace controller\order; */ interface OrderInterface { - /** - * Saves the order - */ - public function store():void; - - public function addProduct():void; - public function basket():void; public function selectPaymentMethod():void; diff --git a/src/controller/product/Product.php b/src/controller/product/Product.php index 56ee5ad..aad5079 100644 --- a/src/controller/product/Product.php +++ b/src/controller/product/Product.php @@ -37,6 +37,7 @@ final class Product extends AbstractDefaultController implements ProductInterfac } $this->render('product/list.html.twig', [ 'products' => $products, + 'add_to_basket'=> new Link(['controller'=>'order','action'=>'basket']), 'colors' => $this->getColors(), 'menu_items'=>[$this->getColors()] ]); diff --git a/src/repository/product/Product.php b/src/repository/product/Product.php index 6469a5d..82346a0 100644 --- a/src/repository/product/Product.php +++ b/src/repository/product/Product.php @@ -18,11 +18,9 @@ use entity\image\UrlImage; final class Product extends AbstractRepository implements ProductInterface { - const TABLE = 'product'; - + const TABLE = 'product'; + /** - * Out of time reasons the values are not escaped! - * * {@inheritdoc} * @see \repository\product\ProductInterface::addProducts() */ @@ -59,7 +57,7 @@ final class Product extends AbstractRepository implements ProductInterface { $products = new ArrayCollection(); foreach ($fetch as $product) { - $products->add($this->createProduct($product['name'], $product['color'], $product['price'], $product['tax'], $product['image'])); + $products->add($this->createProduct($product['name'], $product['color'], $product['price'], $product['tax'], $product['image'],$product['id'])); } return $products; } @@ -71,7 +69,7 @@ final class Product extends AbstractRepository implements ProductInterface return $this->transformFetchToArrayCollection($statement->fetchAll()); } - static public function createProduct(string $name, string $color, int $cents, int $tax, string $imagePath): ProductEntity + static public function createProduct(string $name, string $color, int $cents, int $tax, string $imagePath,?int $id=null): ProductEntity { $product = new ProductEntity(); $product->setName($name); @@ -85,6 +83,9 @@ final class Product extends AbstractRepository implements ProductInterface $image = new UrlImage(); $image->setImage($imagePath); $product->setImage($image); + if($id){ + $product->setId($id); + } return $product; } @@ -94,4 +95,13 @@ final class Product extends AbstractRepository implements ProductInterface $statement->execute([$color]); return $this->transformFetchToArrayCollection($statement->fetchAll()); } + + public function getById(int $id): ProductEntityInterface + { + $statement = $this->database->prepare('SELECT * FROM ' . self::TABLE . ' WHERE id = ?;'); + $statement->execute([$id]); + $product = $statement->fetch(); + return $this->createProduct($product['name'], $product['color'], $product['price'], $product['tax'], $product['image'],$product['id']); + } + } \ No newline at end of file diff --git a/src/repository/product/ProductInterface.php b/src/repository/product/ProductInterface.php index f68af9c..b543881 100644 --- a/src/repository/product/ProductInterface.php +++ b/src/repository/product/ProductInterface.php @@ -2,6 +2,7 @@ namespace repository\product; use Doctrine\Common\Collections\ArrayCollection; +use entity\product\ProductInterface as EntityProductInterface; /** * @@ -17,5 +18,7 @@ interface ProductInterface public function getColors():array; public function getAllByColor(string $color):ArrayCollection; + + public function getById(int $id):EntityProductInterface; } diff --git a/src/template/order/basket.html.twig b/src/template/order/basket.html.twig index c17d05e..eecede5 100644 --- a/src/template/order/basket.html.twig +++ b/src/template/order/basket.html.twig @@ -14,11 +14,11 @@ basket {% for product in basket.products %}