# API Route zu erstellen sehr schwer, trotz Docu

**URL:** <https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415>\
**Category:** Programmierung\
**Created:** [27. September 2022 um 17:57 UTC](https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415 "2022-09-27T17:57:49Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![shopwarer1638](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@shopwarer1638](https://forum.shopware.com/u/shopwarer1638)\
**Post date:** [2. April 2023 um 19:13 UTC](https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415/21 "2023-04-02T19:13:04Z")

</div>

Mit `shopware.entity.definition` habe ich ausprobiert, wie es funktioniert (ich habe es irgendwo in der Dokumentation gefunden).

Ich habe es entfernt und habe jetzt eine Schleife:  
ERR\_TOO\_MANY\_REDIRECTS

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [3. April 2023 um 05:30 UTC](https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415/22 "2023-04-03T05:30:53Z")

</div>

Was ist denn dein `ProductCountRoute` für ein Service? Wenn es keine Custom Entity ist, dann hat der Tag da nichts verloren.

Mit dem wenigen was man an Information von dir hat, lässt sich auch nur wenig dazu sagen.

---

<div class="post-metadata">

**Author:** ![shopwarer1638](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@shopwarer1638](https://forum.shopware.com/u/shopwarer1638)\
**Post date:** [3. April 2023 um 13:20 UTC](https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415/23 "2023-04-03T13:20:56Z")

</div>

Hier ist es:

```auto
<?php declare(strict_types=1);

namespace CustomFilter\Core\Content\Example\SalesChannel;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;

abstract class AbstractProductCountRoute
{
    abstract public function getDecorated(): AbstractProductCountRoute;

    abstract public function load(Criteria $criteria, SalesChannelContext $context): ProductCountRouteResponse;
}

```

und:

```auto
<?php declare(strict_types=1);

namespace CustomFilter\Core\Content\Example\SalesChannel;

use OpenApi\Annotations as OA;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route(defaults={"_routeScope"={"store-api"}})
 */
class ProductCountRoute extends AbstractProductCountRoute
{
    protected EntityRepository $productRepository;

    public function __construct(EntityRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function getDecorated(): AbstractProductCountRoute
    {
        throw new DecorationPatternException(self::class);
    }

    /**
     * @Entity("swag_get_active_product_count")
     * @OA\Post(
     * path="/get-active-product-count",
     * summary="This route can be used to get the count of all active products",
     * operationId="readProductCount",
     * tags={"Store API", "productCount"},
     * @OA\Parameter(name="Api-Basic-Parameters"),
     * @OA\Response(
     * response="200",
     * description="",
     * @OA\JsonContent(type="object",
     * @OA\Property(
     * property="productCount",
     * type="integer",
     * description="Total amount"
     * )
     * )
     * )
     * )
     * @Route("/store-api/get-active-product-count", name="store-api.product-count.get", methods={"GET", "POST"})
     */
    public function load(Criteria $criteria, SalesChannelContext $context): ProductCountRouteResponse
    {
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('product.active', true));
        $criteria->addAggregation(new CountAggregation('productCount', 'product.id'));

        /** @var CountResult $productCountResult */
        $productCountResult = $this->productRepository
            ->aggregate($criteria, $event->getContext())
            ->get('productCount');
            
        return new ProductCountRouteResponse($productCountResult);
    }
}

```

Und:

```auto
<?php declare(strict_types=1);

namespace CustomFilter\Core\Content\Example\SalesChannel;

use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\System\SalesChannel\StoreApiResponse;
use ADFilter\Core\Content\Example\ExampleCollection;

use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;

use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;

/**
 * Class ProductCountRouteResponse
 * @property EntitySearchResult $object
 */
class ProductCountRouteResponse extends StoreApiResponse
{
    public function getExamples(): ExampleCollection
    {
        /** @var ExampleCollection $collection */
        $collection = $this->object->getEntities();

        return $collection;
    }
}

```

Und:

```auto
<services>
    <service id="CustomFilter\Service\AddDataToPage" >
        <argument type="service" id="CustomFilter\Core\Content\Example\SalesChannel\ProductCountRoute"/>
        <tag name="kernel.event_subscriber"/>
    </service>

    <service id="CustomFilter\Core\Content\Example\SalesChannel\ProductCountRoute">
        <argument type="service" id="product.repository"/>
    </service>
</services>

```

---

<div class="post-metadata">

**Author:** ![shopwarer1638](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@shopwarer1638](https://forum.shopware.com/u/shopwarer1638)\
**Post date:** [7. April 2023 um 11:32 UTC](https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415/24 "2023-04-07T11:32:56Z")

</div>

Dieser Teil meines Plugins basiert auf dieser Dokumentation:

> **[Add data to storefront page](https://developer.shopware.com/docs/guides/plugins/plugins/storefront/add-data-to-storefront-page)**

Wo `ProductCountRoute extends AbstractProductCountRoute`

Es gibt auch ProductCountRouteResponse.php  
Aber es funktioniert nicht alles zusammen ☹

[Vorherige Seite](https://forum.shopware.com/t/api-route-zu-erstellen-sehr-schwer-trotz-docu/96415.md?page=1)
