Previous chapter fix

Chapter not found now redirect to chapters_show
This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2024-10-04 10:27:59 +02:00
parent 5f15d14ae1
commit 2941bbecd1

View File

@@ -4,6 +4,7 @@ namespace App\Controller;
use App\Repository\MangaRepository;
use App\Service\CbzService;
use App\Service\NotificationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
@@ -12,8 +13,9 @@ use Symfony\Component\Routing\Attribute\Route;
class ReaderController extends AbstractController
{
public function __construct(
private readonly MangaRepository $mangaRepository,
private readonly CbzService $cbzService
private readonly MangaRepository $mangaRepository,
private readonly CbzService $cbzService,
private readonly NotificationService $notificationService,
)
{
}
@@ -32,7 +34,11 @@ class ReaderController extends AbstractController
}
if (is_null($chapter->getCbzPath())) {
throw $this->createNotFoundException("Le chapitre demandé n'a pas été scrapé.");
$this->notificationService->sendUpdate([
'status' => 'error',
'message' => 'Le chapitre demandé n\'est pas encore disponible.',
]);
return $this->redirectToRoute('app_manga_show', ['mangaSlug' => $mangaSlug]);
}
$totalPages = $this->cbzService->getPageCount($chapter->getCbzPath());
@@ -81,10 +87,9 @@ class ReaderController extends AbstractController
$chapters = array_values(array_map(fn($chapter) => [
'number' => $chapter->getNumber(),
'title' => $chapter->getTitle()
'title' => $chapter->getTitle(),
], $chapters));
return $this->json($chapters);
}
@@ -96,13 +101,17 @@ class ReaderController extends AbstractController
throw $this->createNotFoundException("Le manga demandé n'existe pas.");
}
$previousChapter = $manga->getChapters()
$chapters = $manga->getChapters()
->filter(fn($chapter) => $chapter->isVisible() && $chapter->getNumber() < $currentChapterNumber)
->last();
->toArray();
usort($chapters, fn($a, $b) => $b->getNumber() <=> $a->getNumber());
$previousChapter = reset($chapters) ?: null;
return $this->json($previousChapter ? [
'number' => $previousChapter->getNumber(),
'title' => $previousChapter->getTitle()
'title' => $previousChapter->getTitle(),
] : null);
}
@@ -124,7 +133,7 @@ class ReaderController extends AbstractController
return $this->json($nextChapter ? [
'number' => $nextChapter->getNumber(),
'title' => $nextChapter->getTitle()
'title' => $nextChapter->getTitle(),
] : null);
}
}