v1
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params
|
||||
|
||||
const component = await prisma.component.findUnique({
|
||||
where: { id: id },
|
||||
include: {
|
||||
componentType: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!component) {
|
||||
return NextResponse.json(
|
||||
{ message: '配件不存在' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json(component)
|
||||
} catch (error) {
|
||||
console.error('Component fetch error:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '获取配件详情失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params
|
||||
const data = await request.json()
|
||||
|
||||
const component = await prisma.component.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
name: data.name,
|
||||
brand: data.brand,
|
||||
model: data.model,
|
||||
price: parseFloat(data.price),
|
||||
description: data.description,
|
||||
imageUrl: data.imageUrl,
|
||||
stock: parseInt(data.stock),
|
||||
specifications: data.specifications,
|
||||
componentTypeId: data.componentTypeId
|
||||
},
|
||||
include: {
|
||||
componentType: true
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(component)
|
||||
} catch (error) {
|
||||
console.error('Component update error:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '更新配件失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params
|
||||
|
||||
await prisma.component.delete({
|
||||
where: { id: id }
|
||||
})
|
||||
|
||||
return NextResponse.json({ message: '删除成功' })
|
||||
} catch (error) {
|
||||
console.error('Component delete error:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '删除配件失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user