custom/plugins/DmSanettaTheme/src/Storefront/Controller/KidsController.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SanettaTheme\Storefront\Controller;
  3. use SanettaTheme\Service\WritingKidsData;
  4. use SanettaTheme\Storefront\Page\Kids\KidsPageLoader;
  5. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Controller\StorefrontController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @RouteScope(scopes={"storefront"})
  15.  */
  16. class KidsController extends StorefrontController
  17. {
  18.     protected KidsPageLoader $pageLoader;
  19.     protected WritingKidsData $writingKidsService;
  20.     public function __construct(KidsPageLoader $pageLoaderWritingKidsData $writingKidsService)
  21.     {
  22.         $this->pageLoader $pageLoader;
  23.         $this->writingKidsService $writingKidsService;
  24.     }
  25.     /**
  26.      * @Route("/account/kids", name="frontend.account.kids.overview", methods={"GET"})
  27.      * @LoginRequired()
  28.      */
  29.     public function showKids(Request $requestSalesChannelContext $context): Response
  30.     {
  31.         $page $this->pageLoader->load($request$context);
  32.         return $this->renderStorefront('@SanettaTheme/storefront/page/kids/overview.html.twig', [
  33.             'page' => $page
  34.         ]);
  35.     }
  36.     /**
  37.      * @Route("/account/kids", name="api.account.kids.store", defaults={"XmlHttpRequest"=true}, methods={"POST"})
  38.      * @LoginRequired()
  39.      */
  40.     public function writeKid(Request $requestSalesChannelContext $context): Response
  41.     {
  42.         try {
  43.             $payloads $this->writingKidsService->writeData($context$request);
  44.         } catch (\Exception $e) {
  45.             return new JsonResponse([
  46.                 'status'  => 'error',
  47.                 'code'    => 500,
  48.                 'message' => $this->trans('account.kids.create.errorMessage')
  49.             ], 500);
  50.         }
  51.         return new JsonResponse([
  52.             'status'  => 'success',
  53.             'code'    => 200,
  54.             'message' => $this->trans('account.kids.create.successMessage'),
  55.             'data'    => $payloads
  56.         ]);
  57.     }
  58.     /**
  59.      * @Route("/account/kids", name="frontend.account.kids.delete", defaults={"XmlHttpRequest"=true}, methods={"DELETE"})
  60.      * @LoginRequired()
  61.      */
  62.     public function deleteKid(Request $requestSalesChannelContext $context): Response
  63.     {
  64.         $kidsToDelete json_decode($request->getContent(), true);
  65.         try {
  66.             $this->writingKidsService->deleteKids($context->getContext(), $kidsToDelete);
  67.         } catch (\Exception $e) {
  68.             return new JsonResponse([
  69.                 'status'  => 'error',
  70.                 'code'    => 500,
  71.                 'message' => $this->trans('account.kids.table.errorMessage'),
  72.             ]);
  73.         }
  74.         return new JsonResponse([
  75.             'status'      => 'success',
  76.             'code'        => 200,
  77.             'message'     => $this->trans('account.kids.table.successMessage'),
  78.             'deletedKids' => $kidsToDelete
  79.         ]);
  80.     }
  81. }