import { prisma } from "@/lib/prisma";
const BASE_URL = `${process.env.FEED_URL}`
function escapeXml(value: string): string {
return value
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
export async function GET() {
const items = await prisma.artwork.findMany({
where: { published: true },
orderBy: { createdAt: "desc" },
take: 10,
select: {
id: true,
name: true,
description: true,
altText: true,
createdAt: true,
updatedAt: true,
},
});
const lastBuildDate =
items[0]?.updatedAt?.toUTCString() ?? new Date().toUTCString();
const itemXml = items
.map((item) => {
const title = escapeXml(item.name || "Artwork");
const description = escapeXml(
item.description || item.altText || item.name || "Artwork"
);
const link = `${BASE_URL}/artworks/single/${item.id}`;
const pubDate = item.createdAt.toUTCString();
return [
"- ",
`${title}`,
`${link}`,
`${link}`,
`${description}`,
`${pubDate}`,
"
",
].join("");
})
.join("");
const xml = [
'',
'',
"",
"Gaertan Art - Latest Artworks",
`${BASE_URL}`,
"Ten newest artworks from Gaertan Art.",
`${lastBuildDate}`,
itemXml,
"",
"",
].join("");
return new Response(xml, {
headers: {
"Content-Type": "application/rss+xml; charset=utf-8",
},
});
}