svrjs-nextjs-website/app/api/update/mods/[id]/route.ts
Dorian Niemiec e67ab5688f
Some checks are pending
Deploy Next.js application / deploy (push) Waiting to run
fix: update the date when editing the mod entry
2024-09-08 19:14:39 +02:00

49 lines
1.2 KiB
TypeScript

import { NextResponse } from "next/server";
import clientPromise from "@/lib/db";
import { ObjectId } from "mongodb";
import { revalidatePath } from "next/cache";
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(process.env.MONGODB_DB);
const result = await db.collection("mods").updateOne(
{ _id: new ObjectId(id) },
{
$set: {
date: new Date().toISOString().split("T")[0],
fileName,
version,
downloadLink,
fileSize
}
}
);
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"
});
}
}