<?php
namespace App\Controller;
use App\Dto\CartItem;
use App\Dto\KimAddress;
use App\Dto\Product as ProductDto;
use App\Dto\ProductAddon;
use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductController extends AbstractController {
use ServiceTrait;
/**
* @Route(
* "/produkt/{productId}",
* name="product_details",
* requirements={"productId"="\d+"}
* )
*/
public function productsAction(int $productId): Response {
$product = $this->entityManager->getRepository(Product::class)->find($productId);
return $this->render('page/product/details.html.twig', [
'product' => $product,
]);
}
/**
* @Route(
* "/cart/add",
* name="add_to_cart",
* methods="POST"
* )
*/
public function addProductToCartAction(Request $request): JsonResponse {
/** @var string */
$content = $request->getContent();
$productDto = $this->transformProduct($content);
/** @var CartItem */
$cartItem = $this->productService->createCartItem($productDto);
$result = $this->shoppingCartService->addItem($cartItem);
if ($result) {
return new JsonResponse([
'success' => true,
'message' => 'Produkt wurde erfolgreich zum Warenkorb hinzugefügt.',
]);
}
return new JsonResponse([
'success' => $result,
'message' => 'Produkt konnte nicht zum Warenkorb hinzugefügt werden.',
]);
}
/**
* @Route(
* "/cart/remove/{productId}",
* name="remove_from_cart",
* requirements={
* "productId"="\d+"
* }
* )
*/
public function removeProductFromCartAction(int $productId): JsonResponse {
/** @var Product */
$product = $this->productService->findProduct($productId);
/** @var CartItem */
$cartItem = $this->productService->convertToCartItem($product);
$result = $this->shoppingCartService->removeItem($cartItem);
if ($result) {
return new JsonResponse([
'success' => true,
'message' => 'Produkt wurde erfolgreich aus dem Warenkorb entfernt.',
]);
}
return new JsonResponse([
'success' => $result,
'message' => 'Produkt konnte nicht aus dem Warenkorb entfernt werden.',
]);
}
/**
* @Route(
* "/cart/increase/{productId}",
* name="increase_quantity",
* requirements={
* "productId"="\d+"
* }
* )
*/
public function increaseQuantityAction(int $productId): JsonResponse {
/** @var Product */
$product = $this->productService->findProduct($productId);
/** @var CartItem */
$cartItem = $this->productService->convertToCartItem($product);
$result = $this->shoppingCartService->increaseQuantity($cartItem);
if ($result) {
return new JsonResponse([
'success' => true,
'message' => 'Die Produktmenge wurde erfolgreich erhöht.',
]);
}
return new JsonResponse([
'success' => $result,
'message' => 'Die Produktmenge konnte nicht erhöht werden.',
]);
}
/**
* @Route(
* "/cart/decrease/{productId}",
* name="decrease_quantity",
* requirements={
* "productId"="\d+"
* }
* )
*/
public function decreaseQuantityAction(int $productId): JsonResponse {
/** @var Product */
$product = $this->productService->findProduct($productId);
/** @var CartItem */
$cartItem = $this->productService->convertToCartItem($product);
$result = $this->shoppingCartService->decreaseQuantity($cartItem);
if ($result) {
return new JsonResponse([
'success' => true,
'message' => 'Die Produktmenge wurde erfolgreich verringert.',
]);
}
return new JsonResponse([
'success' => $result,
'message' => 'Die Produktmenge konnte nicht verringert werden.',
]);
}
/**
* Transforms a JSON string into a ProductDto object.
*
* @param string $product
*
* @return ProductDto
*/
private function transformProduct(string $product): ProductDto {
$product = json_decode($product);
$productDto = new ProductDto(
id: $product->id,
quantity: $product->quantity
);
foreach ($product->kim_addresses as $kimAddress) {
$kimAddress = new KimAddress(
address: $kimAddress->address,
domain: $kimAddress->domain,
isIncluded: $kimAddress->included,
price: $kimAddress->price,
);
$productDto->addKimAddress($kimAddress);
}
foreach ($product->product_addons as $productAddon) {
$productAddon = new ProductAddon(
itemNumber: $productAddon->item_number,
quantity: $productAddon->quantity,
price: $productAddon->price,
);
$productDto->addProductAddon($productAddon);
}
return $productDto;
}
}