Refactor API routes and remove unused endpoints

- Deleted the following API routes:
  - `upload-cover`
  - `truyen/[id]/chapters`
  - `truyen/[id]/comments`
  - `truyen/[id]/rate`
  - `truyen/suggest`
  - `user/bookmarks`
  - `user/recommendations`
  - `user/settings`

- Updated `chapter-client.tsx` and `novel-client.tsx` to handle error messages more gracefully.
- Adjusted pagination logic in `tim-kiem/page.tsx` to improve user experience.
- Added support for additional search parameters in the search functionality.
- Introduced API rewrites in `next.config.mjs` to route requests to a reader API.
This commit is contained in:
2026-03-30 11:35:03 +07:00
parent ffd177718f
commit f9bb247ff1
33 changed files with 178 additions and 4842 deletions
-52
View File
@@ -1,52 +0,0 @@
import { NextResponse } from "next/server"
import connectToMongoDB from "@/lib/mongoose"
import { Chapter } from "@/lib/models/chapter"
export async function GET(
req: Request,
{ params }: { params: Promise<{ id: string }> } // `id` is the `novel.id`
) {
try {
const { id: novelId } = await params
const { searchParams } = new URL(req.url)
const page = parseInt(searchParams.get("page") || "1", 10)
const limit = parseInt(searchParams.get("limit") || "100", 10)
await connectToMongoDB()
const skip = (page - 1) * limit
const [chapters, totalChapters] = await Promise.all([
Chapter.find({ novelId })
.sort({ number: 1 })
.skip(skip)
.limit(limit)
.select("number title createdAt volumeNumber volumeTitle volumeChapterNumber") // don't return content
.lean(),
Chapter.countDocuments({ novelId })
])
return NextResponse.json({
chapters: chapters.map(c => ({
id: c._id.toString(),
number: c.number,
title: c.title,
volumeNumber: (c as any).volumeNumber ?? null,
volumeTitle: (c as any).volumeTitle ?? null,
volumeChapterNumber: (c as any).volumeChapterNumber ?? null,
createdAt: (c.createdAt as Date).toISOString()
})),
totalChapters,
totalPages: Math.ceil(totalChapters / limit),
currentPage: page
})
} catch (error: any) {
console.error("Fetch novel chapters error:", error)
return NextResponse.json(
{ error: "Không thể lấy danh sách chương" },
{ status: 500 }
)
}
}
-47
View File
@@ -1,47 +0,0 @@
import { NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth"
import { prisma } from "@/lib/prisma"
export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
const { id: novelId } = await params
const body = await req.json()
const { content, chapterId } = body
if (!content || typeof content !== "string") {
return NextResponse.json({ error: "Content is required" }, { status: 400 })
}
const newComment = await prisma.comment.create({
data: {
content: content.trim(),
userId: session.user.id,
novelId,
chapterId: chapterId || null
},
include: {
user: true
}
})
return NextResponse.json({
id: newComment.id,
userId: newComment.user.id,
username: newComment.user.name || "User",
avatarColor: newComment.user.image || "bg-primary",
novelId: newComment.novelId,
chapterId: newComment.chapterId,
content: newComment.content,
createdAt: newComment.createdAt.toISOString().split("T")[0]
})
} catch (error) {
console.error("POST Comment Error", error)
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 })
}
}
-44
View File
@@ -1,44 +0,0 @@
import { NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params
const body = await req.json()
const { score } = body
if (typeof score !== 'number' || score < 1 || score > 5) {
return NextResponse.json({ error: "Invalid score" }, { status: 400 })
}
// Fetch current rating
const novel = await prisma.novel.findUnique({
where: { id },
select: { rating: true, ratingCount: true }
})
if (!novel) {
return NextResponse.json({ error: "Novel not found" }, { status: 404 })
}
const { rating, ratingCount } = novel
const newRatingCount = ratingCount + 1
const newRating = ((rating * ratingCount) + score) / newRatingCount
const updatedNovel = await prisma.novel.update({
where: { id },
data: {
rating: newRating,
ratingCount: newRatingCount
}
})
return NextResponse.json({
rating: updatedNovel.rating,
ratingCount: updatedNovel.ratingCount
})
} catch (error) {
console.error("Rating Error", error)
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 })
}
}
-42
View File
@@ -1,42 +0,0 @@
import { NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
export async function GET(req: Request) {
try {
const url = new URL(req.url)
const q = url.searchParams.get("q")?.trim() || ""
if (q.length < 2) {
return NextResponse.json([])
}
const novels = await prisma.novel.findMany({
where: {
OR: [
{ title: { contains: q, mode: "insensitive" } },
{ authorName: { contains: q, mode: "insensitive" } },
{ series: { name: { contains: q, mode: "insensitive" } } },
],
},
select: {
id: true,
title: true,
slug: true,
authorName: true,
coverUrl: true,
series: {
select: {
id: true,
name: true,
},
},
},
orderBy: [{ views: "desc" }, { updatedAt: "desc" }],
take: 8,
})
return NextResponse.json(novels)
} catch {
return NextResponse.json({ error: "Failed to fetch suggestions" }, { status: 500 })
}
}