-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add article recommendation endpoint on api
- Loading branch information
1 parent
20f8ecb
commit c11ded1
Showing
4 changed files
with
95 additions
and
4 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
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 |
13 changes: 13 additions & 0 deletions
13
src/Application/Query/GetArticleRecommendations/GetArticleRecommendationsQuery.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,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) {} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Application/Query/GetArticleRecommendations/GetArticleRecommendationsQueryHandler.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,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
36
src/Presentation/Api/GetArticleRecommendationsController.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,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); | ||
} | ||
} |