Skip to content

Commit

Permalink
feat: add article recommendation endpoint on api
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-chinour committed Jun 26, 2024
1 parent 20f8ecb commit c11ded1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
12 changes: 8 additions & 4 deletions http/api_article.http
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
@id = 6J0hWoFCuiuWQtjp3K1mXn
@slug = les-structures-de-donnees-en-php

# @name Get Articles
GET {{host}}/api/articles?limit=2
Content-Type: application/json

###

# @name Get Article By ID
@id = 6J0hWoFCuiuWQtjp3K1mXn
GET {{host}}/api/articles/{{id}}
Content-Type: application/json

###

# @name Get Article By Slug
@slug = les-structures-de-donnees-en-php
GET {{host}}/api/articles/{{slug}}
Content-Type: application/json

###
# @name Get Article Recommendations
GET {{host}}/api/articles/{{id}}/recommendations
Content-Type: application/json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Application\Query\GetArticleRecommendations;

use App\Application\Query\QueryCache;

#[QueryCache(ttl: 86_400)]
final readonly class GetArticleRecommendationsQuery
{
public function __construct(public string $articleIdentifier) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Application\Query\GetArticleRecommendations;

use App\Application\Query\BatchArticle\BatchArticleQuery;
use App\Application\Query\GetArticle\GetArticleQuery;
use App\Application\Query\GetArticleList\GetArticleListQuery;
use App\Domain\Blogging\BlogArticle;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\HandleTrait;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
final class GetArticleRecommendationsQueryHandler
{
use HandleTrait;

public function __construct(MessageBusInterface $bus)
{
$this->messageBus = $bus;
}

public function __invoke(GetArticleRecommendationsQuery $query): array
{
$sourceArticle = $this->handle(new GetArticleQuery($query->articleIdentifier));
if (!($sourceArticle instanceof BlogArticle)) {
return [];
}

if (empty($sourceArticle->recommendations)) {
return $this->handle(new GetArticleListQuery(2));
}

return $this->handle(new BatchArticleQuery($sourceArticle->recommendations));
}
}
36 changes: 36 additions & 0 deletions src/Presentation/Api/GetArticleRecommendationsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Presentation\Api;

use App\Application\Query\GetArticleRecommendations\GetArticleRecommendationsQuery;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\Cache;
use Symfony\Component\Messenger\HandleTrait;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Attribute\Route;

#[AsController]
#[Route(path: '/articles/{id}/recommendations', name: 'get_article_recommendations', requirements: ['id' => '\w+'], methods: ['GET'])]
#[Cache(maxage: 60, smaxage: 86_400, public: true)]
final class GetArticleRecommendationsController extends AbstractController
{
use HandleTrait;

public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}

public function __invoke(string $id): JsonResponse
{
if (null === ($recommendations = $this->handle(new GetArticleRecommendationsQuery($id)))) {
throw $this->createNotFoundException();
}

return $this->json($recommendations);
}
}

0 comments on commit c11ded1

Please sign in to comment.