This repository was archived by the owner on Oct 8, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 91
add simple product name and description search #564
Open
CSchulz
wants to merge
2
commits into
Sylius:1.5
Choose a base branch
from
CSchulz:product-search
base: 1.5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
src/Controller/Product/ShowProductCatalogByPhraseAction.php
This file contains hidden or 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,59 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\ShopApiPlugin\Controller\Product; | ||
|
|
||
| use FOS\RestBundle\View\View; | ||
| use FOS\RestBundle\View\ViewHandlerInterface; | ||
| use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
| use Sylius\Component\Channel\Context\ChannelNotFoundException; | ||
| use Sylius\ShopApiPlugin\Model\PaginatorDetails; | ||
| use Sylius\ShopApiPlugin\ViewRepository\Product\ProductCatalogViewRepositoryInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
|
||
| final class ShowProductCatalogByPhraseAction | ||
| { | ||
| /** @var ViewHandlerInterface */ | ||
| private $viewHandler; | ||
|
|
||
| /** @var ProductCatalogViewRepositoryInterface */ | ||
| private $productCatalogViewRepository; | ||
|
|
||
| /** @var ChannelContextInterface */ | ||
| private $channelContext; | ||
|
|
||
| public function __construct( | ||
| ViewHandlerInterface $viewHandler, | ||
| ProductCatalogViewRepositoryInterface $productLatestQuery, | ||
| ChannelContextInterface $channelContext | ||
| ) { | ||
| $this->viewHandler = $viewHandler; | ||
| $this->productCatalogViewRepository = $productLatestQuery; | ||
| $this->channelContext = $channelContext; | ||
| } | ||
|
|
||
| public function __invoke(Request $request): Response | ||
| { | ||
| try { | ||
| $channel = $this->channelContext->getChannel(); | ||
| $phrase = $request->attributes->get('phrase'); | ||
| $queryParameters = $request->query->all(); | ||
| $queryParameters['phrase'] = $phrase; | ||
|
|
||
| return $this->viewHandler->handle(View::create($this->productCatalogViewRepository->findByPhrase( | ||
| $phrase, | ||
| $channel->getCode(), | ||
| new PaginatorDetails($request->attributes->get('_route'), $queryParameters), | ||
| $request->query->getBoolean('includeDescription', false), | ||
| $request->query->get('locale') | ||
| ), Response::HTTP_OK)); | ||
| } catch (ChannelNotFoundException $exception) { | ||
| throw new NotFoundHttpException('Channel has not been found.'); | ||
| } catch (\InvalidArgumentException $exception) { | ||
| throw new NotFoundHttpException($exception->getMessage()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,7 @@ | ||
| sylius_shop_api_product_show_by_phrase: | ||
| path: /products/by-phrase/{phrase} | ||
| methods: [GET] | ||
| defaults: | ||
| _controller: sylius.shop_api_plugin.controller.product.show_product_catalog_by_phrase_action | ||
| requirements: | ||
| phrase: .+ |
This file contains hidden or 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 hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
tests/Controller/Product/ShowCatalogByPhraseApiTest.php
This file contains hidden or 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,100 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Sylius\ShopApiPlugin\Controller\Product; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase; | ||
|
|
||
| final class ShowCatalogByPhraseApiTest extends JsonApiTestCase | ||
| { | ||
| /** | ||
| * @test | ||
| */ | ||
| public function it_shows_paginated_products_for_phrase(): void | ||
| { | ||
| $this->loadFixturesFromFiles(['shop.yml']); | ||
|
|
||
| $this->client->request('GET', '/shop-api/products/by-phrase/mug', [], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/product_list_page_by_phrase_mug_response', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_shows_empty_products_list_for_non_existing_products(): void | ||
| { | ||
| $this->loadFixturesFromFiles(['shop.yml']); | ||
|
|
||
| $this->client->request('GET', '/shop-api/products/by-phrase/unknown', [], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/product_list_page_by_phrase_empty_response', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_shows_paginated_products_for_phrase_including_description(): void | ||
| { | ||
| $this->loadFixturesFromFiles(['shop.yml']); | ||
|
|
||
| $this->client->request('GET', '/shop-api/products/by-phrase/Lorem', ['includeDescription' => true], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/product_list_page_by_phrase_mug_including_description_response', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_shows_paginated_products_for_phrase_including_short_description(): void | ||
| { | ||
| $this->loadFixturesFromFiles(['shop.yml']); | ||
|
|
||
| $this->client->request('GET', '/shop-api/products/by-phrase/short', ['includeDescription' => true], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/product_list_page_by_phrase_mug_including_short_description_response', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_shows_paginated_products_for_phrase_in_different_language(): void | ||
| { | ||
| $this->loadFixturesFromFiles(['shop.yml']); | ||
|
|
||
| $this->client->request('GET', '/shop-api/products/by-phrase/becher?locale=de_DE', [], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/product_list_page_by_phrase_mug_in_german_response', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_shows_paginated_products_for_phrase_in_different_language_including_description(): void | ||
| { | ||
| $this->loadFixturesFromFiles(['shop.yml']); | ||
|
|
||
| $this->client->request('GET', '/shop-api/products/by-phrase/Beschreibung?locale=de_DE', ['includeDescription' => true], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/product_list_page_by_phrase_mug_in_german_including_description_response', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_throws_a_not_found_exception_if_channel_has_not_been_found(): void | ||
| { | ||
| $this->client->request('GET', '/shop-api/products/by-phrase/mug', [], [], self::CONTENT_TYPE_HEADER); | ||
| $response = $this->client->getResponse(); | ||
|
|
||
| $this->assertResponse($response, 'product/channel_has_not_been_found_response', Response::HTTP_NOT_FOUND); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
tests/Responses/Expected/product/product_list_page_by_phrase_empty_response.json
This file contains hidden or 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,13 @@ | ||
| { | ||
| "page": 1, | ||
| "limit": 10, | ||
| "pages": 1, | ||
| "total": 0, | ||
| "_links": { | ||
| "self": "/shop-api/products/by-phrase/unknown?page=1&limit=10", | ||
| "first": "/shop-api/products/by-phrase/unknown?page=1&limit=10", | ||
| "last": "/shop-api/products/by-phrase/unknown?page=1&limit=10", | ||
| "next": "/shop-api/products/by-phrase/unknown?page=1&limit=10" | ||
| }, | ||
| "items": [] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.