refactor: replace random-book GET route with server action

This commit is contained in:
2026-02-28 12:10:15 +01:00
parent e5497b4f58
commit b1e0e18d9e
4 changed files with 30 additions and 58 deletions

View File

@@ -2,6 +2,7 @@
import { revalidatePath } from "next/cache";
import { LibraryService } from "@/lib/services/library.service";
import { BookService } from "@/lib/services/book.service";
import { AppError } from "@/utils/errors";
/**
@@ -25,3 +26,25 @@ export async function scanLibrary(
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" };
}
}