Files
admin.gaertan.art/src/actions/portfolio/images/updateImage.ts
2025-07-26 12:20:44 +02:00

86 lines
1.5 KiB
TypeScript

"use server"
import prisma from "@/lib/prisma";
import { imageSchema } from "@/schemas/portfolio/imageSchema";
import { z } from "zod/v4";
export async function updateImage(
values: z.infer<typeof imageSchema>,
id: string
) {
const validated = imageSchema.safeParse(values);
if (!validated.success) {
throw new Error("Invalid image data");
}
const {
fileKey,
originalFile,
name,
nsfw,
published,
setAsHeader,
altText,
description,
fileType,
fileSize,
month,
year,
creationDate,
typeId,
tagIds,
categoryIds
} = validated.data;
if(setAsHeader) {
await prisma.portfolioImage.updateMany({
where: { setAsHeader: true },
data: { setAsHeader: false },
})
}
const updatedImage = await prisma.portfolioImage.update({
where: { id: id },
data: {
fileKey,
originalFile,
name,
nsfw,
published,
setAsHeader,
altText,
description,
fileType,
fileSize,
month,
year,
creationDate,
typeId
}
});
if (tagIds) {
await prisma.portfolioImage.update({
where: { id: id },
data: {
tags: {
set: tagIds.map(id => ({ id }))
}
}
});
}
if (categoryIds) {
await prisma.portfolioImage.update({
where: { id: id },
data: {
categories: {
set: categoryIds.map(id => ({ id }))
}
}
});
}
return updatedImage
}