Add tags and categories
This commit is contained in:
25
src/actions/categories/createCategory.ts
Normal file
25
src/actions/categories/createCategory.ts
Normal file
@ -0,0 +1,25 @@
|
||||
"use server"
|
||||
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { categorySchema } from "@/schemas/artworks/categorySchema"
|
||||
|
||||
export async function createCategory(formData: categorySchema) {
|
||||
const parsed = categorySchema.safeParse(formData)
|
||||
|
||||
if (!parsed.success) {
|
||||
console.error("Validation failed", parsed.error)
|
||||
throw new Error("Invalid input")
|
||||
}
|
||||
|
||||
const data = parsed.data
|
||||
|
||||
const created = await prisma.artCategory.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
slug: data.slug,
|
||||
description: data.description
|
||||
},
|
||||
})
|
||||
|
||||
return created
|
||||
}
|
||||
27
src/actions/categories/updateCategory.ts
Normal file
27
src/actions/categories/updateCategory.ts
Normal file
@ -0,0 +1,27 @@
|
||||
"use server"
|
||||
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { categorySchema } from '@/schemas/artworks/categorySchema';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export async function updateCategory(id: string, rawData: z.infer<typeof categorySchema>) {
|
||||
const parsed = categorySchema.safeParse(rawData)
|
||||
|
||||
if (!parsed.success) {
|
||||
console.error("Validation failed", parsed.error)
|
||||
throw new Error("Invalid input")
|
||||
}
|
||||
|
||||
const data = parsed.data
|
||||
|
||||
const updated = await prisma.artCategory.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: data.name,
|
||||
slug: data.slug,
|
||||
description: data.description
|
||||
},
|
||||
})
|
||||
|
||||
return updated
|
||||
}
|
||||
23
src/actions/deleteItem.ts
Normal file
23
src/actions/deleteItem.ts
Normal file
@ -0,0 +1,23 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function deleteItems(itemId: string, type: string) {
|
||||
|
||||
switch (type) {
|
||||
case "categories":
|
||||
await prisma.artCategory.delete({ where: { id: itemId } });
|
||||
break;
|
||||
case "tags":
|
||||
await prisma.artTag.delete({ where: { id: itemId } });
|
||||
break;
|
||||
// case "types":
|
||||
// await prisma.portfolioType.delete({ where: { id: itemId } });
|
||||
// break;
|
||||
// case "albums":
|
||||
// await prisma.portfolioAlbum.delete({ where: { id: itemId } });
|
||||
// break;
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
36
src/actions/tags/createTag.ts
Normal file
36
src/actions/tags/createTag.ts
Normal file
@ -0,0 +1,36 @@
|
||||
"use server"
|
||||
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { tagSchema } from "@/schemas/artworks/tagSchema"
|
||||
|
||||
export async function createTag(formData: tagSchema) {
|
||||
const parsed = tagSchema.safeParse(formData)
|
||||
|
||||
if (!parsed.success) {
|
||||
console.error("Validation failed", parsed.error)
|
||||
throw new Error("Invalid input")
|
||||
}
|
||||
|
||||
const data = parsed.data
|
||||
|
||||
const created = await prisma.artTag.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
slug: data.slug,
|
||||
description: data.description
|
||||
},
|
||||
})
|
||||
|
||||
if (data.categoryIds) {
|
||||
await prisma.artTag.update({
|
||||
where: { id: created.id },
|
||||
data: {
|
||||
categories: {
|
||||
set: data.categoryIds.map(id => ({ id }))
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return created
|
||||
}
|
||||
38
src/actions/tags/updateTag.ts
Normal file
38
src/actions/tags/updateTag.ts
Normal file
@ -0,0 +1,38 @@
|
||||
"use server"
|
||||
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { tagSchema } from '@/schemas/artworks/tagSchema';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export async function updateTag(id: string, rawData: z.infer<typeof tagSchema>) {
|
||||
const parsed = tagSchema.safeParse(rawData)
|
||||
|
||||
if (!parsed.success) {
|
||||
console.error("Validation failed", parsed.error)
|
||||
throw new Error("Invalid input")
|
||||
}
|
||||
|
||||
const data = parsed.data
|
||||
|
||||
const updated = await prisma.artTag.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: data.name,
|
||||
slug: data.slug,
|
||||
description: data.description
|
||||
},
|
||||
})
|
||||
|
||||
if (data.categoryIds) {
|
||||
await prisma.artTag.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
categories: {
|
||||
set: data.categoryIds.map(id => ({ id }))
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return updated
|
||||
}
|
||||
Reference in New Issue
Block a user