nitropage
Version:
A free and open source, extensible visual page builder based on SolidStart.
147 lines (125 loc) • 3.38 kB
text/typescript
import { getUser } from "#lib/auth/server";
import { useFilesystem } from "#lib/server/filesystem";
import {
deleteFont as deleteFont_,
updateFonts as updateFonts_,
uploadFonts as uploadFonts_,
} from "#lib/server/font";
import { useDatabase } from "#lib/server/prisma";
import { throwOnDemo } from "#lib/server/util";
import { createId } from "@paralleldrive/cuid2";
import { action, json, query, reload } from "@solidjs/router";
export const getFonts = query(async ({ projectId }: { projectId: string }) => {
"use server";
await getUser({ assert: {} });
const db = useDatabase();
return await db.nitroFont.findMany({
where: {
projectId,
},
orderBy: {
createdAt: "desc",
},
include: {
faces: {
orderBy: [
{
weight: "asc",
},
{
style: "asc",
},
],
},
},
});
}, "np-fonts");
export const createFont = action(
async ({ csrf, projectId }: { csrf: string; projectId: string }) => {
"use server";
await getUser({ assert: { csrf: csrf || "" } });
const db = useDatabase();
let publicId = createId();
const existingFont = await db.nitroMedia.findUnique({
where: {
publicId,
},
});
// If the publicId already exists, we try another one
// TODO: Improve this (e.g. by trying multiple ones)
if (existingFont) {
publicId = createId();
}
const fs = useFilesystem();
const project = await db.nitroProject.findUniqueOrThrow({
where: {
id: projectId,
},
});
const font = db.nitroFont.create({
data: {
project: project
? {
connect: {
id: project.id,
},
}
: undefined,
publicId,
driver: fs.default,
},
});
return json(font, { revalidate: getFonts.key });
},
);
export const deleteFont = action(async (args: { csrf: string; id: string }) => {
"use server";
await throwOnDemo();
await getUser({ assert: { csrf: args.csrf || "" } });
await deleteFont_(args.id);
return reload({ revalidate: getFonts.key });
});
export const createFontFace = action(
async (args: { csrf: string; id: string }) => {
"use server";
await getUser({ assert: { csrf: args.csrf || "" } });
const db = useDatabase();
await db.nitroFontFace.create({
data: {
font: {
connect: {
id: args.id,
},
},
},
});
return reload({ revalidate: getFonts.key });
},
);
export const deleteFontFace = action(
async (args: { csrf: string; id: string }) => {
"use server";
await getUser({ assert: { csrf: args.csrf || "" } });
const db = useDatabase();
await db.nitroFontFace.delete({
where: {
id: args.id,
},
});
return reload({ revalidate: getFonts.key });
},
);
export const updateFonts = action(
async (...args: Parameters<typeof updateFonts_>) => {
"use server";
await getUser({ assert: { csrf: true } });
await updateFonts_(...args);
return reload({ revalidate: getFonts.key });
},
);
export const uploadFonts = action(async (form: FormData) => {
"use server";
await getUser({ assert: { csrf: true } });
await uploadFonts_();
return reload({ revalidate: getFonts.key });
});