mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-22 02:01:04 +01:00
In between commit implementing order repository
This commit is contained in:
parent
3fdcd47e9c
commit
aa18f2b18e
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
35
src/repository/order/Order.php
Normal file
35
src/repository/order/Order.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace repository\order;
|
||||
|
||||
use repository\AbstractRepository;
|
||||
use entity\order\OrderInterface as OrderEntityInterface;
|
||||
use core\CoreInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
final class Order extends AbstractRepository implements OrderInterface
|
||||
{
|
||||
public function saveOrder(OrderEntityInterface $order): bool
|
||||
{
|
||||
$this->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()]);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user