refactor: replace random-book GET route with server action
This commit is contained in:
@@ -24,7 +24,6 @@ Routes GET actuellement présentes :
|
|||||||
|
|
||||||
| Route | Utilisation actuelle | Pourquoi garder maintenant | Piste de simplification |
|
| Route | Utilisation actuelle | Pourquoi garder maintenant | Piste de simplification |
|
||||||
|-------|----------------------|----------------------------|-------------------------|
|
|-------|----------------------|----------------------------|-------------------------|
|
||||||
| `GET /api/komga/random-book` | `src/components/layout/ClientLayout.tsx` | Action utilisateur ponctuelle (random) | Option: server action dédiée plutôt qu'API GET |
|
|
||||||
| `GET /api/komga/books/[bookId]` | fallback dans `ClientBookPage.tsx`, usage `DownloadManager.tsx` | fallback utile hors flux page SSR | Limiter au fallback strict, éviter le double-fetch |
|
| `GET /api/komga/books/[bookId]` | fallback dans `ClientBookPage.tsx`, usage `DownloadManager.tsx` | fallback utile hors flux page SSR | Limiter au fallback strict, éviter le double-fetch |
|
||||||
| `GET /api/komga/series/[seriesId]` | utilisé via Sidebar pour enrichir les favoris | enrichissement client en cascade | Charger les métadonnées nécessaires en amont côté server |
|
| `GET /api/komga/series/[seriesId]` | utilisé via Sidebar pour enrichir les favoris | enrichissement client en cascade | Charger les métadonnées nécessaires en amont côté server |
|
||||||
| `GET /api/user/profile` | pas d'appel client direct trouvé | route utile pour consommation API interne/outils | Vérifier si remplaçable par service server direct |
|
| `GET /api/user/profile` | pas d'appel client direct trouvé | route utile pour consommation API interne/outils | Vérifier si remplaçable par service server direct |
|
||||||
@@ -36,6 +35,7 @@ Routes GET actuellement présentes :
|
|||||||
|-------|-----------------------------|-------|--------|
|
|-------|-----------------------------|-------|--------|
|
||||||
| `GET /api/komga/libraries/[libraryId]/series` | `src/app/libraries/[libraryId]/LibraryClientWrapper.tsx` | Chargement via `searchParams` dans page server | ✅ Supprimée |
|
| `GET /api/komga/libraries/[libraryId]/series` | `src/app/libraries/[libraryId]/LibraryClientWrapper.tsx` | Chargement via `searchParams` dans page server | ✅ Supprimée |
|
||||||
| `GET /api/komga/series/[seriesId]/books` | `src/app/series/[seriesId]/SeriesClientWrapper.tsx` | Chargement via `searchParams` dans page server | ✅ Supprimée |
|
| `GET /api/komga/series/[seriesId]/books` | `src/app/series/[seriesId]/SeriesClientWrapper.tsx` | Chargement via `searchParams` dans page server | ✅ Supprimée |
|
||||||
|
| `GET /api/komga/random-book` | `src/components/layout/ClientLayout.tsx` | Action utilisateur via server action | ✅ Supprimée |
|
||||||
|
|
||||||
### C. A conserver (API de transport / framework)
|
### C. A conserver (API de transport / framework)
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { LibraryService } from "@/lib/services/library.service";
|
import { LibraryService } from "@/lib/services/library.service";
|
||||||
|
import { BookService } from "@/lib/services/book.service";
|
||||||
import { AppError } from "@/utils/errors";
|
import { AppError } from "@/utils/errors";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,3 +26,25 @@ export async function scanLibrary(
|
|||||||
return { success: false, message: "Erreur lors du scan" };
|
return { success: false, message: "Erreur lors du scan" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne un livre aléatoire depuis les bibliothèques sélectionnées
|
||||||
|
*/
|
||||||
|
export async function getRandomBookFromLibraries(
|
||||||
|
libraryIds: string[]
|
||||||
|
): Promise<{ success: boolean; bookId?: string; message?: string }> {
|
||||||
|
try {
|
||||||
|
if (!libraryIds.length) {
|
||||||
|
return { success: false, message: "Au moins une bibliothèque doit être sélectionnée" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const bookId = await BookService.getRandomBookFromLibraries(libraryIds);
|
||||||
|
return { success: true, bookId };
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof AppError) {
|
||||||
|
return { success: false, message: error.message };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: false, message: "Erreur lors de la récupération d'un livre aléatoire" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import type { NextRequest } from "next/server";
|
|
||||||
import { BookService } from "@/lib/services/book.service";
|
|
||||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
|
||||||
import { AppError } from "@/utils/errors";
|
|
||||||
import { getErrorMessage } from "@/utils/errors";
|
|
||||||
import logger from "@/lib/logger";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
|
||||||
try {
|
|
||||||
const { searchParams } = new URL(request.url);
|
|
||||||
const libraryIds = searchParams.get("libraryIds")?.split(",") || [];
|
|
||||||
|
|
||||||
if (libraryIds.length === 0) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: ERROR_CODES.LIBRARY.FETCH_ERROR,
|
|
||||||
message: "Au moins une bibliothèque doit être sélectionnée",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const bookId = await BookService.getRandomBookFromLibraries(libraryIds);
|
|
||||||
return NextResponse.json({ bookId });
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({ err: error }, "Erreur lors de la récupération d'un livre aléatoire");
|
|
||||||
if (error instanceof AppError) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: error.code,
|
|
||||||
message: getErrorMessage(error.code),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: ERROR_CODES.SERIES.FETCH_ERROR,
|
|
||||||
message: getErrorMessage(ERROR_CODES.SERIES.FETCH_ERROR),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,6 +12,7 @@ import { usePreferences } from "@/contexts/PreferencesContext";
|
|||||||
import { ServiceWorkerProvider } from "@/contexts/ServiceWorkerContext";
|
import { ServiceWorkerProvider } from "@/contexts/ServiceWorkerContext";
|
||||||
import type { KomgaLibrary, KomgaSeries } from "@/types/komga";
|
import type { KomgaLibrary, KomgaSeries } from "@/types/komga";
|
||||||
import logger from "@/lib/logger";
|
import logger from "@/lib/logger";
|
||||||
|
import { getRandomBookFromLibraries } from "@/app/actions/library";
|
||||||
|
|
||||||
// Routes qui ne nécessitent pas d'authentification
|
// Routes qui ne nécessitent pas d'authentification
|
||||||
const publicRoutes = ["/login", "/register"];
|
const publicRoutes = ["/login", "/register"];
|
||||||
@@ -51,10 +52,11 @@ export default function ClientLayout({
|
|||||||
const fetchRandomBook = useCallback(async () => {
|
const fetchRandomBook = useCallback(async () => {
|
||||||
if (backgroundType === "komga-random" && libraryIdsString) {
|
if (backgroundType === "komga-random" && libraryIdsString) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/komga/random-book?libraryIds=${libraryIdsString}`);
|
const libraryIds = libraryIdsString.split(",").filter(Boolean);
|
||||||
if (response.ok) {
|
const result = await getRandomBookFromLibraries(libraryIds);
|
||||||
const data = await response.json();
|
|
||||||
setRandomBookId(data.bookId);
|
if (result.success && result.bookId) {
|
||||||
|
setRandomBookId(result.bookId);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error({ err: error }, "Erreur lors de la récupération d'un book aléatoire:");
|
logger.error({ err: error }, "Erreur lors de la récupération d'un book aléatoire:");
|
||||||
|
|||||||
Reference in New Issue
Block a user