Merge pull request 'feat(manga): regrouper les chapitres d'un volume importé dans la liste API' (#17) from feat/volume-chapter-grouping into main
All checks were successful
Deploy / deploy (push) Successful in 2m59s
All checks were successful
Deploy / deploy (push) Successful in 2m59s
Reviewed-on: #17
This commit was merged in pull request #17.
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
<template>
|
<template>
|
||||||
<tr class="border-t dark:border-gray-700 hover:bg-green-100 dark:hover:bg-green-900/20">
|
<tr class="border-t dark:border-gray-700 hover:bg-green-100 dark:hover:bg-green-900/20">
|
||||||
<td class="px-4 py-2 text-gray-900 dark:text-gray-100" :class="{ 'text-green-500 dark:text-green-400': chapter.isAvailable }">
|
<td class="px-4 py-2 text-gray-900 dark:text-gray-100" :class="{ 'text-green-500 dark:text-green-400': chapter.isAvailable }">
|
||||||
{{ String(chapter.number).padStart(2, '0') }}
|
<template v-if="chapter.isVolumeGroup">Vol. {{ chapter.volume }}</template>
|
||||||
|
<template v-else>{{ String(chapter.number).padStart(2, '0') }}</template>
|
||||||
</td>
|
</td>
|
||||||
<td class="px-4 py-2 w-full text-left text-gray-900 dark:text-gray-100">
|
<td class="px-4 py-2 w-full text-left text-gray-900 dark:text-gray-100">
|
||||||
<router-link
|
<router-link
|
||||||
@@ -13,9 +14,17 @@
|
|||||||
chapterId: chapter.id
|
chapterId: chapter.id
|
||||||
}
|
}
|
||||||
}">
|
}">
|
||||||
{{ chapter.title || 'Sans titre' }}
|
<template v-if="chapter.isVolumeGroup">
|
||||||
|
{{ chapter.volumeChapterCount > 1 ? 'Chapitres ' : 'Chapitre ' }}{{ chapter.volumeChaptersRange }}
|
||||||
|
</template>
|
||||||
|
<template v-else>{{ chapter.title || 'Sans titre' }}</template>
|
||||||
</router-link>
|
</router-link>
|
||||||
<span v-else class="text-gray-500 dark:text-gray-400">{{ chapter.title || 'Sans titre' }}</span>
|
<span v-else class="text-gray-500 dark:text-gray-400">
|
||||||
|
<template v-if="chapter.isVolumeGroup">
|
||||||
|
{{ chapter.volumeChapterCount > 1 ? 'Chapitres ' : 'Chapitre ' }}{{ chapter.volumeChaptersRange }}
|
||||||
|
</template>
|
||||||
|
<template v-else>{{ chapter.title || 'Sans titre' }}</template>
|
||||||
|
</span>
|
||||||
</td>
|
</td>
|
||||||
<td class="px-4 py-2 flex justify-end gap-2">
|
<td class="px-4 py-2 flex justify-end gap-2">
|
||||||
<button v-if="!chapter.isAvailable" @click="handleSearch" :class="buttonClass">
|
<button v-if="!chapter.isAvailable" @click="handleSearch" :class="buttonClass">
|
||||||
|
|||||||
@@ -23,18 +23,60 @@ readonly class GetMangaChaptersHandler
|
|||||||
throw new MangaNotFoundException();
|
throw new MangaNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$chapters = $this->mangaRepository->findChapters(
|
$allChapters = $this->mangaRepository->findAllChapters(
|
||||||
mangaId: $query->mangaId,
|
mangaId: $query->mangaId,
|
||||||
page: $query->page,
|
sortOrder: 'asc'
|
||||||
limit: $query->limit,
|
|
||||||
sortOrder: $query->sortOrder
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$total = $this->mangaRepository->countChapters($query->mangaId);
|
$grouped = $this->groupChapters($allChapters);
|
||||||
|
|
||||||
|
if ($query->sortOrder === 'desc') {
|
||||||
|
usort($grouped, fn (ChapterResponse $a, ChapterResponse $b) => $b->number <=> $a->number);
|
||||||
|
}
|
||||||
|
|
||||||
|
$total = count($grouped);
|
||||||
|
$offset = ($query->page - 1) * $query->limit;
|
||||||
|
$paginatedChapters = array_slice($grouped, $offset, $query->limit);
|
||||||
|
|
||||||
return new ChapterListResponse(
|
return new ChapterListResponse(
|
||||||
chapters: array_map(
|
chapters: $paginatedChapters,
|
||||||
fn (Chapter $chapter) => new ChapterResponse(
|
total: $total,
|
||||||
|
page: $query->page,
|
||||||
|
limit: $query->limit
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Chapter[] $chapters */
|
||||||
|
private function groupChapters(array $chapters): array
|
||||||
|
{
|
||||||
|
$result = [];
|
||||||
|
$currentGroup = [];
|
||||||
|
$currentPagesDir = null;
|
||||||
|
$currentVolume = null;
|
||||||
|
|
||||||
|
foreach ($chapters as $chapter) {
|
||||||
|
$pagesDir = $chapter->getPagesDirectory();
|
||||||
|
$volume = $chapter->getVolume();
|
||||||
|
|
||||||
|
if ($pagesDir !== null && $volume !== null) {
|
||||||
|
if ($pagesDir === $currentPagesDir && $volume === $currentVolume) {
|
||||||
|
$currentGroup[] = $chapter;
|
||||||
|
} else {
|
||||||
|
if (!empty($currentGroup)) {
|
||||||
|
$result[] = $this->buildVolumeGroupResponse($currentGroup);
|
||||||
|
}
|
||||||
|
$currentGroup = [$chapter];
|
||||||
|
$currentPagesDir = $pagesDir;
|
||||||
|
$currentVolume = $volume;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!empty($currentGroup)) {
|
||||||
|
$result[] = $this->buildVolumeGroupResponse($currentGroup);
|
||||||
|
$currentGroup = [];
|
||||||
|
$currentPagesDir = null;
|
||||||
|
$currentVolume = null;
|
||||||
|
}
|
||||||
|
$result[] = new ChapterResponse(
|
||||||
id: $chapter->getId(),
|
id: $chapter->getId(),
|
||||||
number: $chapter->getNumber(),
|
number: $chapter->getNumber(),
|
||||||
title: $chapter->getTitle(),
|
title: $chapter->getTitle(),
|
||||||
@@ -42,12 +84,39 @@ readonly class GetMangaChaptersHandler
|
|||||||
isVisible: $chapter->isVisible(),
|
isVisible: $chapter->isVisible(),
|
||||||
pagesDirectory: $chapter->getPagesDirectory(),
|
pagesDirectory: $chapter->getPagesDirectory(),
|
||||||
createdAt: $chapter->getCreatedAt()->format(\DateTimeInterface::RFC3339)
|
createdAt: $chapter->getCreatedAt()->format(\DateTimeInterface::RFC3339)
|
||||||
),
|
);
|
||||||
$chapters
|
}
|
||||||
),
|
}
|
||||||
total: $total,
|
|
||||||
page: $query->page,
|
if (!empty($currentGroup)) {
|
||||||
limit: $query->limit
|
$result[] = $this->buildVolumeGroupResponse($currentGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Chapter[] $group */
|
||||||
|
private function buildVolumeGroupResponse(array $group): ChapterResponse
|
||||||
|
{
|
||||||
|
$first = $group[0];
|
||||||
|
$numbers = array_map(fn (Chapter $c) => $c->getNumber(), $group);
|
||||||
|
$min = min($numbers);
|
||||||
|
$max = max($numbers);
|
||||||
|
|
||||||
|
$fmt = fn (float $n) => $n == (int) $n ? (string) (int) $n : (string) $n;
|
||||||
|
$range = count($group) > 1 ? $fmt($min) . '-' . $fmt($max) : $fmt($min);
|
||||||
|
|
||||||
|
return new ChapterResponse(
|
||||||
|
id: $first->getId(),
|
||||||
|
number: $first->getNumber(),
|
||||||
|
title: $first->getTitle(),
|
||||||
|
volume: $first->getVolume(),
|
||||||
|
isVisible: $first->isVisible(),
|
||||||
|
pagesDirectory: $first->getPagesDirectory(),
|
||||||
|
createdAt: $first->getCreatedAt()->format(\DateTimeInterface::RFC3339),
|
||||||
|
isVolumeGroup: true,
|
||||||
|
volumeChaptersRange: $range,
|
||||||
|
volumeChapterCount: count($group)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ readonly class ChapterResponse
|
|||||||
public ?int $volume,
|
public ?int $volume,
|
||||||
public bool $isVisible,
|
public bool $isVisible,
|
||||||
public ?string $pagesDirectory,
|
public ?string $pagesDirectory,
|
||||||
public string $createdAt
|
public string $createdAt,
|
||||||
|
public bool $isVolumeGroup = false,
|
||||||
|
public ?string $volumeChaptersRange = null,
|
||||||
|
public int $volumeChapterCount = 0,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,11 @@ interface MangaRepositoryInterface
|
|||||||
// --- Chapters (read) ---
|
// --- Chapters (read) ---
|
||||||
|
|
||||||
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array;
|
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Chapter[]
|
||||||
|
*/
|
||||||
|
public function findAllChapters(string $mangaId, string $sortOrder = 'desc'): array;
|
||||||
public function countChapters(string $mangaId): int;
|
public function countChapters(string $mangaId): int;
|
||||||
public function countAvailableChapters(string $mangaId): int;
|
public function countAvailableChapters(string $mangaId): int;
|
||||||
public function findChapterById(string $id): ?Chapter;
|
public function findChapterById(string $id): ?Chapter;
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ readonly class ChapterListItem
|
|||||||
public ?int $volume,
|
public ?int $volume,
|
||||||
public bool $isVisible,
|
public bool $isVisible,
|
||||||
public bool $isAvailable,
|
public bool $isAvailable,
|
||||||
public string $createdAt
|
public string $createdAt,
|
||||||
|
public bool $isVolumeGroup = false,
|
||||||
|
public ?string $volumeChaptersRange = null,
|
||||||
|
public int $volumeChapterCount = 0,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,10 @@ readonly class GetMangaChaptersStateProvider implements ProviderInterface
|
|||||||
volume: $chapter->volume,
|
volume: $chapter->volume,
|
||||||
isVisible: $chapter->isVisible,
|
isVisible: $chapter->isVisible,
|
||||||
isAvailable: $chapter->pagesDirectory !== null,
|
isAvailable: $chapter->pagesDirectory !== null,
|
||||||
createdAt: $chapter->createdAt
|
createdAt: $chapter->createdAt,
|
||||||
|
isVolumeGroup: $chapter->isVolumeGroup,
|
||||||
|
volumeChaptersRange: $chapter->volumeChaptersRange,
|
||||||
|
volumeChapterCount: $chapter->volumeChapterCount,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,6 +185,21 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findAllChapters(string $mangaId, string $sortOrder = 'desc'): array
|
||||||
|
{
|
||||||
|
$queryBuilder = $this->entityManager->createQueryBuilder()
|
||||||
|
->select('c')
|
||||||
|
->from(EntityChapter::class, 'c')
|
||||||
|
->where('c.manga = :mangaId')
|
||||||
|
->orderBy('c.number', $sortOrder)
|
||||||
|
->setParameter('mangaId', $mangaId);
|
||||||
|
|
||||||
|
return array_map(
|
||||||
|
fn (EntityChapter $entity) => $this->toChapterDomain($entity),
|
||||||
|
$queryBuilder->getQuery()->getResult()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function countChapters(string $mangaId): int
|
public function countChapters(string $mangaId): int
|
||||||
{
|
{
|
||||||
return $this->entityManager->createQueryBuilder()
|
return $this->entityManager->createQueryBuilder()
|
||||||
|
|||||||
@@ -112,6 +112,23 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findAllChapters(string $mangaId, string $sortOrder = 'desc'): array
|
||||||
|
{
|
||||||
|
if (!isset($this->chapters[$mangaId])) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$chapters = $this->chapters[$mangaId];
|
||||||
|
|
||||||
|
usort($chapters, function (Chapter $a, Chapter $b) use ($sortOrder) {
|
||||||
|
return $sortOrder === 'desc'
|
||||||
|
? $b->getNumber() <=> $a->getNumber()
|
||||||
|
: $a->getNumber() <=> $b->getNumber();
|
||||||
|
});
|
||||||
|
|
||||||
|
return $chapters;
|
||||||
|
}
|
||||||
|
|
||||||
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array
|
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array
|
||||||
{
|
{
|
||||||
if (!isset($this->chapters[$mangaId])) {
|
if (!isset($this->chapters[$mangaId])) {
|
||||||
@@ -198,6 +215,15 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addChapter(string $mangaId, Chapter $chapter): void
|
||||||
|
{
|
||||||
|
if (!isset($this->chapters[$mangaId])) {
|
||||||
|
$this->chapters[$mangaId] = [];
|
||||||
|
}
|
||||||
|
$this->chapters[$mangaId][] = $chapter;
|
||||||
|
$this->chaptersById[$chapter->getId()] = $chapter;
|
||||||
|
}
|
||||||
|
|
||||||
public function addChaptersToManga(string $mangaId, int $count): void
|
public function addChaptersToManga(string $mangaId, int $count): void
|
||||||
{
|
{
|
||||||
$this->chapters[$mangaId] = [];
|
$this->chapters[$mangaId] = [];
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ namespace App\Tests\Domain\Manga\Application\QueryHandler;
|
|||||||
use App\Domain\Manga\Application\Query\GetMangaChapters;
|
use App\Domain\Manga\Application\Query\GetMangaChapters;
|
||||||
use App\Domain\Manga\Application\QueryHandler\GetMangaChaptersHandler;
|
use App\Domain\Manga\Application\QueryHandler\GetMangaChaptersHandler;
|
||||||
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
|
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
|
||||||
|
use App\Domain\Manga\Domain\Model\Chapter;
|
||||||
use App\Domain\Manga\Domain\Model\Manga;
|
use App\Domain\Manga\Domain\Model\Manga;
|
||||||
|
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
||||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
|
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
|
||||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
|
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
|
||||||
@@ -67,6 +69,139 @@ class GetMangaChaptersHandlerTest extends TestCase
|
|||||||
$this->assertTrue($response->hasPreviousPage());
|
$this->assertTrue($response->hasPreviousPage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGroupsVolumeChaptersWithSharedPagesDirectory(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$this->givenMangaExists('1');
|
||||||
|
$sharedDir = '/manga/vol1/';
|
||||||
|
foreach ([1, 2, 3] as $num) {
|
||||||
|
$this->repository->addChapter('1', new Chapter(
|
||||||
|
id: new ChapterId((string) $num),
|
||||||
|
mangaId: new MangaId('1'),
|
||||||
|
number: (float) $num,
|
||||||
|
title: null,
|
||||||
|
volume: 1,
|
||||||
|
isVisible: true,
|
||||||
|
pagesDirectory: $sharedDir,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$response = $this->handler->handle(new GetMangaChapters('1'));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(1, $response->chapters);
|
||||||
|
$this->assertEquals(1, $response->total);
|
||||||
|
$item = $response->chapters[0];
|
||||||
|
$this->assertTrue($item->isVolumeGroup);
|
||||||
|
$this->assertEquals('1-3', $item->volumeChaptersRange);
|
||||||
|
$this->assertEquals(3, $item->volumeChapterCount);
|
||||||
|
$this->assertEquals(1, $item->volume);
|
||||||
|
$this->assertEquals(1.0, $item->number);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGroupsSingleVolumeChapter(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$this->givenMangaExists('1');
|
||||||
|
$this->repository->addChapter('1', new Chapter(
|
||||||
|
id: new ChapterId('10'),
|
||||||
|
mangaId: new MangaId('1'),
|
||||||
|
number: 5.0,
|
||||||
|
title: null,
|
||||||
|
volume: 2,
|
||||||
|
isVisible: true,
|
||||||
|
pagesDirectory: '/manga/vol2/',
|
||||||
|
));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$response = $this->handler->handle(new GetMangaChapters('1'));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(1, $response->chapters);
|
||||||
|
$item = $response->chapters[0];
|
||||||
|
$this->assertTrue($item->isVolumeGroup);
|
||||||
|
$this->assertEquals('5', $item->volumeChaptersRange);
|
||||||
|
$this->assertEquals(1, $item->volumeChapterCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDoesNotGroupChaptersWithDistinctPagesDirectory(): void
|
||||||
|
{
|
||||||
|
// Arrange — 3 chapitres scrapés avec pagesDirectory distinctes, pas de volume
|
||||||
|
$this->givenMangaExists('1');
|
||||||
|
foreach ([1, 2, 3] as $num) {
|
||||||
|
$this->repository->addChapter('1', new Chapter(
|
||||||
|
id: new ChapterId((string) $num),
|
||||||
|
mangaId: new MangaId('1'),
|
||||||
|
number: (float) $num,
|
||||||
|
title: null,
|
||||||
|
volume: null,
|
||||||
|
isVisible: true,
|
||||||
|
pagesDirectory: '/manga/ch' . $num . '/',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$response = $this->handler->handle(new GetMangaChapters('1'));
|
||||||
|
|
||||||
|
// Assert — 3 items distincts, aucun groupe
|
||||||
|
$this->assertCount(3, $response->chapters);
|
||||||
|
$this->assertEquals(3, $response->total);
|
||||||
|
foreach ($response->chapters as $item) {
|
||||||
|
$this->assertFalse($item->isVolumeGroup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMixedNormalAndVolumeChapters(): void
|
||||||
|
{
|
||||||
|
// Arrange — 2 chapitres scrapés + 3 chapitres de volume importé
|
||||||
|
$this->givenMangaExists('1');
|
||||||
|
|
||||||
|
// Chapitres scrapés (pagesDirectory individuel, pas de volume)
|
||||||
|
foreach ([1, 2] as $num) {
|
||||||
|
$this->repository->addChapter('1', new Chapter(
|
||||||
|
id: new ChapterId((string) $num),
|
||||||
|
mangaId: new MangaId('1'),
|
||||||
|
number: (float) $num,
|
||||||
|
title: null,
|
||||||
|
volume: null,
|
||||||
|
isVisible: true,
|
||||||
|
pagesDirectory: '/manga/ch' . $num . '/',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Volume importé — 3 chapitres avec même pagesDirectory
|
||||||
|
$sharedDir = '/manga/vol1/';
|
||||||
|
foreach ([3, 4, 5] as $num) {
|
||||||
|
$this->repository->addChapter('1', new Chapter(
|
||||||
|
id: new ChapterId((string) ($num + 10)),
|
||||||
|
mangaId: new MangaId('1'),
|
||||||
|
number: (float) $num,
|
||||||
|
title: null,
|
||||||
|
volume: 1,
|
||||||
|
isVisible: true,
|
||||||
|
pagesDirectory: $sharedDir,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$response = $this->handler->handle(new GetMangaChapters('1', sortOrder: 'asc'));
|
||||||
|
|
||||||
|
// Assert — 2 chapitres normaux + 1 groupe = 3 items
|
||||||
|
$this->assertCount(3, $response->chapters);
|
||||||
|
$this->assertEquals(3, $response->total);
|
||||||
|
|
||||||
|
// Les 2 premiers sont des chapitres normaux
|
||||||
|
$this->assertFalse($response->chapters[0]->isVolumeGroup);
|
||||||
|
$this->assertFalse($response->chapters[1]->isVolumeGroup);
|
||||||
|
|
||||||
|
// Le 3e est un groupe de volume
|
||||||
|
$volumeItem = $response->chapters[2];
|
||||||
|
$this->assertTrue($volumeItem->isVolumeGroup);
|
||||||
|
$this->assertEquals('3-5', $volumeItem->volumeChaptersRange);
|
||||||
|
$this->assertEquals(3, $volumeItem->volumeChapterCount);
|
||||||
|
}
|
||||||
|
|
||||||
protected function tearDown(): void
|
protected function tearDown(): void
|
||||||
{
|
{
|
||||||
$this->repository->clear();
|
$this->repository->clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user