Improve artwork edit form
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { artworkSchema } from "@/schemas/artworks/imageSchema";
|
||||
import { normalizeNames, slugify } from "@/utils/artworkHelpers";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
export async function updateArtwork(
|
||||
@ -9,7 +10,6 @@ export async function updateArtwork(
|
||||
id: string
|
||||
) {
|
||||
const validated = artworkSchema.safeParse(values);
|
||||
// console.log(validated)
|
||||
if (!validated.success) {
|
||||
throw new Error("Invalid image data");
|
||||
}
|
||||
@ -27,55 +27,71 @@ export async function updateArtwork(
|
||||
year,
|
||||
creationDate,
|
||||
tagIds,
|
||||
categoryIds
|
||||
categoryIds,
|
||||
newTagNames,
|
||||
newCategoryNames,
|
||||
} = validated.data;
|
||||
|
||||
if(setAsHeader) {
|
||||
await prisma.artwork.updateMany({
|
||||
where: { setAsHeader: true },
|
||||
data: { setAsHeader: false },
|
||||
})
|
||||
}
|
||||
|
||||
const tagsToCreate = normalizeNames(newTagNames);
|
||||
const categoriesToCreate = normalizeNames(newCategoryNames);
|
||||
|
||||
const updatedArtwork = await prisma.artwork.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
name,
|
||||
needsWork,
|
||||
nsfw,
|
||||
published,
|
||||
setAsHeader,
|
||||
altText,
|
||||
description,
|
||||
notes,
|
||||
month,
|
||||
year,
|
||||
creationDate
|
||||
const updatedArtwork = await prisma.$transaction(async (tx) => {
|
||||
|
||||
if(setAsHeader) {
|
||||
await tx.artwork.updateMany({
|
||||
where: { setAsHeader: true },
|
||||
data: { setAsHeader: false },
|
||||
})
|
||||
}
|
||||
|
||||
const tagsRelation =
|
||||
tagIds || tagsToCreate.length
|
||||
? {
|
||||
tags: {
|
||||
set: [], // replace entire relation
|
||||
connect: (tagIds ?? []).map((tagId) => ({ id: tagId })),
|
||||
connectOrCreate: tagsToCreate.map((tName) => ({
|
||||
where: { name: tName },
|
||||
create: { name: tName, slug: slugify(tName) },
|
||||
})),
|
||||
},
|
||||
}
|
||||
: {};
|
||||
|
||||
const categoriesRelation =
|
||||
categoryIds || categoriesToCreate.length
|
||||
? {
|
||||
categories: {
|
||||
set: [],
|
||||
connect: (categoryIds ?? []).map((catId) => ({ id: catId })),
|
||||
connectOrCreate: categoriesToCreate.map((cName) => ({
|
||||
where: { name: cName },
|
||||
create: { name: cName, slug: slugify(cName) },
|
||||
})),
|
||||
},
|
||||
}
|
||||
: {};
|
||||
|
||||
return tx.artwork.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
name,
|
||||
slug: slugify(name),
|
||||
needsWork,
|
||||
nsfw,
|
||||
published,
|
||||
setAsHeader,
|
||||
altText,
|
||||
description,
|
||||
notes,
|
||||
month,
|
||||
year,
|
||||
creationDate,
|
||||
...tagsRelation,
|
||||
...categoriesRelation,
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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
|
||||
return updatedArtwork;
|
||||
}
|
||||
Reference in New Issue
Block a user