src/Controller/Front/Wishlist/WishlistController.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\Wishlist;
  3. use App\Application\Service\Session\SessionService;
  4. use App\Application\Service\Wishlist\ProductAttributeMapService;
  5. use App\Application\Service\Wishlist\RetrieveWishlistWithProductsService;
  6. use App\Application\Service\Wishlist\WishlistProductService;
  7. use App\Application\Service\Wishlist\WishlistService;
  8. use App\Entity\System\Customer;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. /**
  17. * @Route("/wishlists", name="wishlists_")
  18. */
  19. class WishlistController extends AbstractController
  20. {
  21. public function __construct(
  22. private readonly WishlistService $wishlistCreateService,
  23. private readonly SerializerInterface $serializer,
  24. private readonly SessionService $sessionService,
  25. ) {
  26. }
  27. /**
  28. * @Route("/simple-product/{productId<\d+>}", name="add_simple_product", methods={"POST"})
  29. */
  30. public function addSimpleProductsToWishlists(Request $request, int $productId, WishlistProductService $wishlistProductService): JsonResponse
  31. {
  32. /** @var Customer|null $customer */
  33. $customer = $this->getUser();
  34. if (!$customer) {
  35. return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
  36. }
  37. $requestData = json_decode($request->getContent(), true);
  38. try {
  39. $wishlistProductService->setSimpleProductWishlists($customer, $productId, $requestData);
  40. } catch (\Exception $exception) {
  41. return new JsonResponse(['code' => Response::HTTP_CONFLICT, 'message' => $exception->getMessage()], Response::HTTP_CONFLICT);
  42. }
  43. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  44. }
  45. /**
  46. * @Route("/variation-product/{productAttributeId<\d+>}", name="add_variation_product", methods={"POST"})
  47. */
  48. public function addVariationProductsToWishlists(Request $request, int $productAttributeId, WishlistProductService $wishlistProductService): JsonResponse
  49. {
  50. /** @var Customer|null $customer */
  51. $customer = $this->getUser();
  52. if (!$customer) {
  53. return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
  54. }
  55. $requestData = json_decode($request->getContent(), true);
  56. try {
  57. $wishlistProductService->setProductVariationWishlists($customer, $productAttributeId, $requestData);
  58. } catch (\Exception $e) {
  59. return new JsonResponse(['code' => Response::HTTP_CONFLICT, 'message' => $e->getMessage()], Response::HTTP_CONFLICT);
  60. }
  61. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  62. }
  63. /**
  64. * @Route("/wishlists-with-products/{productId<\d+>}", name="get_wishlists_with_products", methods={"GET"})
  65. */
  66. public function retrieveWishlistsWithProducts(
  67. RetrieveWishlistWithProductsService $retrieveWishlistWithProductsService,
  68. int $productId,
  69. ): JsonResponse {
  70. /** @var Customer|null $customer */
  71. $customer = $this->getUser();
  72. if (!$customer) {
  73. return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
  74. }
  75. try {
  76. $wishlistWithProducts = $retrieveWishlistWithProductsService->retrieveWishlistWithProductsByCustomer($customer->getId(), $productId);
  77. } catch (\Exception $exception) {
  78. return new JsonResponse(null, Response::HTTP_NOT_FOUND);
  79. }
  80. return new JsonResponse($this->serializer->serialize($wishlistWithProducts, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  81. }
  82. /**
  83. * @Route("/create-custom-wishlist", name="create_new_custom_wishlist", methods={"POST"})
  84. */
  85. public function create(Request $request): JsonResponse
  86. {
  87. /** @var Customer|null $loggedUser */
  88. $loggedUser = $this->getUser();
  89. if (!$loggedUser) {
  90. return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
  91. }
  92. $content = $request->getContent();
  93. $data = json_decode($content, true);
  94. try {
  95. $wishlistName = trim($data['name']);
  96. $newWishlist = $this->wishlistCreateService->create($loggedUser, $wishlistName);
  97. $serializedWishlist = $this->serializer->serialize($newWishlist, JsonEncoder::FORMAT);
  98. } catch (\Exception $e) {
  99. return new JsonResponse(['code' => Response::HTTP_CONFLICT, 'message' => $e->getMessage()], Response::HTTP_CONFLICT);
  100. }
  101. return new JsonResponse($serializedWishlist, Response::HTTP_CREATED, [], true);
  102. }
  103. /**
  104. * @Route("/attribute-map/{productId<\d*>}", name="attribute_map")
  105. */
  106. public function attributeMap(int $productId, ProductAttributeMapService $productAttributeMapService): Response
  107. {
  108. $attributeMap = $productAttributeMapService->getMapForProductId($productId, $this->sessionService->getLocaleId());
  109. return new JsonResponse($this->serializer->serialize($attributeMap, 'json'), Response::HTTP_OK, [], true);
  110. }
  111. }