<?php
namespace App\Controller\Front\Wishlist;
use App\Application\Service\Session\SessionService;
use App\Application\Service\Wishlist\ProductAttributeMapService;
use App\Application\Service\Wishlist\RetrieveWishlistWithProductsService;
use App\Application\Service\Wishlist\WishlistProductService;
use App\Application\Service\Wishlist\WishlistService;
use App\Entity\System\Customer;
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;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/wishlists", name="wishlists_")
*/
class WishlistController extends AbstractController
{
public function __construct(
private readonly WishlistService $wishlistCreateService,
private readonly SerializerInterface $serializer,
private readonly SessionService $sessionService,
) {
}
/**
* @Route("/simple-product/{productId<\d+>}", name="add_simple_product", methods={"POST"})
*/
public function addSimpleProductsToWishlists(Request $request, int $productId, WishlistProductService $wishlistProductService): JsonResponse
{
/** @var Customer|null $customer */
$customer = $this->getUser();
if (!$customer) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}
$requestData = json_decode($request->getContent(), true);
try {
$wishlistProductService->setSimpleProductWishlists($customer, $productId, $requestData);
} catch (\Exception $exception) {
return new JsonResponse(['code' => Response::HTTP_CONFLICT, 'message' => $exception->getMessage()], Response::HTTP_CONFLICT);
}
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}
/**
* @Route("/variation-product/{productAttributeId<\d+>}", name="add_variation_product", methods={"POST"})
*/
public function addVariationProductsToWishlists(Request $request, int $productAttributeId, WishlistProductService $wishlistProductService): JsonResponse
{
/** @var Customer|null $customer */
$customer = $this->getUser();
if (!$customer) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}
$requestData = json_decode($request->getContent(), true);
try {
$wishlistProductService->setProductVariationWishlists($customer, $productAttributeId, $requestData);
} catch (\Exception $e) {
return new JsonResponse(['code' => Response::HTTP_CONFLICT, 'message' => $e->getMessage()], Response::HTTP_CONFLICT);
}
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}
/**
* @Route("/wishlists-with-products/{productId<\d+>}", name="get_wishlists_with_products", methods={"GET"})
*/
public function retrieveWishlistsWithProducts(
RetrieveWishlistWithProductsService $retrieveWishlistWithProductsService,
int $productId,
): JsonResponse {
/** @var Customer|null $customer */
$customer = $this->getUser();
if (!$customer) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}
try {
$wishlistWithProducts = $retrieveWishlistWithProductsService->retrieveWishlistWithProductsByCustomer($customer->getId(), $productId);
} catch (\Exception $exception) {
return new JsonResponse(null, Response::HTTP_NOT_FOUND);
}
return new JsonResponse($this->serializer->serialize($wishlistWithProducts, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
}
/**
* @Route("/create-custom-wishlist", name="create_new_custom_wishlist", methods={"POST"})
*/
public function create(Request $request): JsonResponse
{
/** @var Customer|null $loggedUser */
$loggedUser = $this->getUser();
if (!$loggedUser) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}
$content = $request->getContent();
$data = json_decode($content, true);
try {
$wishlistName = trim($data['name']);
$newWishlist = $this->wishlistCreateService->create($loggedUser, $wishlistName);
$serializedWishlist = $this->serializer->serialize($newWishlist, JsonEncoder::FORMAT);
} catch (\Exception $e) {
return new JsonResponse(['code' => Response::HTTP_CONFLICT, 'message' => $e->getMessage()], Response::HTTP_CONFLICT);
}
return new JsonResponse($serializedWishlist, Response::HTTP_CREATED, [], true);
}
/**
* @Route("/attribute-map/{productId<\d*>}", name="attribute_map")
*/
public function attributeMap(int $productId, ProductAttributeMapService $productAttributeMapService): Response
{
$attributeMap = $productAttributeMapService->getMapForProductId($productId, $this->sessionService->getLocaleId());
return new JsonResponse($this->serializer->serialize($attributeMap, 'json'), Response::HTTP_OK, [], true);
}
}