first commit
This commit is contained in:
54
app/api/admin/products/route.ts
Normal file
54
app/api/admin/products/route.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { name, description, price, discount, images, stock, sku, isActive, categoryId } = body
|
||||
|
||||
// Check if SKU already exists
|
||||
const existingSku = await prisma.product.findUnique({
|
||||
where: { sku }
|
||||
})
|
||||
|
||||
if (existingSku) {
|
||||
return NextResponse.json(
|
||||
{ error: 'SKU already exists' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const product = await prisma.product.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
price,
|
||||
discount,
|
||||
images,
|
||||
stock,
|
||||
sku,
|
||||
isActive,
|
||||
categoryId,
|
||||
slug: name.toLowerCase().replace(/[^a-z0-9\s-]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-') + '-' + Date.now()
|
||||
},
|
||||
include: {
|
||||
category: true
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(product)
|
||||
} catch (error) {
|
||||
console.error('Error creating product:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create product' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user