70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
||
import { prisma } from '@/lib/prisma'
|
||
import { ComponentService } from '@/lib/services/component-service'
|
||
|
||
export async function GET(request: NextRequest) {
|
||
try {
|
||
const { searchParams } = new URL(request.url)
|
||
const typeId = searchParams.get('type') // 注意:这里仍然叫type,但实际是typeId
|
||
const brand = searchParams.get('brand')
|
||
const minPrice = searchParams.get('minPrice')
|
||
const maxPrice = searchParams.get('maxPrice')
|
||
const search = searchParams.get('search')
|
||
const page = parseInt(searchParams.get('page') || '1')
|
||
const limit = parseInt(searchParams.get('limit') || '12')
|
||
|
||
const result = await ComponentService.queryComponents({
|
||
typeId: typeId || undefined, // 将null转换为undefined
|
||
brand: brand || undefined,
|
||
minPrice: minPrice ? parseFloat(minPrice) : undefined,
|
||
maxPrice: maxPrice ? parseFloat(maxPrice) : undefined,
|
||
search: search || undefined,
|
||
page,
|
||
limit
|
||
})
|
||
|
||
return NextResponse.json({
|
||
components: result.components,
|
||
pagination: result.pagination
|
||
})
|
||
} catch (error) {
|
||
console.error('Components fetch error:', error)
|
||
return NextResponse.json(
|
||
{ message: '获取配件列表失败' },
|
||
{ status: 500 }
|
||
)
|
||
}
|
||
}
|
||
|
||
export async function POST(request: NextRequest) {
|
||
try {
|
||
// 这个API需要管理员权限,暂时简化处理
|
||
const data = await request.json()
|
||
|
||
const component = await prisma.component.create({
|
||
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, { status: 201 })
|
||
} catch (error) {
|
||
console.error('Component create error:', error)
|
||
return NextResponse.json(
|
||
{ message: '创建配件失败' },
|
||
{ status: 500 }
|
||
)
|
||
}
|
||
}
|