83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { auth } from "@clerk/nextjs/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { deleteS3Object } from "@/lib/s3";
|
|
|
|
export async function DELETE(req: NextRequest) {
|
|
const { userId } = await auth();
|
|
if (!userId) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const user = await prisma.user.findUnique({
|
|
where: { clerkId: userId },
|
|
include: { images: true },
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: "Monument not found" }, { status: 404 });
|
|
}
|
|
|
|
for (const image of user.images) {
|
|
try {
|
|
await deleteS3Object(image.key);
|
|
} catch (e) {
|
|
console.error("Failed to delete S3 object:", image.key, e);
|
|
}
|
|
}
|
|
|
|
await prisma.user.delete({ where: { id: user.id } });
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Delete monument error:", error);
|
|
return NextResponse.json({ error: "Failed to delete monument" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function GET() {
|
|
const { userId } = await auth();
|
|
if (!userId) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { clerkId: userId },
|
|
include: { images: { orderBy: { order: "asc" } } },
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: "Monument not found" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json({ user });
|
|
}
|
|
|
|
export async function PUT(req: NextRequest) {
|
|
const { userId } = await auth();
|
|
if (!userId) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const body = await req.json();
|
|
const { title, description, templateId, published } = body;
|
|
|
|
const user = await prisma.user.update({
|
|
where: { clerkId: userId },
|
|
data: {
|
|
...(title !== undefined && { title }),
|
|
...(description !== undefined && { description }),
|
|
...(templateId !== undefined && { templateId }),
|
|
...(published !== undefined && { published }),
|
|
},
|
|
include: { images: { orderBy: { order: "asc" } } },
|
|
});
|
|
|
|
return NextResponse.json({ user });
|
|
} catch (error) {
|
|
console.error("Update error:", error);
|
|
return NextResponse.json({ error: "Failed to update monument" }, { status: 500 });
|
|
}
|
|
} |