refactor: 引入 apiHandler + ApiError,消除 20 个路由的 try/catch 样板
- 新增 src/lib/api.ts:ApiError 错误类 + apiHandler 统一包装器 - 20 个 API 路由统一使用 apiHandler,删除重复的 try/catch 块 - 验证错误改用 throw new ApiError(),减少嵌套层级 - join/manage 路由的错误码映射改为直接抛出 ApiError - 删除已无引用的 errorResponse 辅助函数 - 净减 273 行代码
This commit is contained in:
@@ -1,51 +1,42 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { errorResponse, validateMembership } from "@/lib/blindbox";
|
||||
import { validateMembership } from "@/lib/blindbox";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { roomId, userId } = await req.json();
|
||||
export const POST = apiHandler(async (req) => {
|
||||
const { roomId, userId } = await req.json();
|
||||
|
||||
if (!userId || typeof userId !== "string") {
|
||||
return errorResponse("请先登录", 401);
|
||||
}
|
||||
if (!roomId || typeof roomId !== "string") {
|
||||
return errorResponse("roomId 不能为空", 400);
|
||||
}
|
||||
if (!userId || typeof userId !== "string") throw new ApiError("请先登录", 401);
|
||||
if (!roomId || typeof roomId !== "string") throw new ApiError("roomId 不能为空");
|
||||
|
||||
const isMember = await validateMembership(roomId, userId);
|
||||
if (!isMember) {
|
||||
return errorResponse("你不是这个房间的成员", 403);
|
||||
}
|
||||
const isMember = await validateMembership(roomId, userId);
|
||||
if (!isMember) throw new ApiError("你不是这个房间的成员", 403);
|
||||
|
||||
const pool = await prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, status: "in_pool" },
|
||||
select: { id: true },
|
||||
});
|
||||
const pool = await prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, status: "in_pool" },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (pool.length === 0) {
|
||||
return errorResponse("盒子是空的,先往里面塞点想法吧!", 404);
|
||||
}
|
||||
|
||||
const picked = pool[Math.floor(Math.random() * pool.length)];
|
||||
|
||||
const idea = await prisma.blindBoxIdea.update({
|
||||
where: { id: picked.id },
|
||||
data: { status: "drawn", drawnById: userId },
|
||||
include: {
|
||||
user: { select: { id: true, username: true, avatar: true } },
|
||||
drawnBy: { select: { id: true, username: true, avatar: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
id: idea.id,
|
||||
content: idea.content,
|
||||
createdAt: idea.createdAt,
|
||||
submitter: idea.user,
|
||||
drawnBy: idea.drawnBy,
|
||||
});
|
||||
} catch {
|
||||
return errorResponse("抽取失败", 500);
|
||||
if (pool.length === 0) {
|
||||
throw new ApiError("盒子是空的,先往里面塞点想法吧!", 404);
|
||||
}
|
||||
}
|
||||
|
||||
const picked = pool[Math.floor(Math.random() * pool.length)];
|
||||
|
||||
const idea = await prisma.blindBoxIdea.update({
|
||||
where: { id: picked.id },
|
||||
data: { status: "drawn", drawnById: userId },
|
||||
include: {
|
||||
user: { select: { id: true, username: true, avatar: true } },
|
||||
drawnBy: { select: { id: true, username: true, avatar: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
id: idea.id,
|
||||
content: idea.content,
|
||||
createdAt: idea.createdAt,
|
||||
submitter: idea.user,
|
||||
drawnBy: idea.drawnBy,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,63 +1,48 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { errorResponse, getRoomByCode } from "@/lib/blindbox";
|
||||
import { getRoomByCode } from "@/lib/blindbox";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ code: string }> },
|
||||
) {
|
||||
try {
|
||||
const { code } = await params;
|
||||
const room = await getRoomByCode(code.toUpperCase());
|
||||
export const GET = apiHandler(async (_req, { params }) => {
|
||||
const { code } = await params;
|
||||
const room = await getRoomByCode(code.toUpperCase());
|
||||
|
||||
if (!room) {
|
||||
return errorResponse("房间不存在", 404);
|
||||
}
|
||||
if (!room) throw new ApiError("房间不存在", 404);
|
||||
|
||||
return NextResponse.json({
|
||||
id: room.id,
|
||||
code: room.code,
|
||||
name: room.name,
|
||||
creatorId: room.creatorId,
|
||||
poolCount: room._count.ideas,
|
||||
members: room.members.map((m) => ({
|
||||
...m.user,
|
||||
joinedAt: m.joinedAt,
|
||||
})),
|
||||
});
|
||||
} catch {
|
||||
return errorResponse("获取房间信息失败", 500);
|
||||
return NextResponse.json({
|
||||
id: room.id,
|
||||
code: room.code,
|
||||
name: room.name,
|
||||
creatorId: room.creatorId,
|
||||
poolCount: room._count.ideas,
|
||||
members: room.members.map((m) => ({
|
||||
...m.user,
|
||||
joinedAt: m.joinedAt,
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
export const DELETE = apiHandler(async (req, { params }) => {
|
||||
const { code } = await params;
|
||||
const { userId } = await req.json();
|
||||
|
||||
if (!userId) throw new ApiError("缺少用户 ID");
|
||||
|
||||
const room = await prisma.blindBoxRoom.findUnique({
|
||||
where: { code: code.toUpperCase() },
|
||||
});
|
||||
if (!room) throw new ApiError("房间不存在", 404);
|
||||
|
||||
if (room.creatorId === userId) {
|
||||
await prisma.blindBoxRoom.delete({ where: { id: room.id } });
|
||||
return NextResponse.json({ action: "deleted" });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ code: string }> },
|
||||
) {
|
||||
try {
|
||||
const { code } = await params;
|
||||
const { userId } = await req.json();
|
||||
const membership = await prisma.blindBoxMember.findUnique({
|
||||
where: { roomId_userId: { roomId: room.id, userId } },
|
||||
});
|
||||
if (!membership) throw new ApiError("你不是该房间成员", 403);
|
||||
|
||||
if (!userId) return errorResponse("缺少用户 ID", 400);
|
||||
|
||||
const room = await prisma.blindBoxRoom.findUnique({
|
||||
where: { code: code.toUpperCase() },
|
||||
});
|
||||
if (!room) return errorResponse("房间不存在", 404);
|
||||
|
||||
if (room.creatorId === userId) {
|
||||
await prisma.blindBoxRoom.delete({ where: { id: room.id } });
|
||||
return NextResponse.json({ action: "deleted" });
|
||||
}
|
||||
|
||||
const membership = await prisma.blindBoxMember.findUnique({
|
||||
where: { roomId_userId: { roomId: room.id, userId } },
|
||||
});
|
||||
if (!membership) return errorResponse("你不是该房间成员", 403);
|
||||
|
||||
await prisma.blindBoxMember.delete({ where: { id: membership.id } });
|
||||
return NextResponse.json({ action: "left" });
|
||||
} catch {
|
||||
return errorResponse("操作失败", 500);
|
||||
}
|
||||
}
|
||||
await prisma.blindBoxMember.delete({ where: { id: membership.id } });
|
||||
return NextResponse.json({ action: "left" });
|
||||
});
|
||||
|
||||
@@ -1,38 +1,36 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { errorResponse } from "@/lib/blindbox";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { userId, code } = await req.json();
|
||||
export const POST = apiHandler(async (req) => {
|
||||
const { userId, code } = await req.json();
|
||||
|
||||
if (!userId || typeof userId !== "string") {
|
||||
return errorResponse("请先登录", 401);
|
||||
}
|
||||
if (!code || typeof code !== "string") {
|
||||
return errorResponse("请输入房间号", 400);
|
||||
}
|
||||
if (!userId || typeof userId !== "string") throw new ApiError("请先登录", 401);
|
||||
if (!code || typeof code !== "string") throw new ApiError("请输入房间号");
|
||||
|
||||
const room = await prisma.blindBoxRoom.findUnique({
|
||||
where: { code: code.trim().toUpperCase() },
|
||||
const room = await prisma.blindBoxRoom.findUnique({
|
||||
where: { code: code.trim().toUpperCase() },
|
||||
});
|
||||
if (!room) throw new ApiError("房间不存在,请检查房间号", 404);
|
||||
|
||||
const existing = await prisma.blindBoxMember.findUnique({
|
||||
where: { roomId_userId: { roomId: room.id, userId } },
|
||||
});
|
||||
if (existing) {
|
||||
return NextResponse.json({
|
||||
id: room.id,
|
||||
code: room.code,
|
||||
name: room.name,
|
||||
alreadyMember: true,
|
||||
});
|
||||
if (!room) {
|
||||
return errorResponse("房间不存在,请检查房间号", 404);
|
||||
}
|
||||
|
||||
const existing = await prisma.blindBoxMember.findUnique({
|
||||
where: { roomId_userId: { roomId: room.id, userId } },
|
||||
});
|
||||
if (existing) {
|
||||
return NextResponse.json({ id: room.id, code: room.code, name: room.name, alreadyMember: true });
|
||||
}
|
||||
|
||||
await prisma.blindBoxMember.create({
|
||||
data: { roomId: room.id, userId },
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: room.id, code: room.code, name: room.name }, { status: 201 });
|
||||
} catch {
|
||||
return errorResponse("加入房间失败", 500);
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.blindBoxMember.create({
|
||||
data: { roomId: room.id, userId },
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ id: room.id, code: room.code, name: room.name },
|
||||
{ status: 201 },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,40 +1,32 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { errorResponse, generateUniqueRoomCode } from "@/lib/blindbox";
|
||||
import { generateUniqueRoomCode } from "@/lib/blindbox";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { userId, name } = await req.json();
|
||||
export const POST = apiHandler(async (req) => {
|
||||
const { userId, name } = await req.json();
|
||||
|
||||
if (!userId || typeof userId !== "string") {
|
||||
return errorResponse("请先登录", 401);
|
||||
}
|
||||
if (!userId || typeof userId !== "string") throw new ApiError("请先登录", 401);
|
||||
|
||||
const roomName = (name || "").trim() || "我们的周末";
|
||||
if (roomName.length > 30) {
|
||||
return errorResponse("房间名不能超过 30 个字", 400);
|
||||
}
|
||||
const roomName = (name || "").trim() || "我们的周末";
|
||||
if (roomName.length > 30) throw new ApiError("房间名不能超过 30 个字");
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
return errorResponse("用户不存在", 404);
|
||||
}
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) throw new ApiError("用户不存在", 404);
|
||||
|
||||
const code = await generateUniqueRoomCode();
|
||||
const code = await generateUniqueRoomCode();
|
||||
|
||||
const room = await prisma.blindBoxRoom.create({
|
||||
data: {
|
||||
code,
|
||||
name: roomName,
|
||||
creatorId: userId,
|
||||
members: {
|
||||
create: { userId },
|
||||
},
|
||||
},
|
||||
});
|
||||
const room = await prisma.blindBoxRoom.create({
|
||||
data: {
|
||||
code,
|
||||
name: roomName,
|
||||
creatorId: userId,
|
||||
members: { create: { userId } },
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: room.id, code: room.code, name: room.name }, { status: 201 });
|
||||
} catch {
|
||||
return errorResponse("创建房间失败", 500);
|
||||
}
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ id: room.id, code: room.code, name: room.name },
|
||||
{ status: 201 },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,69 +1,59 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { errorResponse } from "@/lib/blindbox";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
export const GET = apiHandler(async (req) => {
|
||||
const userId = req.nextUrl.searchParams.get("userId");
|
||||
if (!userId) throw new ApiError("请先登录", 401);
|
||||
|
||||
if (!userId) {
|
||||
return errorResponse("请先登录", 401);
|
||||
}
|
||||
|
||||
try {
|
||||
const memberships = await prisma.blindBoxMember.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
room: {
|
||||
include: {
|
||||
members: {
|
||||
include: { user: { select: { id: true, username: true, avatar: true } } },
|
||||
orderBy: { joinedAt: "asc" },
|
||||
take: 5,
|
||||
},
|
||||
_count: {
|
||||
select: {
|
||||
ideas: true,
|
||||
members: true,
|
||||
},
|
||||
},
|
||||
ideas: {
|
||||
where: { status: "drawn" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 1,
|
||||
select: { content: true, createdAt: true },
|
||||
},
|
||||
const memberships = await prisma.blindBoxMember.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
room: {
|
||||
include: {
|
||||
members: {
|
||||
include: { user: { select: { id: true, username: true, avatar: true } } },
|
||||
orderBy: { joinedAt: "asc" },
|
||||
take: 5,
|
||||
},
|
||||
_count: {
|
||||
select: { ideas: true, members: true },
|
||||
},
|
||||
ideas: {
|
||||
where: { status: "drawn" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 1,
|
||||
select: { content: true, createdAt: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { joinedAt: "desc" },
|
||||
});
|
||||
},
|
||||
orderBy: { joinedAt: "desc" },
|
||||
});
|
||||
|
||||
const rooms = memberships.map((m) => ({
|
||||
id: m.room.id,
|
||||
code: m.room.code,
|
||||
name: m.room.name,
|
||||
memberCount: m.room._count.members,
|
||||
ideaCount: m.room._count.ideas,
|
||||
poolCount: 0,
|
||||
members: m.room.members.map((mb) => mb.user),
|
||||
lastDrawn: m.room.ideas[0] ?? null,
|
||||
joinedAt: m.joinedAt,
|
||||
}));
|
||||
const rooms = memberships.map((m) => ({
|
||||
id: m.room.id,
|
||||
code: m.room.code,
|
||||
name: m.room.name,
|
||||
memberCount: m.room._count.members,
|
||||
ideaCount: m.room._count.ideas,
|
||||
poolCount: 0,
|
||||
members: m.room.members.map((mb) => mb.user),
|
||||
lastDrawn: m.room.ideas[0] ?? null,
|
||||
joinedAt: m.joinedAt,
|
||||
}));
|
||||
|
||||
const roomIds = rooms.map((r) => r.id);
|
||||
const poolCounts = await prisma.blindBoxIdea.groupBy({
|
||||
by: ["roomId"],
|
||||
where: { roomId: { in: roomIds }, status: "in_pool" },
|
||||
_count: true,
|
||||
});
|
||||
const poolMap = new Map(poolCounts.map((p) => [p.roomId, p._count]));
|
||||
const roomIds = rooms.map((r) => r.id);
|
||||
const poolCounts = await prisma.blindBoxIdea.groupBy({
|
||||
by: ["roomId"],
|
||||
where: { roomId: { in: roomIds }, status: "in_pool" },
|
||||
_count: true,
|
||||
});
|
||||
const poolMap = new Map(poolCounts.map((p) => [p.roomId, p._count]));
|
||||
|
||||
for (const room of rooms) {
|
||||
room.poolCount = poolMap.get(room.id) ?? 0;
|
||||
}
|
||||
|
||||
return NextResponse.json({ rooms });
|
||||
} catch {
|
||||
return errorResponse("获取房间列表失败", 500);
|
||||
for (const room of rooms) {
|
||||
room.poolCount = poolMap.get(room.id) ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ rooms });
|
||||
});
|
||||
|
||||
+76
-111
@@ -1,130 +1,95 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { errorResponse, validateMembership } from "@/lib/blindbox";
|
||||
import { validateMembership } from "@/lib/blindbox";
|
||||
import { apiHandler, ApiError } from "@/lib/api";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { roomId, userId, content } = await req.json();
|
||||
export const POST = apiHandler(async (req) => {
|
||||
const { roomId, userId, content } = await req.json();
|
||||
|
||||
if (!userId || typeof userId !== "string") {
|
||||
return errorResponse("请先登录", 401);
|
||||
}
|
||||
if (!roomId || typeof roomId !== "string") {
|
||||
return errorResponse("roomId 不能为空", 400);
|
||||
}
|
||||
if (!content || typeof content !== "string" || content.trim().length === 0) {
|
||||
return errorResponse("内容不能为空", 400);
|
||||
}
|
||||
if (content.trim().length > 200) {
|
||||
return errorResponse("内容不能超过 200 字", 400);
|
||||
}
|
||||
|
||||
const isMember = await validateMembership(roomId, userId);
|
||||
if (!isMember) {
|
||||
return errorResponse("你不是这个房间的成员", 403);
|
||||
}
|
||||
|
||||
const idea = await prisma.blindBoxIdea.create({
|
||||
data: {
|
||||
roomId,
|
||||
userId,
|
||||
content: content.trim(),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: idea.id }, { status: 201 });
|
||||
} catch {
|
||||
return errorResponse("提交失败", 500);
|
||||
if (!userId || typeof userId !== "string") throw new ApiError("请先登录", 401);
|
||||
if (!roomId || typeof roomId !== "string") throw new ApiError("roomId 不能为空");
|
||||
if (!content || typeof content !== "string" || content.trim().length === 0) {
|
||||
throw new ApiError("内容不能为空");
|
||||
}
|
||||
}
|
||||
if (content.trim().length > 200) throw new ApiError("内容不能超过 200 字");
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const isMember = await validateMembership(roomId, userId);
|
||||
if (!isMember) throw new ApiError("你不是这个房间的成员", 403);
|
||||
|
||||
const idea = await prisma.blindBoxIdea.create({
|
||||
data: { roomId, userId, content: content.trim() },
|
||||
});
|
||||
|
||||
return NextResponse.json({ id: idea.id }, { status: 201 });
|
||||
});
|
||||
|
||||
export const GET = apiHandler(async (req) => {
|
||||
const roomId = req.nextUrl.searchParams.get("roomId");
|
||||
const userId = req.nextUrl.searchParams.get("userId");
|
||||
|
||||
if (!roomId) {
|
||||
return errorResponse("缺少 roomId", 400);
|
||||
}
|
||||
if (!userId) {
|
||||
return errorResponse("请先登录", 401);
|
||||
}
|
||||
if (!roomId) throw new ApiError("缺少 roomId");
|
||||
if (!userId) throw new ApiError("请先登录", 401);
|
||||
|
||||
const isMember = await validateMembership(roomId, userId);
|
||||
if (!isMember) {
|
||||
return errorResponse("你不是这个房间的成员", 403);
|
||||
if (!isMember) throw new ApiError("你不是这个房间的成员", 403);
|
||||
|
||||
const [poolCount, myIdeas, drawn] = await Promise.all([
|
||||
prisma.blindBoxIdea.count({
|
||||
where: { roomId, status: "in_pool" },
|
||||
}),
|
||||
prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, userId, status: "in_pool" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, content: true, createdAt: true },
|
||||
}),
|
||||
prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, status: "drawn" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
user: { select: { id: true, username: true, avatar: true } },
|
||||
drawnBy: { select: { id: true, username: true, avatar: true } },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return NextResponse.json({ poolCount, myIdeas, drawn });
|
||||
});
|
||||
|
||||
export const PUT = apiHandler(async (req) => {
|
||||
const { ideaId, userId, content } = await req.json();
|
||||
|
||||
if (!userId) throw new ApiError("请先登录", 401);
|
||||
if (!ideaId) throw new ApiError("缺少 ideaId");
|
||||
if (!content || typeof content !== "string" || content.trim().length === 0) {
|
||||
throw new ApiError("内容不能为空");
|
||||
}
|
||||
if (content.trim().length > 200) throw new ApiError("内容不能超过 200 字");
|
||||
|
||||
try {
|
||||
const [poolCount, myIdeas, drawn] = await Promise.all([
|
||||
prisma.blindBoxIdea.count({
|
||||
where: { roomId, status: "in_pool" },
|
||||
}),
|
||||
prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, userId, status: "in_pool" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, content: true, createdAt: true },
|
||||
}),
|
||||
prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, status: "drawn" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
user: { select: { id: true, username: true, avatar: true } },
|
||||
drawnBy: { select: { id: true, username: true, avatar: true } },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
const idea = await prisma.blindBoxIdea.findUnique({ where: { id: ideaId } });
|
||||
if (!idea) throw new ApiError("想法不存在", 404);
|
||||
if (idea.userId !== userId) throw new ApiError("只能编辑自己的想法", 403);
|
||||
if (idea.status !== "in_pool") throw new ApiError("已抽中的想法不能编辑");
|
||||
|
||||
return NextResponse.json({ poolCount, myIdeas, drawn });
|
||||
} catch {
|
||||
return errorResponse("查询失败", 500);
|
||||
}
|
||||
}
|
||||
const updated = await prisma.blindBoxIdea.update({
|
||||
where: { id: ideaId },
|
||||
data: { content: content.trim() },
|
||||
});
|
||||
|
||||
export async function PUT(req: NextRequest) {
|
||||
try {
|
||||
const { ideaId, userId, content } = await req.json();
|
||||
return NextResponse.json({ id: updated.id, content: updated.content });
|
||||
});
|
||||
|
||||
if (!userId) return errorResponse("请先登录", 401);
|
||||
if (!ideaId) return errorResponse("缺少 ideaId", 400);
|
||||
if (!content || typeof content !== "string" || content.trim().length === 0) {
|
||||
return errorResponse("内容不能为空", 400);
|
||||
}
|
||||
if (content.trim().length > 200) {
|
||||
return errorResponse("内容不能超过 200 字", 400);
|
||||
}
|
||||
export const DELETE = apiHandler(async (req) => {
|
||||
const { ideaId, userId } = await req.json();
|
||||
|
||||
const idea = await prisma.blindBoxIdea.findUnique({ where: { id: ideaId } });
|
||||
if (!idea) return errorResponse("想法不存在", 404);
|
||||
if (idea.userId !== userId) return errorResponse("只能编辑自己的想法", 403);
|
||||
if (idea.status !== "in_pool") return errorResponse("已抽中的想法不能编辑", 400);
|
||||
if (!userId) throw new ApiError("请先登录", 401);
|
||||
if (!ideaId) throw new ApiError("缺少 ideaId");
|
||||
|
||||
const updated = await prisma.blindBoxIdea.update({
|
||||
where: { id: ideaId },
|
||||
data: { content: content.trim() },
|
||||
});
|
||||
const idea = await prisma.blindBoxIdea.findUnique({ where: { id: ideaId } });
|
||||
if (!idea) throw new ApiError("想法不存在", 404);
|
||||
if (idea.userId !== userId) throw new ApiError("只能删除自己的想法", 403);
|
||||
if (idea.status !== "in_pool") throw new ApiError("已抽中的想法不能删除");
|
||||
|
||||
return NextResponse.json({ id: updated.id, content: updated.content });
|
||||
} catch {
|
||||
return errorResponse("编辑失败", 500);
|
||||
}
|
||||
}
|
||||
await prisma.blindBoxIdea.delete({ where: { id: ideaId } });
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
try {
|
||||
const { ideaId, userId } = await req.json();
|
||||
|
||||
if (!userId) return errorResponse("请先登录", 401);
|
||||
if (!ideaId) return errorResponse("缺少 ideaId", 400);
|
||||
|
||||
const idea = await prisma.blindBoxIdea.findUnique({ where: { id: ideaId } });
|
||||
if (!idea) return errorResponse("想法不存在", 404);
|
||||
if (idea.userId !== userId) return errorResponse("只能删除自己的想法", 403);
|
||||
if (idea.status !== "in_pool") return errorResponse("已抽中的想法不能删除", 400);
|
||||
|
||||
await prisma.blindBoxIdea.delete({ where: { id: ideaId } });
|
||||
|
||||
return NextResponse.json({ deleted: true });
|
||||
} catch {
|
||||
return errorResponse("删除失败", 500);
|
||||
}
|
||||
}
|
||||
return NextResponse.json({ deleted: true });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user