Refactor API integration and data fetching for genre, novel, and chapter pages

- Replace Prisma database calls with API fetches from the reader API in GenreDetailPage, GenresPage, SearchPage, ChapterReaderPage, and NovelDetailPage.
- Introduce new utility functions for API requests in server-api.ts, including error handling.
- Update authentication flow in auth.ts to sync Google login with the reader API.
- Modify NextAuth session and JWT types to include additional user information.
- Clean up unused imports and code related to Prisma and MongoDB connections.
- Adjust the configuration in next.config.mjs to remove unnecessary API routes.
This commit is contained in:
2026-03-30 13:54:51 +07:00
parent f9bb247ff1
commit 41aca718c9
12 changed files with 515 additions and 749 deletions
+30 -31
View File
@@ -1,11 +1,36 @@
import Link from "next/link"
import { ChevronLeft } from "lucide-react"
import { prisma } from "@/lib/prisma"
import { NovelCard } from "@/components/novel-card"
import { notFound } from "next/navigation"
import { readerApiFetch, readerApiFetchNullable } from "@/lib/server-api"
export const dynamic = "force-dynamic"
type GenreItem = {
id: string
name: string
slug: string
description: string | null
}
type BrowseNovel = {
id: string
slug: string
title: string
authorName: string
coverColor: string | null
coverUrl: string | null
rating: number
views: number
totalChapters: number
status: string
seriesId?: string | null
}
type BrowseResponse = {
items: BrowseNovel[]
}
function collapseSeriesRows<T extends { id: string; seriesId?: string | null }>(rows: T[]): T[] {
const pickedSeries = new Set<string>()
const output: T[] = []
@@ -27,42 +52,16 @@ function collapseSeriesRows<T extends { id: string; seriesId?: string | null }>(
export default async function GenreDetailPage({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params
const genre = await prisma.genre.findUnique({
where: { slug }
})
const genres = await readerApiFetch<GenreItem[]>("/api/genres")
const genre = genres.find((item) => item.slug === slug) || null
if (!genre) {
notFound()
}
const allNovelsRaw = await prisma.novel.findMany({
where: {
genres: {
some: {
genreId: genre.id
}
}
},
select: {
id: true,
slug: true,
title: true,
authorName: true,
coverColor: true,
coverUrl: true,
rating: true,
views: true,
totalChapters: true,
status: true,
seriesId: true,
},
orderBy: {
updatedAt: "desc"
},
take: 80
})
const browse = await readerApiFetch<BrowseResponse>(`/api/novels/browse?genre=${encodeURIComponent(slug)}&sort=latest&page=1&limit=80`)
const allNovels = collapseSeriesRows(allNovelsRaw).slice(0, 20)
const allNovels = collapseSeriesRows(browse.items).slice(0, 20)
// Basic layout without sort for purely server side representation without search params. Optional searchParams can be added later if needed.
return (
+13 -10
View File
@@ -1,6 +1,6 @@
import Link from "next/link"
import { BookOpen, Sparkles, Flame, Heart, Swords, Building2, Rocket, Crown, Laugh, Search, Shield } from "lucide-react"
import { prisma } from "@/lib/prisma"
import { readerApiFetch } from "@/lib/server-api"
const iconMap: Record<string, React.ReactNode> = {
Sparkles: <Sparkles className="h-6 w-6" />,
@@ -17,17 +17,20 @@ const iconMap: Record<string, React.ReactNode> = {
export const dynamic = "force-dynamic"
type GenreItem = {
id: string
name: string
slug: string
description: string | null
icon: string | null
novelCount: number
}
export default async function GenresPage() {
let genres: any[] = []
let genres: GenreItem[] = []
try {
genres = await prisma.genre.findMany({
include: {
_count: {
select: { novels: true }
}
}
})
genres = await readerApiFetch<GenreItem[]>("/api/genres")
} catch (error) {
console.error("Failed to fetch genres during build/runtime", error)
}
@@ -37,7 +40,7 @@ export default async function GenresPage() {
<h1 className="mb-6 text-2xl font-bold text-foreground">Thể Loại Truyện</h1>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
{genres.map((genre) => {
const novelCount = genre._count.novels
const novelCount = genre.novelCount
return (
<Link
key={genre.id}