refactor: replace random-book GET route with server action
This commit is contained in:
@@ -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" };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user