Initial commit

This commit is contained in:
2026-03-05 16:46:38 +07:00
commit 112e8604e2
124 changed files with 14369 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
import NextAuth from "next-auth"
import { authOptions } from "@/lib/auth"
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
+30
View File
@@ -0,0 +1,30 @@
import { NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth"
import { prisma } from "@/lib/prisma"
export async function GET() {
if (process.env.NODE_ENV === "production") {
return NextResponse.json({ error: "Not found" }, { status: 404 })
}
const session = await getServerSession(authOptions)
if (!session || !session.user || !session.user.email) {
return NextResponse.json({ error: "Bạn phải đăng nhập trước" }, { status: 401 })
}
try {
const updatedUser = await prisma.user.update({
where: { email: session.user.email },
data: { role: "MOD" },
})
return NextResponse.json({
message: `Tài khoản ${updatedUser.email} đã được cấp quyền MOD. Xin hãy Đăng xuất và Đăng nhập lại để cập nhật phiên.`,
user: updatedUser
})
} catch (error) {
return NextResponse.json({ error: "Lỗi hệ thống" }, { status: 500 })
}
}
+72
View File
@@ -0,0 +1,72 @@
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) {
const { searchParams } = new URL(req.url)
const novelId = searchParams.get("novelId")
if (!novelId) {
return NextResponse.json({ error: "novelId is required" }, { status: 400 })
}
try {
await connectToMongoDB()
const chapters = await Chapter.find({ novelId }).sort({ number: 1 }).select("-content")
return NextResponse.json(chapters)
} catch (error) {
console.error("GET Chapter Error:", error)
return NextResponse.json({ error: "Failed to fetch chapters" }, { status: 500 })
}
}
export async function POST(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 { 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()
// Kiểm tra chương đã tồn tại
const existingChapter = await Chapter.findOne({ novelId, number })
if (existingChapter) {
return NextResponse.json({ error: "Chương này đã tồn tại" }, { status: 400 })
}
const newChapter = await Chapter.create({
novelId,
number,
title,
content,
})
// Cập nhật số chương trong table PostgreSQL, tự động đếm lại
const totalChapters = await Chapter.countDocuments({ novelId })
await prisma.novel.update({
where: { id: novelId },
data: { totalChapters },
})
return NextResponse.json(newChapter, { status: 201 })
} catch (error) {
console.error("POST Chapter Error:", error)
return NextResponse.json({ error: "Failed to create chapter" }, { status: 500 })
}
}
+52
View File
@@ -0,0 +1,52 @@
import { NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth"
import { prisma } from "@/lib/prisma"
export async function GET() {
const session = await getServerSession(authOptions)
if (!session || (session.user.role !== "MOD" && session.user.role !== "ADMIN")) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
const novels = await prisma.novel.findMany({
where: { uploaderId: session.user.id },
orderBy: { updatedAt: "desc" },
})
return NextResponse.json(novels)
} catch (error) {
return NextResponse.json({ error: "Failed to fetch novels" }, { status: 500 })
}
}
export async function POST(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()
// Tạo slug từ title
const slug = data.title
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-$)+/g, "")
const newNovel = await prisma.novel.create({
data: {
title: data.title,
slug: slug,
authorName: data.authorName,
description: data.description,
uploaderId: session.user.id,
},
})
return NextResponse.json(newNovel, { status: 201 })
} catch (error) {
return NextResponse.json({ error: "Failed to create novel" }, { status: 500 })
}
}