feat: GetMangaList endpoint + tests + test db
This commit is contained in:
parent
073439163b
commit
e3d380eadd
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
|
||||
readonly class MangaCollection
|
||||
{
|
||||
public function __construct(
|
||||
/** @var MangaListItem[] */
|
||||
public array $items,
|
||||
public int $total,
|
||||
public int $page,
|
||||
public int $limit,
|
||||
public bool $hasNextPage,
|
||||
public bool $hasPreviousPage
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
|
||||
readonly class MangaListItem
|
||||
{
|
||||
public function __construct(
|
||||
#[ApiProperty(identifier: true)]
|
||||
public string $id,
|
||||
public string $title,
|
||||
public string $slug,
|
||||
public ?string $imageUrl,
|
||||
public string $author,
|
||||
public int $publicationYear,
|
||||
public array $genres,
|
||||
public string $status,
|
||||
public ?float $rating
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaCollection;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider\GetMangaListStateProvider;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'Manga',
|
||||
operations: [
|
||||
new GetCollection(
|
||||
uriTemplate: '/mangas',
|
||||
provider: GetMangaListStateProvider::class,
|
||||
output: MangaCollection::class
|
||||
)
|
||||
]
|
||||
)]
|
||||
class MangaListResource
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Domain\Manga\Application\Query\GetMangaList;
|
||||
use App\Domain\Manga\Application\QueryHandler\GetMangaListHandler;
|
||||
use App\Domain\Manga\Domain\Model\Manga;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaCollection;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaListItem;
|
||||
|
||||
readonly class GetMangaListStateProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private GetMangaListHandler $handler
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): MangaCollection
|
||||
{
|
||||
$page = $context['filters']['page'] ?? 1;
|
||||
$limit = $context['filters']['limit'] ?? 20;
|
||||
$sortBy = $context['filters']['sortBy'] ?? 'title';
|
||||
$sortOrder = $context['filters']['sortOrder'] ?? 'asc';
|
||||
|
||||
$query = new GetMangaList($page, $limit, $sortBy, $sortOrder);
|
||||
$response = $this->handler->handle($query);
|
||||
|
||||
return new MangaCollection(
|
||||
items: array_map(
|
||||
fn (Manga $manga) => $this->createMangaListItem($manga),
|
||||
$response->mangas
|
||||
),
|
||||
total: $response->total,
|
||||
page: $response->page,
|
||||
limit: $response->limit,
|
||||
hasNextPage: $response->hasNextPage(),
|
||||
hasPreviousPage: $response->hasPreviousPage()
|
||||
);
|
||||
}
|
||||
|
||||
private function createMangaListItem(Manga $manga): MangaListItem
|
||||
{
|
||||
return new MangaListItem(
|
||||
id: $manga->getId()->getValue(),
|
||||
title: $manga->getTitle()->getValue(),
|
||||
slug: $manga->getSlug()->getValue(),
|
||||
imageUrl: $manga->getImageUrl(),
|
||||
author: $manga->getAuthor(),
|
||||
publicationYear: $manga->getPublicationYear(),
|
||||
genres: $manga->getGenres(),
|
||||
status: $manga->getStatus(),
|
||||
rating: $manga->getRating()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user