Files
portfolio/app/api/fetchAllProjects/route.tsx
2026-01-12 15:27:35 +00:00

62 lines
1.4 KiB
TypeScript

import { NextResponse } from "next/server";
import NodeCache from "node-cache";
import { prisma } from "@/lib/prisma";
export const runtime = "nodejs"; // Force Node runtime
const cache = new NodeCache({ stdTTL: 300 }); // Cache für 5 Minuten
type LegacyPost = {
slug: string;
id: string;
title: string;
meta_description: string | null;
updated_at: string;
};
type LegacyPostsResponse = {
posts: Array<LegacyPost>;
};
export async function GET() {
const cacheKey = "projects:legacyPosts";
const cachedPosts = cache.get<LegacyPostsResponse>(cacheKey);
if (cachedPosts) {
return NextResponse.json(cachedPosts);
}
try {
const projects = await prisma.project.findMany({
where: { published: true },
orderBy: { updatedAt: "desc" },
select: {
id: true,
slug: true,
title: true,
updatedAt: true,
metaDescription: true,
},
});
const payload: LegacyPostsResponse = {
posts: projects.map((p) => ({
id: String(p.id),
slug: p.slug,
title: p.title,
meta_description: p.metaDescription ?? null,
updated_at: (p.updatedAt ?? new Date()).toISOString(),
})),
};
cache.set(cacheKey, payload);
return NextResponse.json(payload);
} catch (error) {
console.error("Failed to fetch projects:", error);
return NextResponse.json(
{ error: "Failed to fetch projects" },
{ status: 500 },
);
}
}