Add image upload and edit functions
This commit is contained in:
81
src/actions/artworks/updateArtwork.ts
Normal file
81
src/actions/artworks/updateArtwork.ts
Normal file
@ -0,0 +1,81 @@
|
||||
"use server"
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { artworkSchema } from "@/schemas/artworks/imageSchema";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
export async function updateArtwork(
|
||||
values: z.infer<typeof artworkSchema>,
|
||||
id: string
|
||||
) {
|
||||
const validated = artworkSchema.safeParse(values);
|
||||
// console.log(validated)
|
||||
if (!validated.success) {
|
||||
throw new Error("Invalid image data");
|
||||
}
|
||||
|
||||
const {
|
||||
name,
|
||||
needsWork,
|
||||
nsfw,
|
||||
published,
|
||||
setAsHeader,
|
||||
altText,
|
||||
description,
|
||||
notes,
|
||||
month,
|
||||
year,
|
||||
creationDate,
|
||||
tagIds,
|
||||
categoryIds
|
||||
} = validated.data;
|
||||
|
||||
if(setAsHeader) {
|
||||
await prisma.artwork.updateMany({
|
||||
where: { setAsHeader: true },
|
||||
data: { setAsHeader: false },
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
const updatedArtwork = await prisma.artwork.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
name,
|
||||
needsWork,
|
||||
nsfw,
|
||||
published,
|
||||
setAsHeader,
|
||||
altText,
|
||||
description,
|
||||
notes,
|
||||
month,
|
||||
year,
|
||||
creationDate
|
||||
}
|
||||
});
|
||||
|
||||
if (tagIds) {
|
||||
await prisma.artwork.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
tags: {
|
||||
set: tagIds.map(id => ({ id }))
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (categoryIds) {
|
||||
await prisma.artwork.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
categories: {
|
||||
set: categoryIds.map(id => ({ id }))
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return updatedArtwork
|
||||
}
|
||||
Reference in New Issue
Block a user