feat: GetChapters endpoint + tests
This commit is contained in:
parent
2f615a4936
commit
6667cc224b
@@ -3,12 +3,17 @@
|
||||
namespace App\Tests\Domain\Manga\Adapter;
|
||||
|
||||
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
|
||||
use App\Domain\Manga\Domain\Model\Chapter;
|
||||
use App\Domain\Manga\Domain\Model\Manga;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
||||
|
||||
class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
{
|
||||
/** @var Manga[] */
|
||||
private array $mangas = [];
|
||||
|
||||
/** @var array<string, Chapter[]> */
|
||||
private array $chapters = [];
|
||||
|
||||
public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array
|
||||
{
|
||||
@@ -66,8 +71,49 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||
};
|
||||
}
|
||||
|
||||
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, 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();
|
||||
});
|
||||
|
||||
$offset = ($page - 1) * $limit;
|
||||
return array_slice($chapters, $offset, $limit);
|
||||
}
|
||||
|
||||
public function countChapters(string $mangaId): int
|
||||
{
|
||||
return isset($this->chapters[$mangaId]) ? count($this->chapters[$mangaId]) : 0;
|
||||
}
|
||||
|
||||
public function addChaptersToManga(string $mangaId, int $count): void
|
||||
{
|
||||
$this->chapters[$mangaId] = [];
|
||||
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->chapters[$mangaId][] = new Chapter(
|
||||
id: new ChapterId((string)$i),
|
||||
mangaId: $mangaId,
|
||||
number: (float)$i,
|
||||
title: "Chapter $i",
|
||||
volume: (int)ceil($i / 10),
|
||||
isVisible: true,
|
||||
createdAt: new \DateTimeImmutable()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
$this->mangas = [];
|
||||
$this->chapters = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user