<?php declare(strict_types=1);
namespace SanettaTheme\Storefront\Controller;
use SanettaTheme\Service\WritingKidsData;
use SanettaTheme\Storefront\Page\Kids\KidsPageLoader;
use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class KidsController extends StorefrontController
{
protected KidsPageLoader $pageLoader;
protected WritingKidsData $writingKidsService;
public function __construct(KidsPageLoader $pageLoader, WritingKidsData $writingKidsService)
{
$this->pageLoader = $pageLoader;
$this->writingKidsService = $writingKidsService;
}
/**
* @Route("/account/kids", name="frontend.account.kids.overview", methods={"GET"})
* @LoginRequired()
*/
public function showKids(Request $request, SalesChannelContext $context): Response
{
$page = $this->pageLoader->load($request, $context);
return $this->renderStorefront('@SanettaTheme/storefront/page/kids/overview.html.twig', [
'page' => $page
]);
}
/**
* @Route("/account/kids", name="api.account.kids.store", defaults={"XmlHttpRequest"=true}, methods={"POST"})
* @LoginRequired()
*/
public function writeKid(Request $request, SalesChannelContext $context): Response
{
try {
$payloads = $this->writingKidsService->writeData($context, $request);
} catch (\Exception $e) {
return new JsonResponse([
'status' => 'error',
'code' => 500,
'message' => $this->trans('account.kids.create.errorMessage')
], 500);
}
return new JsonResponse([
'status' => 'success',
'code' => 200,
'message' => $this->trans('account.kids.create.successMessage'),
'data' => $payloads
]);
}
/**
* @Route("/account/kids", name="frontend.account.kids.delete", defaults={"XmlHttpRequest"=true}, methods={"DELETE"})
* @LoginRequired()
*/
public function deleteKid(Request $request, SalesChannelContext $context): Response
{
$kidsToDelete = json_decode($request->getContent(), true);
try {
$this->writingKidsService->deleteKids($context->getContext(), $kidsToDelete);
} catch (\Exception $e) {
return new JsonResponse([
'status' => 'error',
'code' => 500,
'message' => $this->trans('account.kids.table.errorMessage'),
]);
}
return new JsonResponse([
'status' => 'success',
'code' => 200,
'message' => $this->trans('account.kids.table.successMessage'),
'deletedKids' => $kidsToDelete
]);
}
}