svrjs-nextjs-website/app/api/update/mods/[id]/route.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-08-27 13:15:36 +02:00
import { NextResponse } from "next/server";
import clientPromise from "@/lib/db";
import { ObjectId } from "mongodb";
import { revalidatePath } from "next/cache";
2024-08-27 13:15:36 +02:00
export const dynamic = "force-dynamic";
export async function PUT(
request: Request,
{ params }: { params: { id: string } }
2024-08-27 13:15:36 +02:00
) {
const { id } = params;
const body = await request.json();
const { fileName, version, downloadLink, fileSize } = body;
2024-08-27 13:15:36 +02:00
try {
const client = await clientPromise;
const db = client.db(process.env.MONGODB_DB);
2024-08-27 13:15:36 +02:00
const result = await db.collection("mods").updateOne(
{ _id: new ObjectId(id) },
{
$set: {
fileName,
version,
downloadLink,
fileSize
}
}
);
2024-08-27 13:15:36 +02:00
if (result.modifiedCount > 0) {
revalidatePath("/mods");
return NextResponse.json({ success: true });
} else {
return NextResponse.json({
success: false,
message: "No document updated"
});
}
} catch (error) {
console.error("Update failed", error);
return NextResponse.json({
success: false,
message: "Failed to update mod"
});
}
2024-08-27 13:15:36 +02:00
}