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:
@@ -0,0 +1,47 @@
|
||||
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 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user