-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #13280 Get products by slug api endpoint (paullla)
This PR was merged into the 1.11-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | mentioned in #12453 | License | MIT Commits ------- 5e40d5b Get product by slug api endpoint
- Loading branch information
Showing
8 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Sylius/Bundle/ApiBundle/Controller/GetProductBySlugAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Controller; | ||
|
||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class GetProductBySlugAction | ||
{ | ||
private ChannelContextInterface $channelContext; | ||
private LocaleContextInterface $localeContext; | ||
private ProductRepositoryInterface $productRepository; | ||
private IriConverterInterface $iriConverter; | ||
private RequestStack $requestStack; | ||
|
||
public function __construct( | ||
ChannelContextInterface $channelContext, | ||
LocaleContextInterface $localeContext, | ||
ProductRepositoryInterface $productRepository, | ||
IriConverterInterface $iriConverter, | ||
RequestStack $requestStack | ||
) { | ||
$this->channelContext = $channelContext; | ||
$this->localeContext = $localeContext; | ||
$this->productRepository = $productRepository; | ||
$this->iriConverter = $iriConverter; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function __invoke(string $slug): RedirectResponse | ||
{ | ||
$channel = $this->channelContext->getChannel(); | ||
$locale = $this->localeContext->getLocaleCode(); | ||
|
||
$product = $this->productRepository->findOneByChannelAndSlug($channel, $locale, $slug); | ||
|
||
if (null === $product) { | ||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
$iri = $this->iriConverter->getIriFromItem($product); | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
$requestQuery = $request->getQueryString(); | ||
if (null !== $requestQuery) { | ||
$iri .= sprintf('?%s', $requestQuery); | ||
} | ||
|
||
return new RedirectResponse($iri, Response::HTTP_MOVED_PERMANENTLY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Sylius/Bundle/ApiBundle/Swagger/ProductSlugDocumentationNormalizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Swagger; | ||
|
||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** @experimental */ | ||
final class ProductSlugDocumentationNormalizer implements NormalizerInterface | ||
{ | ||
private const PRODUCT_SLUG_PATH = '/api/v2/shop/products-by-slug/{slug}'; | ||
|
||
private NormalizerInterface $decoratedNormalizer; | ||
|
||
public function __construct(NormalizerInterface $decoratedNormalizer) | ||
{ | ||
$this->decoratedNormalizer = $decoratedNormalizer; | ||
} | ||
|
||
public function supportsNormalization($data, $format = null): bool | ||
{ | ||
return $this->decoratedNormalizer->supportsNormalization($data, $format); | ||
} | ||
|
||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
$docs = $this->decoratedNormalizer->normalize($object, $format, $context); | ||
|
||
$params = $docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters']; | ||
|
||
foreach ($params as $index => $param) { | ||
if ($param['name'] === 'code') { | ||
unset($docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters'][$index]); | ||
} | ||
} | ||
|
||
return $docs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Tests\Api\Shop; | ||
|
||
use Sylius\Tests\Api\JsonApiTestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class ProductsTest extends JsonApiTestCase | ||
{ | ||
/** @test */ | ||
public function it_preserves_query_param_when_redirecting_from_product_slug_to_product_code(): void | ||
{ | ||
$this->loadFixturesFromFile('product_variant_with_original_price.yaml'); | ||
|
||
$this->client->request('GET', '/api/v2/shop/products-by-slug/mug?paramName=paramValue', [], [], self::CONTENT_TYPE_HEADER); | ||
$response = $this->client->getResponse(); | ||
|
||
$this->assertEquals('/api/v2/shop/products/MUG?paramName=paramValue', $response->headers->get(('Location'))); | ||
$this->assertResponseCode($response, Response::HTTP_MOVED_PERMANENTLY); | ||
} | ||
} |