diff --git a/src/controller/order/Order.php b/src/controller/order/Order.php index c0c8f72..3c3158f 100644 --- a/src/controller/order/Order.php +++ b/src/controller/order/Order.php @@ -7,6 +7,8 @@ use repository\order\OrderInterface as OrderRepositoryInterface; use repository\product\ProductInterface as ProductRepositoryInterface; use repository\product\Product as ProductRepository; use entity\payment\AbstractPayment; +use repository\order\Order as OrderRepository; +use entity\order\Order as OrderEntity; /** * @@ -32,6 +34,7 @@ final class Order extends AbstractDefaultController implements OrderInterface { parent::__construct($core); $this->productRepository = new ProductRepository($this->core); + $this->orderRepository = new OrderRepository($this->core); } private function addProduct(): void @@ -58,6 +61,12 @@ final class Order extends AbstractDefaultController implements OrderInterface if ($this->post['add']) { $this->addProduct(); } + if ($this->post['store']){ + $this->core->getBasket()->setCustomer($this->core->getUser()); + if($this->orderRepository->saveOrder($this->core->getBasket())){ + $this->core->setBasket(new OrderEntity()); + } + } } } diff --git a/src/repository/order/Order.php b/src/repository/order/Order.php new file mode 100644 index 0000000..acfc797 --- /dev/null +++ b/src/repository/order/Order.php @@ -0,0 +1,35 @@ +database->beginTransaction(); + $this->saveOrderEntity($order); + $this->saveConnection($order); + return $this->database->rollBack(); + } + + private function saveOrderEntity(OrderEntityInterface $order):void{ + $statement = $this->database->prepare("INSERT INTO `order` (`customer`) VALUES (?);"); + $statement->execute([$order->getCustomer()->getId()]); + $order->setId($this->database->lastInsertId()); + } + + private function saveConnection(OrderEntityInterface $order):void{ + foreach ($order->getProducts()->toArray() as $product){ + $statement = $this->database->prepare("INSERT INTO `order_product` (`product_id`,`order_id`) VALUES (?,?);"); + $statement->execute([$product->getId(),$order->getId()]); + } + } +} \ No newline at end of file diff --git a/src/repository/order/OrderInterface.php b/src/repository/order/OrderInterface.php index 4e0222e..0f1b49b 100644 --- a/src/repository/order/OrderInterface.php +++ b/src/repository/order/OrderInterface.php @@ -10,11 +10,6 @@ use entity\order\OrderInterface as OrderEntityInterface; */ interface OrderInterface { - public function saveOrder(OrderEntityInterface $order):void; - - /** - * This function just exists for maintaining reasons - */ - public function deleteAllOrders():void; + public function saveOrder(OrderEntityInterface $order):bool; }