Implemented color filter

This commit is contained in:
Kevin Frantz 2018-07-15 00:06:56 +02:00
parent 1bc7219caa
commit 401fa64733
7 changed files with 75 additions and 25 deletions

View File

@ -3,6 +3,8 @@ namespace controller\product;
use controller\AbstractController; use controller\AbstractController;
use repository\product\Product as ProductRepository; use repository\product\Product as ProductRepository;
use core\Core;
use router\Link;
/** /**
* *
@ -11,14 +13,41 @@ use repository\product\Product as ProductRepository;
*/ */
final class Product extends AbstractController implements ProductInterface final class Product extends AbstractController implements ProductInterface
{ {
public function list(): void
/**
*
* @var ProductRepository
*/
protected $productRepository;
public function __construct(Core $core)
{ {
$productRepository = new ProductRepository($this->core->getDatabase()); parent::__construct($core);
$this->render('product/list.html.twig',['products'=>$productRepository->getAllProducts()->toArray()]); $this->productRepository = new ProductRepository($this->core->getDatabase());
} }
public function colorFilter(string $color): void public function list(?string $color = null): void
{} {
if ($color) {
$products = $this->productRepository->getAllByColor($color)->toArray();
} else {
$products = $this->productRepository->getAllProducts()->toArray();
}
$this->render('product/list.html.twig', [
'products' => $products,
'colors' => $this->getColors()
]);
}
private function getColors(): array
{
$colors = [];
foreach ($this->productRepository->getColors() as $color) {
$parameters = $_GET;
$parameters['color'] = $color['color'];
$colors[] = new Link($parameters, $color['color']);
}
return $colors;
}
} }

View File

@ -7,13 +7,6 @@ namespace controller\product;
*/ */
interface ProductInterface interface ProductInterface
{ {
/** public function list(?string $color = null):void;
* An own Class filter would be better,
* but it's to abstract for this concrete exampl ;)
* @param string $color
*/
public function colorFilter(string $color):void;
public function list():void;
} }

View File

@ -48,15 +48,27 @@ final class Product extends AbstractRepository implements ProductInterface
} }
} }
public function getColors(): array
{
$statement = $this->database->prepare('SELECT DISTINCT color FROM product;');
$statement->execute();
return $statement->fetchAll();
}
private function transformFetchToArrayCollection(array $fetch): ArrayCollection
{
$products = new ArrayCollection();
foreach ($fetch as $product) {
$products->add($this->createProduct($product['name'], $product['color'], $product['price'], $product['tax'], $product['image']));
}
return $products;
}
public function getAllProducts(): ArrayCollection public function getAllProducts(): ArrayCollection
{ {
$statement = $this->database->prepare('SELECT * FROM ' . self::TABLE . ';'); $statement = $this->database->prepare('SELECT * FROM ' . self::TABLE . ';');
$statement->execute(); $statement->execute();
$products = new ArrayCollection(); return $this->transformFetchToArrayCollection($statement->fetchAll());
foreach ($statement->fetchAll() as $product) {
$products->add($this->createProduct($product['name'], $product['color'], $product['price'], $product['tax'], $product['image']));
}
return $products;
} }
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): ProductEntity
@ -76,6 +88,10 @@ final class Product extends AbstractRepository implements ProductInterface
return $product; return $product;
} }
public function getProductById(int $id): ProductEntityInterface public function getAllByColor(string $color): ArrayCollection
{} {
$statement = $this->database->prepare('SELECT * FROM ' . self::TABLE . ' WHERE color = ?;');
$statement->execute([$color]);
return $this->transformFetchToArrayCollection($statement->fetchAll());
}
} }

View File

@ -2,7 +2,7 @@
namespace repository\product; namespace repository\product;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use entity\product\ProductInterface as ProductEntityInterface;
/** /**
* *
* @author kevinfrantz * @author kevinfrantz
@ -14,6 +14,8 @@ interface ProductInterface
public function addProducts(ArrayCollection $products):void; public function addProducts(ArrayCollection $products):void;
public function getProductById(int $id):ProductEntityInterface; public function getColors():array;
public function getAllByColor(string $color):ArrayCollection;
} }

View File

@ -53,9 +53,7 @@ final class Router implements RouterInterface
$productController = new Product($this->core); $productController = new Product($this->core);
switch ($this->get['action']) { switch ($this->get['action']) {
case 'list': case 'list':
return $productController->list(); return $productController->list(($this->get['color'])?$this->get['color']:null);
case 'color':
return $productController->colorFilter($this->get['color']);
} }
case 'order': case 'order':
$orderController = new Order($this->core); $orderController = new Order($this->core);

View File

@ -3,6 +3,7 @@
<div class="container"> <div class="container">
<div class="page-header"> <div class="page-header">
<h1>Online Shop</h1> <h1>Online Shop</h1>
<hr />
</div> </div>
{% block content %} {% block content %}
{% endblock %} {% endblock %}

View File

@ -1,6 +1,17 @@
{% extends 'frames/default.html.twig' %} {% extends 'frames/default.html.twig' %}
{% block title %}product overview{% endblock %} {% block title %}product overview{% endblock %}
{% block content %} {% block content %}
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
filter by color:
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
{% for color in colors %}
<a class="dropdown-item" href="{{ color.getUrl }}">{{ color.getName }}</a>
{% endfor %}
</div>
</div>
<hr />
<div class="card-columns"> <div class="card-columns">
{% for product in products %} {% for product in products %}
<div class="card" style="width: 18rem;"> <div class="card" style="width: 18rem;">