Add moderation APIs and admin UI

Add moderator/admin backend APIs and client features for managing novels and chapters. New endpoints include mod chapter routes (paginated list, single GET, PUT, DELETE, and bulk optimize), mod novel routes (create, GET by id, update, delete), genre CRUD, user bookmarks, novel comments, and rating endpoints. Update EPUB import to use a shared slugify util. Enhance moderator UI: chapter manager gains pagination, bulk optimization preview/apply, edit/delete dialogs; novel client adds genre management and edit/delete flows. Also update Prisma schema, add a DB wipe script, remove unused lib/data.ts, and adjust related types/utils and bookmark context.
This commit is contained in:
2026-03-06 17:30:56 +07:00
parent ce805adb08
commit 75ed8e233b
31 changed files with 1853 additions and 687 deletions
+46
View File
@@ -0,0 +1,46 @@
import { NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth"
import connectToMongoDB from "@/lib/mongoose"
import { Chapter } from "@/lib/models/chapter"
import { prisma } from "@/lib/prisma"
export async function GET(
req: Request,
context: { params: Promise<{ id: string }> }
) {
const { id } = await context.params
const session = await getServerSession(authOptions)
if (!session || (session.user.role !== "MOD" && session.user.role !== "ADMIN")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
await connectToMongoDB()
// console.log("Fetching chapter with ID:", id)
const chapter = await Chapter.findById(id)
if (!chapter) {
// console.log("Chapter not found in DB")
return NextResponse.json({ error: "Chapter not found" }, { status: 404 })
}
// Verify the moderator owns the related novel
const novel = await prisma.novel.findFirst({
where: {
id: chapter.novelId,
uploaderId: session.user.id
}
})
if (!novel) {
return NextResponse.json({ error: "Unauthorized access to this chapter" }, { status: 403 })
}
return NextResponse.json(chapter)
} catch (error) {
console.error("GET Chapter error:", error)
return NextResponse.json({ error: "Failed to fetch chapter details" }, { status: 500 })
}
}
+51
View File
@@ -0,0 +1,51 @@
import { NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth"
import connectToMongoDB from "@/lib/mongoose"
import { Chapter } from "@/lib/models/chapter"
export async function PUT(req: Request) {
const session = await getServerSession(authOptions)
if (!session || (session.user.role !== "MOD" && session.user.role !== "ADMIN")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
const body = await req.json()
const { novelId, updates } = body
if (!novelId || !updates || !Array.isArray(updates)) {
return NextResponse.json({ error: "Tham số không hợp lệ" }, { status: 400 })
}
await connectToMongoDB()
// Prepare bulk operations for mongoose
const bulkOps = updates.map((update: any) => ({
updateOne: {
filter: { _id: update.id, novelId: novelId },
update: {
$set: {
number: update.number,
title: update.title
}
}
}
}))
if (bulkOps.length === 0) {
return NextResponse.json({ message: "Không có thay đổi nào" }, { status: 200 })
}
const result = await Chapter.bulkWrite(bulkOps)
return NextResponse.json({
message: "Cập nhật thành công",
modifiedCount: result.modifiedCount
}, { status: 200 })
} catch (error: any) {
console.error("Bulk optimize error:", error)
return NextResponse.json({ error: "Lỗi cập nhật hàng loạt", details: error.message }, { status: 500 })
}
}
+103 -2
View File
@@ -8,6 +8,8 @@ import { prisma } from "@/lib/prisma"
export async function GET(req: Request) {
const { searchParams } = new URL(req.url)
const novelId = searchParams.get("novelId")
const page = parseInt(searchParams.get("page") || "1")
const limit = parseInt(searchParams.get("limit") || "20")
if (!novelId) {
return NextResponse.json({ error: "novelId is required" }, { status: 400 })
@@ -15,8 +17,23 @@ export async function GET(req: Request) {
try {
await connectToMongoDB()
const chapters = await Chapter.find({ novelId }).sort({ number: 1 }).select("-content")
return NextResponse.json(chapters)
const skip = (page - 1) * limit
const [chapters, totalChapters] = await Promise.all([
Chapter.find({ novelId })
.sort({ number: 1 })
.skip(skip)
.limit(limit)
.select("-content"),
Chapter.countDocuments({ novelId })
])
return NextResponse.json({
chapters,
totalChapters,
totalPages: Math.ceil(totalChapters / limit),
currentPage: page
})
} catch (error) {
console.error("GET Chapter Error:", error)
return NextResponse.json({ error: "Failed to fetch chapters" }, { status: 500 })
@@ -70,3 +87,87 @@ export async function POST(req: Request) {
return NextResponse.json({ error: "Failed to create chapter" }, { status: 500 })
}
}
export async function PUT(req: Request) {
const session = await getServerSession(authOptions)
if (!session || (session.user.role !== "MOD" && session.user.role !== "ADMIN")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
const data = await req.json()
const { id, novelId, number, title, content } = data
// Xác minh truyện thuộc về Mod này
const novel = await prisma.novel.findFirst({
where: { id: novelId, uploaderId: session.user.id },
})
if (!novel) {
return NextResponse.json({ error: "Truyện không tồn tại hoặc không đủ quyền" }, { status: 403 })
}
await connectToMongoDB()
const updatedChapter = await Chapter.findOneAndUpdate(
{ _id: id, novelId },
{ number, title, content },
{ new: true }
)
if (!updatedChapter) {
return NextResponse.json({ error: "Không tìm thấy chương" }, { status: 404 })
}
return NextResponse.json(updatedChapter)
} catch (error) {
console.error("PUT Chapter Error:", error)
return NextResponse.json({ error: "Failed to update chapter" }, { status: 500 })
}
}
export async function DELETE(req: Request) {
const session = await getServerSession(authOptions)
if (!session || (session.user.role !== "MOD" && session.user.role !== "ADMIN")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
const url = new URL(req.url)
const id = url.searchParams.get("id")
const novelId = url.searchParams.get("novelId")
if (!id || !novelId) {
return NextResponse.json({ error: "Thiếu ID chương hoặc ID truyện" }, { status: 400 })
}
// Xác minh truyện thuộc về Mod này
const novel = await prisma.novel.findFirst({
where: { id: novelId, uploaderId: session.user.id },
})
if (!novel) {
return NextResponse.json({ error: "Truyện không tồn tại hoặc không đủ quyền" }, { status: 403 })
}
await connectToMongoDB()
const deletedChapter = await Chapter.findOneAndDelete({ _id: id, novelId })
if (!deletedChapter) {
return NextResponse.json({ error: "Không tìm thấy chương" }, { status: 404 })
}
// Cập nhật lại số lượng chương trong Postgres
const totalChapters = await Chapter.countDocuments({ novelId })
await prisma.novel.update({
where: { id: novelId },
data: { totalChapters },
})
return NextResponse.json({ message: "Đã xóa chương thành công" })
} catch (error) {
console.error("DELETE Chapter Error:", error)
return NextResponse.json({ error: "Failed to delete chapter" }, { status: 500 })
}
}