add: working JSON data insert
This commit is contained in:
40
src/app/(payload)/api/insert-papers/route.ts
Normal file
40
src/app/(payload)/api/insert-papers/route.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getPayload } from 'payload';
|
||||
import config from '@payload-config';
|
||||
|
||||
const payload = await getPayload({ config });
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const papersJson = await req.json();
|
||||
|
||||
const mapPaperData = (paper) => ({
|
||||
title: paper.title,
|
||||
department: paper.Abteilung.includes('WI') ? 'WI' : paper.Abteilung.includes('CI') ? 'CI' : 'MD',
|
||||
year: paper.id.toString().slice(0, 4),
|
||||
issue: paper.Ausgangslage,
|
||||
goal: paper.Zielsetzung,
|
||||
result: paper["Geplantes Ergebnis der Prüfungskandidatin/des Prüfungskandidaten"],
|
||||
mentor: paper["Betreuer/innen"].replace("Hauptverantwortlich: ", ""),
|
||||
authors: paper.Projektteam.split(",").map((author) => {
|
||||
const isLeader = author.includes("(Hauptverantwortlich)");
|
||||
return {
|
||||
name: author.replace("(Hauptverantwortlich)", "").trim(),
|
||||
position: isLeader ? "leader" : "member",
|
||||
};
|
||||
}).filter(author => author.name.length > 0),
|
||||
});
|
||||
|
||||
for (const paper of papersJson) {
|
||||
await payload.create({
|
||||
collection: "papers",
|
||||
data: mapPaperData(paper),
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Papers successfully inserted!" });
|
||||
} catch (err) {
|
||||
console.error("Insertion error:", err);
|
||||
return NextResponse.json({ error: "Failed to insert papers." }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user