198 lines
5.1 KiB
TypeScript
198 lines
5.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
||
import { getServerSession } from 'next-auth';
|
||
import { authOptions } from '@/lib/auth';
|
||
import { prisma } from '@/lib/prisma';
|
||
|
||
// GET - 获取所有使用店铺分类
|
||
export async function GET() {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
if (!session?.user) {
|
||
return NextResponse.json({ error: '未登录' }, { status: 401 });
|
||
}
|
||
|
||
const categories = await prisma.category.findMany({
|
||
orderBy: { name: 'asc' },
|
||
});
|
||
|
||
// 统计每个分类的素材数量(只统计未删除的素材)
|
||
const categoriesWithCount = await Promise.all(
|
||
categories.map(async (category) => {
|
||
let count: number;
|
||
try {
|
||
count = await prisma.material.count({
|
||
where: {
|
||
categoryName: category.name,
|
||
deletedAt: null,
|
||
} as any,
|
||
});
|
||
} catch (e: any) {
|
||
// 兼容旧版本数据库(没有deletedAt字段)
|
||
if (e?.meta?.column?.includes('deletedAt')) {
|
||
count = await prisma.material.count({
|
||
where: { categoryName: category.name },
|
||
});
|
||
} else {
|
||
throw e;
|
||
}
|
||
}
|
||
return {
|
||
...category,
|
||
count,
|
||
};
|
||
})
|
||
);
|
||
|
||
return NextResponse.json(categoriesWithCount);
|
||
} catch (error) {
|
||
console.error('Error fetching categories:', error);
|
||
return NextResponse.json(
|
||
{ error: '获取店铺列表失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
// POST - 创建新的使用店铺分类
|
||
export async function POST(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
|
||
if (!session) {
|
||
return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
||
}
|
||
|
||
const { name } = await request.json();
|
||
|
||
if (!name || !name.trim()) {
|
||
return NextResponse.json(
|
||
{ error: '店铺名称不能为空' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 检查是否已存在
|
||
const existing = await prisma.category.findUnique({
|
||
where: { name: name.trim() },
|
||
});
|
||
|
||
if (existing) {
|
||
return NextResponse.json(
|
||
{ error: '该店铺已存在' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const category = await prisma.category.create({
|
||
data: { name: name.trim() },
|
||
});
|
||
|
||
return NextResponse.json(category, { status: 201 });
|
||
} catch (error) {
|
||
console.error('Error creating category:', error);
|
||
return NextResponse.json(
|
||
{ error: '创建店铺失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
// PATCH - 更新使用店铺分类
|
||
export async function PATCH(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
const userRole = (session.user as any).role;
|
||
if (userRole !== 'ADMIN') {
|
||
return NextResponse.json({ error: '权限不足' }, { status: 403 });
|
||
}
|
||
|
||
const { id, name } = await request.json();
|
||
|
||
if (!id || !name || !name.trim()) {
|
||
return NextResponse.json({ error: '参数错误' }, { status: 400 });
|
||
}
|
||
|
||
// 检查新名称是否已被其他分类使用
|
||
const existing = await prisma.category.findFirst({
|
||
where: {
|
||
name: name.trim(),
|
||
NOT: { id },
|
||
},
|
||
});
|
||
|
||
if (existing) {
|
||
return NextResponse.json(
|
||
{ error: '该店铺名称已存在' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 获取旧名称
|
||
const oldCategory = await prisma.category.findUnique({
|
||
where: { id },
|
||
});
|
||
|
||
if (!oldCategory) {
|
||
return NextResponse.json({ error: '分类不存在' }, { status: 404 });
|
||
}
|
||
|
||
// 更新分类
|
||
const updated = await prisma.category.update({
|
||
where: { id },
|
||
data: { name: name.trim() },
|
||
});
|
||
|
||
// 更新所有使用该分类的素材
|
||
await prisma.material.updateMany({
|
||
where: { categoryName: oldCategory.name },
|
||
data: { categoryName: name.trim() },
|
||
});
|
||
|
||
return NextResponse.json(updated);
|
||
} catch (error) {
|
||
console.error('Error updating category:', error);
|
||
return NextResponse.json({ error: '更新失败' }, { status: 500 });
|
||
}
|
||
}
|
||
|
||
// DELETE - 删除使用店铺分类
|
||
export async function DELETE(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
const userRole = (session.user as any).role;
|
||
if (userRole !== 'ADMIN') {
|
||
return NextResponse.json({ error: '权限不足' }, { status: 403 });
|
||
}
|
||
|
||
const { searchParams } = new URL(request.url);
|
||
const id = searchParams.get('id');
|
||
|
||
if (!id) {
|
||
return NextResponse.json({ error: '缺少ID参数' }, { status: 400 });
|
||
}
|
||
|
||
await prisma.category.delete({
|
||
where: { id },
|
||
});
|
||
|
||
return NextResponse.json({ success: true });
|
||
} catch (error) {
|
||
console.error('Error deleting category:', error);
|
||
return NextResponse.json({ error: '删除失败' }, { status: 500 });
|
||
}
|
||
}
|