Refactor for i18n, CMS integration, and project slugs; enhance admin & analytics
Co-authored-by: dennis <dennis@konkol.net>
This commit is contained in:
71
lib/content.ts
Normal file
71
lib/content.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function getSiteSettings() {
|
||||
return prisma.siteSettings.findUnique({ where: { id: 1 } });
|
||||
}
|
||||
|
||||
export async function getContentByKey(opts: { key: string; locale: string }) {
|
||||
const { key, locale } = opts;
|
||||
const page = await prisma.contentPage.findUnique({
|
||||
where: { key },
|
||||
include: {
|
||||
translations: {
|
||||
where: { locale },
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (page?.translations?.[0]) return page.translations[0];
|
||||
|
||||
const settings = await getSiteSettings();
|
||||
const fallbackLocale = settings?.defaultLocale || "en";
|
||||
|
||||
const fallback = await prisma.contentPageTranslation.findFirst({
|
||||
where: {
|
||||
page: { key },
|
||||
locale: fallbackLocale,
|
||||
},
|
||||
});
|
||||
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export async function upsertContentByKey(opts: {
|
||||
key: string;
|
||||
locale: string;
|
||||
title?: string | null;
|
||||
slug?: string | null;
|
||||
content: unknown;
|
||||
metaDescription?: string | null;
|
||||
keywords?: string | null;
|
||||
}) {
|
||||
const { key, locale, title, slug, content, metaDescription, keywords } = opts;
|
||||
|
||||
const page = await prisma.contentPage.upsert({
|
||||
where: { key },
|
||||
create: { key, status: "PUBLISHED" },
|
||||
update: {},
|
||||
});
|
||||
|
||||
return prisma.contentPageTranslation.upsert({
|
||||
where: { pageId_locale: { pageId: page.id, locale } },
|
||||
create: {
|
||||
pageId: page.id,
|
||||
locale,
|
||||
title: title ?? undefined,
|
||||
slug: slug ?? undefined,
|
||||
content: content as any, // JSON
|
||||
metaDescription: metaDescription ?? undefined,
|
||||
keywords: keywords ?? undefined,
|
||||
},
|
||||
update: {
|
||||
title: title ?? undefined,
|
||||
slug: slug ?? undefined,
|
||||
content: content as any, // JSON
|
||||
metaDescription: metaDescription ?? undefined,
|
||||
keywords: keywords ?? undefined,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user