Skip to content
Snippets Groups Projects
Commit dd814f6e authored by Bastien Hohler's avatar Bastien Hohler
Browse files

Controllers, 1ere version

parent aeb08e11
Branches
No related tags found
No related merge requests found
<?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
<?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
<?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
<?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
<?php <?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
...@@ -126,7 +127,7 @@ class Order ...@@ -126,7 +127,7 @@ class Order
return $this->delivered; return $this->delivered;
} }
public function setDelivered(string $delivered): bool public function setDelivered(bool $delivered): bool
{ {
$this->delivered = $delivered; $this->delivered = $delivered;
return true; return true;
...@@ -137,7 +138,7 @@ class Order ...@@ -137,7 +138,7 @@ class Order
return $this->paid; return $this->paid;
} }
public function setPaid(string $paid): bool public function setPaid(bool $paid): bool
{ {
$this->paid = $paid; $this->paid = $paid;
return true; return true;
......
...@@ -59,4 +59,9 @@ class ProductOrder ...@@ -59,4 +59,9 @@ class ProductOrder
$this->quantity = $quantity; $this->quantity = $quantity;
return true; return true;
} }
public function computePrice()
{
return $this->product->getPrice() * $this->quantity;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment