first commit
This commit is contained in:
195
app/api/admin/categories/[id]/route.ts
Normal file
195
app/api/admin/categories/[id]/route.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
|
||||
// Check if category exists
|
||||
const existingCategory = await prisma.category.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
products: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingCategory) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Category not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// Check if category has products
|
||||
if (existingCategory._count.products > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Cannot delete category with associated products',
|
||||
productCount: existingCategory._count.products
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Delete the category
|
||||
await prisma.category.delete({
|
||||
where: { id }
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Category deleted successfully',
|
||||
deletedCategory: {
|
||||
id: existingCategory.id,
|
||||
name: existingCategory.name
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
console.error('Error deleting category:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete category' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const body = await request.json()
|
||||
const { name, description, image, isActive } = body
|
||||
|
||||
// Check if category exists
|
||||
const existingCategory = await prisma.category.findUnique({
|
||||
where: { id }
|
||||
})
|
||||
|
||||
if (!existingCategory) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Category not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// If name is being updated, check for uniqueness
|
||||
if (name && name !== existingCategory.name) {
|
||||
const nameExists = await prisma.category.findUnique({
|
||||
where: { name }
|
||||
})
|
||||
|
||||
if (nameExists) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Category name already exists' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Update the category
|
||||
const updatedCategory = await prisma.category.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(name !== undefined && { name }),
|
||||
...(description !== undefined && { description }),
|
||||
...(image !== undefined && { image }),
|
||||
...(isActive !== undefined && { isActive })
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
products: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(updatedCategory)
|
||||
} catch (error: any) {
|
||||
console.error('Error updating category:', error)
|
||||
|
||||
// Handle Prisma unique constraint errors
|
||||
if (error.code === 'P2002') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Category name already exists' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update category' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
|
||||
const category = await prisma.category.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
products: true
|
||||
}
|
||||
},
|
||||
products: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
price: true,
|
||||
isActive: true
|
||||
},
|
||||
take: 10 // Limit to first 10 products for preview
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (!category) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Category not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json(category)
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching category:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch category' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
73
app/api/admin/categories/route.ts
Normal file
73
app/api/admin/categories/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const search = searchParams.get('search')
|
||||
|
||||
const categories = await prisma.category.findMany({
|
||||
where: search ? {
|
||||
name: {
|
||||
contains: search,
|
||||
mode: 'insensitive'
|
||||
}
|
||||
} : {},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
products: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(categories)
|
||||
} catch (error) {
|
||||
console.error('Error fetching categories:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch categories' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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, image, isActive } = body
|
||||
|
||||
const category = await prisma.category.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
image,
|
||||
isActive
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(category)
|
||||
} catch (error) {
|
||||
console.error('Error creating category:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create category' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user