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\Repository\MangaRepository;
use App\Service\CbzService; use App\Service\CbzService;
use App\Service\NotificationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -13,7 +14,8 @@ class ReaderController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly MangaRepository $mangaRepository, private readonly MangaRepository $mangaRepository,
private readonly CbzService $cbzService private readonly CbzService $cbzService,
private readonly NotificationService $notificationService,
) )
{ {
} }
@@ -32,7 +34,11 @@ class ReaderController extends AbstractController
} }
if (is_null($chapter->getCbzPath())) { 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()); $totalPages = $this->cbzService->getPageCount($chapter->getCbzPath());
@@ -81,10 +87,9 @@ class ReaderController extends AbstractController
$chapters = array_values(array_map(fn($chapter) => [ $chapters = array_values(array_map(fn($chapter) => [
'number' => $chapter->getNumber(), 'number' => $chapter->getNumber(),
'title' => $chapter->getTitle() 'title' => $chapter->getTitle(),
], $chapters)); ], $chapters));
return $this->json($chapters); return $this->json($chapters);
} }
@@ -96,13 +101,17 @@ class ReaderController extends AbstractController
throw $this->createNotFoundException("Le manga demandé n'existe pas."); throw $this->createNotFoundException("Le manga demandé n'existe pas.");
} }
$previousChapter = $manga->getChapters() $chapters = $manga->getChapters()
->filter(fn($chapter) => $chapter->isVisible() && $chapter->getNumber() < $currentChapterNumber) ->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 ? [ return $this->json($previousChapter ? [
'number' => $previousChapter->getNumber(), 'number' => $previousChapter->getNumber(),
'title' => $previousChapter->getTitle() 'title' => $previousChapter->getTitle(),
] : null); ] : null);
} }
@@ -124,7 +133,7 @@ class ReaderController extends AbstractController
return $this->json($nextChapter ? [ return $this->json($nextChapter ? [
'number' => $nextChapter->getNumber(), 'number' => $nextChapter->getNumber(),
'title' => $nextChapter->getTitle() 'title' => $nextChapter->getTitle(),
] : null); ] : null);
} }
} }