svrjs-nextjs-website/app/api/update/mods/[id]/route.ts
2024-08-27 16:45:36 +05:30

46 lines
992 B
TypeScript

import { NextResponse } from "next/server";
import clientPromise from "@/lib/db";
import { ObjectId } from "mongodb";
export const dynamic = "force-dynamic";
export async function PUT(
request: Request,
{ params }: { params: { id: string } }
) {
const { id } = params;
const body = await request.json();
const { fileName, version, downloadLink, fileSize } = body;
try {
const client = await clientPromise;
const db = client.db("downloadsDatabase");
const result = await db.collection("mods").updateOne(
{ _id: new ObjectId(id) },
{
$set: {
fileName,
version,
downloadLink,
fileSize,
},
}
);
if (result.modifiedCount > 0) {
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",
});
}
}