75ed8e233b
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.
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
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 })
|
|
}
|
|
}
|