diff --git a/src/Controllers/CategoryController.php b/src/Controllers/CategoryController.php new file mode 100644 index 0000000000000000000000000000000000000000..896b604e6da1e291b75d8c50cb6d67bf0af388f1 --- /dev/null +++ b/src/Controllers/CategoryController.php @@ -0,0 +1,48 @@ +<?php + +use Doctrine\ORM\EntityManager; + +require_once '/database/models/Category'; + +class CategoryController { + + /** + * @var EntityManager + */ + private $em; + + public function __construct(EntityManager $em) + { + $this->em = $em; + } + + public function getById(int $id): Category + { + return $this->em->find('Category',$id); + } + + public function getAll() + { + return $this->em->getRepository(Category::class)->findAll(); + } + + public function createCategory(array $args) + { + $category = new Category(); + $category->setName($args['name']); + $category->setDescription($args['description']); + $this->em->persist($category); + $this->em->flush(); + } + + public function deleteCategory(int $id): bool + { + if ($category = $this->getById($id) != null) { + $this->em->remove($category); + $this->em->flush(); + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/src/Controllers/OrderController.php b/src/Controllers/OrderController.php new file mode 100644 index 0000000000000000000000000000000000000000..e21d67e67d6edecfd374524ee796f0263b179256 --- /dev/null +++ b/src/Controllers/OrderController.php @@ -0,0 +1,62 @@ +<?php + +use Doctrine\ORM\EntityManager; + +require_once '/database/models/Order'; + +class OrderController { + + /** + * @var EntityManager + */ + private $em; + + public function __construct(EntityManager $em) + { + $this->em = $em; + } + + public function getById(int $id): Order + { + return $this->em->find('Order',$id); + } + + public function getAll() + { + return $this->em->getRepository(Order::class)->findAll(); + } + + public function createOrder(array $args) + { + $order = new Order(); + $order->setClientName($args['name']); + $order->setClientAdress($args['adress']); + $order->setClientEmail($args['email']); + $order->setClientPhone($args['phone']); + $order->setPaid(false); + $order->setDelivered(false); + $this->em->persist($order); + $this->em->flush(); + } + + public function computeTotalPrice($id) { + $order = $this->getById($id); + $pos = $order->getProductOrders(); + $total_price = 0; + foreach ($pos as $po) { + $total_price+=$po->getQuantity() * $po->getProduct()->getPrice(); + } + $order->setTotalPrice($total_price); + + } + public function deleteOrder(int $id): bool + { + if ($order = $this->getById($id) != null) { + $this->em->remove($order); + $this->em->flush(); + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/src/Controllers/ProducerController.php b/src/Controllers/ProducerController.php new file mode 100644 index 0000000000000000000000000000000000000000..57e1c1fcd82d5e989f2a7a265ccc1e050e3671c0 --- /dev/null +++ b/src/Controllers/ProducerController.php @@ -0,0 +1,50 @@ +<?php + +use Doctrine\ORM\EntityManager; + +require_once '/database/models/Producer'; + +class OrderController { + + /** + * @var EntityManager + */ + private $em; + + public function __construct(EntityManager $em) + { + $this->em = $em; + } + + public function getById(int $id): Order + { + return $this->em->find('Producer',$id); + } + + public function getAll() + { + return $this->em->getRepository(Producer::class)->findAll(); + } + + public function createProducer(array $args) + { + $producer = new Producer(); + $producer->setName($args['name']); + $producer->setAdress($args['adress']); + $producer->setEmail($args['email']); + $producer->setPhone($args['phone']); + $this->em->persist($producer); + $this->em->flush(); + } + + public function deleteProducer(int $id): bool + { + if ($producer = $this->getById($id) != null) { + $this->em->remove($producer); + $this->em->flush(); + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/src/Controllers/ProductController.php b/src/Controllers/ProductController.php new file mode 100644 index 0000000000000000000000000000000000000000..c0c7156d5cd56c3b207e3c5b9530fdd1f4b9ab49 --- /dev/null +++ b/src/Controllers/ProductController.php @@ -0,0 +1,28 @@ +<?php + +use Doctrine\ORM\EntityManager; + +require_once '/database/models/Product'; + +class ProductController { + + /** + * @var EntityManager + */ + private $em; + + public function __construct(EntityManager $em) + { + $this->em = $em; + } + + public function getById(int $id): Product + { + return $this->em->find('Product',$id); + } + + public function getAll() + { + return $this->em->getRepository(Product::class)->findAll(); + } +} \ No newline at end of file diff --git a/src/database/models/Order.php b/src/database/models/Order.php index eb53693996457d285c7b5436c48ca347b585f432..949b71873c758fdff75f6e928c7f480de2a3638c 100644 --- a/src/database/models/Order.php +++ b/src/database/models/Order.php @@ -127,7 +127,7 @@ class Order return $this->delivered; } - public function setDelivered(string $delivered): bool + public function setDelivered(bool $delivered): bool { $this->delivered = $delivered; return true; @@ -138,7 +138,7 @@ class Order return $this->paid; } - public function setPaid(string $paid): bool + public function setPaid(bool $paid): bool { $this->paid = $paid; return true; diff --git a/src/database/models/ProductOrder.php b/src/database/models/ProductOrder.php index 04876c80f89fe77e0b1f2207e62f9d04e799d710..95df31528ef10b552809816c073ac9abb77b3d0e 100644 --- a/src/database/models/ProductOrder.php +++ b/src/database/models/ProductOrder.php @@ -59,4 +59,9 @@ class ProductOrder $this->quantity = $quantity; return true; } + + public function computePrice() + { + return $this->product->getPrice() * $this->quantity; + } }