75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { prisma } from "@/lib/prisma"
|
|
import type { Account } from "@/lib/types"
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const account: Omit<Account, "id"> = await request.json()
|
|
|
|
const created = await prisma.account.create({
|
|
data: {
|
|
name: account.name,
|
|
bankId: account.bankId,
|
|
accountNumber: account.accountNumber,
|
|
type: account.type,
|
|
folderId: account.folderId,
|
|
balance: account.balance,
|
|
currency: account.currency,
|
|
lastImport: account.lastImport,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(created)
|
|
} catch (error) {
|
|
console.error("Error creating account:", error)
|
|
return NextResponse.json({ error: "Failed to create account" }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
try {
|
|
const account: Account = await request.json()
|
|
|
|
const updated = await prisma.account.update({
|
|
where: { id: account.id },
|
|
data: {
|
|
name: account.name,
|
|
bankId: account.bankId,
|
|
accountNumber: account.accountNumber,
|
|
type: account.type,
|
|
folderId: account.folderId,
|
|
balance: account.balance,
|
|
currency: account.currency,
|
|
lastImport: account.lastImport,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(updated)
|
|
} catch (error) {
|
|
console.error("Error updating account:", error)
|
|
return NextResponse.json({ error: "Failed to update account" }, { 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: "Account ID is required" }, { status: 400 })
|
|
}
|
|
|
|
// Transactions will be deleted automatically due to onDelete: Cascade
|
|
await prisma.account.delete({
|
|
where: { id },
|
|
})
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error("Error deleting account:", error)
|
|
return NextResponse.json({ error: "Failed to delete account" }, { status: 500 })
|
|
}
|
|
}
|
|
|