import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"

// 创建新闻
export async function POST(request: NextRequest) {
  try {
    const data = await request.json()
    const news = await prisma.news.create({
      data: {
        title: data.title,
        content: data.content,
        type: data.type || "news",
        published: data.published || false,
      },
    })
    return NextResponse.json(news)
  } catch (error) {
    return NextResponse.json({ error: "创建失败" }, { status: 500 })
  }
}

// 获取所有新闻
export async function GET() {
  try {
    const news = await prisma.news.findMany({
      orderBy: { createdAt: "desc" },
    })
    return NextResponse.json(news)
  } catch (error) {
    return NextResponse.json({ error: "获取失败" }, { status: 500 })
  }
}
