95 lines
2.4 KiB
TypeScript
95 lines
2.4 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { prisma } from "@/lib/prisma"
|
|
import type { Folder } from "@/lib/types"
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const folder: Omit<Folder, "id"> = await request.json()
|
|
|
|
const created = await prisma.folder.create({
|
|
data: {
|
|
name: folder.name,
|
|
parentId: folder.parentId,
|
|
color: folder.color,
|
|
icon: folder.icon,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(created)
|
|
} catch (error) {
|
|
console.error("Error creating folder:", error)
|
|
return NextResponse.json({ error: "Failed to create folder" }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
try {
|
|
const folder: Folder = await request.json()
|
|
|
|
const updated = await prisma.folder.update({
|
|
where: { id: folder.id },
|
|
data: {
|
|
name: folder.name,
|
|
parentId: folder.parentId,
|
|
color: folder.color,
|
|
icon: folder.icon,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(updated)
|
|
} catch (error) {
|
|
console.error("Error updating folder:", error)
|
|
return NextResponse.json({ error: "Failed to update folder" }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: Request) {
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const id = searchParams.get("id")
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: "Folder ID is required" }, { status: 400 })
|
|
}
|
|
|
|
const folder = await prisma.folder.findUnique({
|
|
where: { id },
|
|
include: { children: true },
|
|
})
|
|
|
|
if (!folder) {
|
|
return NextResponse.json({ error: "Folder not found" }, { status: 404 })
|
|
}
|
|
|
|
// Move accounts to root (null)
|
|
await prisma.account.updateMany({
|
|
where: { folderId: id },
|
|
data: { folderId: null },
|
|
})
|
|
|
|
// Move subfolders to parent
|
|
if (folder.parentId) {
|
|
await prisma.folder.updateMany({
|
|
where: { parentId: id },
|
|
data: { parentId: folder.parentId },
|
|
})
|
|
} else {
|
|
// If no parent, move to null (root)
|
|
await prisma.folder.updateMany({
|
|
where: { parentId: id },
|
|
data: { parentId: null },
|
|
})
|
|
}
|
|
|
|
await prisma.folder.delete({
|
|
where: { id },
|
|
})
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error("Error deleting folder:", error)
|
|
return NextResponse.json({ error: "Failed to delete folder" }, { status: 500 })
|
|
}
|
|
}
|
|
|