Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions app/api/endpoint-groups/[id]/copy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { NextResponse } from "next/server"
import { auth } from "@/lib/auth"
import { getDb } from "@/lib/db"
import { endpointGroups, endpointToGroup } from "@/lib/db/schema/endpoint-groups"
import { eq, and } from "drizzle-orm"
import { generateId } from "@/lib/utils"
import { z } from "zod"

export const runtime = 'edge'

const copyEndpointGroupSchema = z.object({
name: z.string().min(1, "名称不能为空"),
status: z.enum(["active", "inactive"]).optional(),
})

export async function POST(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await auth()

const db = await getDb()
const { id } = await params

const originalGroup = await db.query.endpointGroups.findFirst({
where: and(
eq(endpointGroups.id, id),
eq(endpointGroups.userId, session!.user!.id!)
)
})

if (!originalGroup) {
return NextResponse.json(
{ error: "接口组不存在或无权访问" },
{ status: 404 }
)
}

const body = await request.json()
const { name, status } = copyEndpointGroupSchema.parse(body)

const relations = await db.query.endpointToGroup.findMany({
where: eq(endpointToGroup.groupId, id)
})

const newGroupId = generateId()

await db.insert(endpointGroups).values({
id: newGroupId,
name,
userId: session!.user!.id!,
status: status ?? "inactive",
createdAt: new Date(),
updatedAt: new Date(),
})

if (relations.length > 0) {
await db.insert(endpointToGroup).values(
relations.map(relation => ({
endpointId: relation.endpointId,
groupId: newGroupId,
}))
)
}

return NextResponse.json({ id: newGroupId })
} catch (error) {
console.error('复制接口组失败:', error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{ error: error.errors[0].message },
{ status: 400 }
)
}
return NextResponse.json(
{ error: '复制接口组失败' },
{ status: 500 }
)
}
}
68 changes: 68 additions & 0 deletions app/api/endpoint-groups/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,77 @@ import { auth } from "@/lib/auth"
import { getDb } from "@/lib/db"
import { endpointGroups, endpointToGroup } from "@/lib/db/schema/endpoint-groups"
import { eq } from "drizzle-orm"
import { z } from "zod"

export const runtime = 'edge'

const updateEndpointGroupSchema = z.object({
name: z.string().min(1, "名称不能为空"),
endpointIds: z.array(z.string()).min(1, "至少需要一个接口"),
})

export async function PUT(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await auth()

const db = await getDb()
const { id } = await params

const group = await db.query.endpointGroups.findFirst({
where: (groups, { and, eq }) => and(
eq(groups.id, id),
eq(groups.userId, session!.user!.id!)
)
})

if (!group) {
return NextResponse.json(
{ error: "接口组不存在或无权访问" },
{ status: 404 }
)
}

const body = await request.json()
const validatedData = updateEndpointGroupSchema.parse(body)

// 更新接口组名称
await db.update(endpointGroups)
.set({
name: validatedData.name,
updatedAt: new Date(),
})
.where(eq(endpointGroups.id, id))

// 删除旧的关联关系
await db.delete(endpointToGroup).where(eq(endpointToGroup.groupId, id))

// 添加新的关联关系
await db.insert(endpointToGroup).values(
validatedData.endpointIds.map(endpointId => ({
groupId: id,
endpointId,
}))
)

return NextResponse.json({ success: true, id })
} catch (error) {
console.error('更新接口组失败:', error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{ error: error.errors[0].message },
{ status: 400 }
)
}
return NextResponse.json(
{ error: '更新接口组失败' },
{ status: 500 }
)
}
}

export async function DELETE(
request: Request,
{ params }: { params: Promise<{ id: string }> }
Expand Down
61 changes: 61 additions & 0 deletions app/api/endpoints/[endpointId]/copy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { auth } from "@/lib/auth"
import { getDb } from "@/lib/db"
import { endpoints, insertEndpointSchema } from "@/lib/db/schema/endpoints"
import { and, eq } from "drizzle-orm"
import { NextResponse } from "next/server"
import { generateId } from "@/lib/utils"
import { z } from "zod"

export const runtime = "edge"

const copyEndpointSchema = z.object({
name: z.string().min(1, "名称不能为空"),
status: z.enum(["active", "inactive"]).optional(),
})

export async function POST(
req: Request,
{ params }: { params: Promise<{ endpointId: string }> }
) {
try {
const db = await getDb()
const session = await auth()
if (!session?.user) {
return new NextResponse("Unauthorized", { status: 401 })
}

const { endpointId } = await params
const body = await req.json()
const { name, status } = copyEndpointSchema.parse(body)

const originalEndpoint = await db.query.endpoints.findFirst({
where: and(
eq(endpoints.id, endpointId),
eq(endpoints.userId, session.user.id!)
),
})

if (!originalEndpoint) {
return new NextResponse("Not found", { status: 404 })
}

const newEndpoint = insertEndpointSchema.parse({
id: generateId(),
name,
channelId: originalEndpoint.channelId,
rule: originalEndpoint.rule,
userId: session.user.id!,
status: status ?? "inactive",
})

const created = await db.insert(endpoints).values(newEndpoint as any).returning()

return NextResponse.json(created[0])
} catch (error) {
if (error instanceof z.ZodError) {
return new NextResponse(error.message, { status: 400 })
}
console.error("[ENDPOINT_COPY]", error)
return new NextResponse("Internal Error", { status: 500 })
}
}
Loading