enhanced book backend
This commit is contained in:
@@ -3,29 +3,27 @@ import * as booksService from './books.service';
|
||||
import {getExtension} from '../../shared/extensions';
|
||||
import {BookRequest, ChapterRequest, SearchRequest} from '../types';
|
||||
|
||||
export async function getBook(req: BookRequest, reply: FastifyReply) {
|
||||
export async function getBook(req: any, reply: FastifyReply) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const source = req.query.ext || 'anilist';
|
||||
const source = req.query.source;
|
||||
|
||||
let book;
|
||||
if (source === 'anilist') {
|
||||
book = await booksService.getBookById(id);
|
||||
} else {
|
||||
const ext = getExtension(source);
|
||||
|
||||
const result = await booksService.getBookInfoExtension(ext, id);
|
||||
book = result || null;
|
||||
}
|
||||
|
||||
return book;
|
||||
|
||||
} catch (err) {
|
||||
const error = err as Error;
|
||||
return { error: error.toString() };
|
||||
return { error: (err as Error).message };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function getTrending(req: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
const results = await booksService.getTrendingBooks();
|
||||
@@ -88,30 +86,31 @@ export async function searchBooksInExtension(req: any, reply: FastifyReply) {
|
||||
export async function getChapters(req: any, reply: FastifyReply) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { ext } = req.query;
|
||||
return await booksService.getChaptersForBook(id, Boolean(ext));
|
||||
} catch (err) {
|
||||
const source = req.query.source || 'anilist';
|
||||
|
||||
const isExternal = source !== 'anilist';
|
||||
return await booksService.getChaptersForBook(id, isExternal);
|
||||
} catch {
|
||||
return { chapters: [] };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function getChapterContent(req: any, reply: FastifyReply) {
|
||||
try {
|
||||
const { bookId, chapter, provider } = req.params;
|
||||
const { ext } = req.query;
|
||||
const source = req.query.source || 'anilist';
|
||||
|
||||
const content = await booksService.getChapterContent(
|
||||
bookId,
|
||||
chapter,
|
||||
provider,
|
||||
ext
|
||||
source
|
||||
);
|
||||
|
||||
return reply.send(content);
|
||||
} catch (err) {
|
||||
const error = err as Error;
|
||||
console.error("getChapterContent error:", error.message);
|
||||
|
||||
console.error("getChapterContent error:", (err as Error).message);
|
||||
return reply.code(500).send({ error: "Error loading chapter" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,7 +255,8 @@ async function searchChaptersInExtension(ext: Extension, name: string, searchTit
|
||||
number: parseFloat(ch.number.toString()),
|
||||
title: ch.title,
|
||||
date: ch.releaseDate,
|
||||
provider: name
|
||||
provider: name,
|
||||
index: ch.index
|
||||
}));
|
||||
|
||||
await setCache(cacheKey, result, CACHE_TTL_MS);
|
||||
@@ -311,17 +312,15 @@ export async function getChaptersForBook(id: string, ext: Boolean): Promise<{ ch
|
||||
};
|
||||
}
|
||||
|
||||
export async function getChapterContent(bookId: string, chapterIndex: string, providerName: string, name: string): Promise<ChapterContent> {
|
||||
export async function getChapterContent(bookId: string, chapterIndex: string, providerName: string, source: string): Promise<ChapterContent> {
|
||||
const extensions = getAllExtensions();
|
||||
const ext = extensions.get(providerName);
|
||||
|
||||
if (!ext) {
|
||||
throw new Error("Provider not found");
|
||||
}
|
||||
let exts = "anilist";
|
||||
if (name) exts = "ext";
|
||||
|
||||
const contentCacheKey = `content:${providerName}:${exts}:${bookId}:${chapterIndex}`;
|
||||
const contentCacheKey = `content:${providerName}:${source}:${bookId}:${chapterIndex}`;
|
||||
const cachedContent = await getCache(contentCacheKey);
|
||||
|
||||
if (cachedContent) {
|
||||
@@ -340,7 +339,8 @@ export async function getChapterContent(bookId: string, chapterIndex: string, pr
|
||||
}
|
||||
}
|
||||
|
||||
const chapterList = await getChaptersForBook(bookId, Boolean(name));
|
||||
const isExternal = source !== 'anilist';
|
||||
const chapterList = await getChaptersForBook(bookId, isExternal);
|
||||
|
||||
if (!chapterList?.chapters || chapterList.chapters.length === 0) {
|
||||
throw new Error("Chapters not found");
|
||||
|
||||
@@ -78,6 +78,7 @@ export interface Episode {
|
||||
}
|
||||
|
||||
export interface Chapter {
|
||||
index: number;
|
||||
id: string;
|
||||
number: string | number;
|
||||
title?: string;
|
||||
|
||||
Reference in New Issue
Block a user