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

// 更新新闻
export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const data = await request.json()
    const news = await prisma.news.update({
      where: { id: parseInt(id) },
      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 DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    await prisma.news.delete({
      where: { id: parseInt(id) },
    })
    return NextResponse.json({ success: true })
  } catch (error) {
    return NextResponse.json({ error: "删除失败" }, { status: 500 })
  }
}
