feat(manga): regrouper les chapitres d'un volume importé dans la liste API #17

Merged
colgora merged 2 commits from feat/volume-chapter-grouping into main 2026-03-15 19:21:45 +01:00
9 changed files with 287 additions and 19 deletions

View File

@@ -1,7 +1,8 @@
<template>
<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 }">
{{ 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 class="px-4 py-2 w-full text-left text-gray-900 dark:text-gray-100">
<router-link
@@ -13,9 +14,17 @@
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>
<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 class="px-4 py-2 flex justify-end gap-2">
<button v-if="!chapter.isAvailable" @click="handleSearch" :class="buttonClass">

View File

@@ -23,18 +23,60 @@ readonly class GetMangaChaptersHandler
throw new MangaNotFoundException();
}
$chapters = $this->mangaRepository->findChapters(
$allChapters = $this->mangaRepository->findAllChapters(
mangaId: $query->mangaId,
page: $query->page,
limit: $query->limit,
sortOrder: $query->sortOrder
sortOrder: 'asc'
);
$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(
chapters: array_map(
fn (Chapter $chapter) => new ChapterResponse(
chapters: $paginatedChapters,
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(),
number: $chapter->getNumber(),
title: $chapter->getTitle(),
@@ -42,12 +84,39 @@ readonly class GetMangaChaptersHandler
isVisible: $chapter->isVisible(),
pagesDirectory: $chapter->getPagesDirectory(),
createdAt: $chapter->getCreatedAt()->format(\DateTimeInterface::RFC3339)
),
$chapters
),
total: $total,
page: $query->page,
limit: $query->limit
);
}
}
if (!empty($currentGroup)) {
$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)
);
}
}

View File

@@ -11,7 +11,10 @@ readonly class ChapterResponse
public ?int $volume,
public bool $isVisible,
public ?string $pagesDirectory,
public string $createdAt
public string $createdAt,
public bool $isVolumeGroup = false,
public ?string $volumeChaptersRange = null,
public int $volumeChapterCount = 0,
) {
}
}

View File

@@ -30,6 +30,11 @@ interface MangaRepositoryInterface
// --- Chapters (read) ---
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 countAvailableChapters(string $mangaId): int;
public function findChapterById(string $id): ?Chapter;

View File

@@ -14,7 +14,10 @@ readonly class ChapterListItem
public ?int $volume,
public bool $isVisible,
public bool $isAvailable,
public string $createdAt
public string $createdAt,
public bool $isVolumeGroup = false,
public ?string $volumeChaptersRange = null,
public int $volumeChapterCount = 0,
) {
}
}

View File

@@ -54,7 +54,10 @@ readonly class GetMangaChaptersStateProvider implements ProviderInterface
volume: $chapter->volume,
isVisible: $chapter->isVisible,
isAvailable: $chapter->pagesDirectory !== null,
createdAt: $chapter->createdAt
createdAt: $chapter->createdAt,
isVolumeGroup: $chapter->isVolumeGroup,
volumeChaptersRange: $chapter->volumeChaptersRange,
volumeChapterCount: $chapter->volumeChapterCount,
);
}
}

View File

@@ -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
{
return $this->entityManager->createQueryBuilder()

View File

@@ -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
{
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
{
$this->chapters[$mangaId] = [];

View File

@@ -5,7 +5,9 @@ namespace App\Tests\Domain\Manga\Application\QueryHandler;
use App\Domain\Manga\Application\Query\GetMangaChapters;
use App\Domain\Manga\Application\QueryHandler\GetMangaChaptersHandler;
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\ValueObject\ChapterId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
@@ -67,6 +69,139 @@ class GetMangaChaptersHandlerTest extends TestCase
$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
{
$this->repository->clear();