subman-nextjs/src/app/lib/create.ts

30 lines
623 B
TypeScript
Raw Normal View History

2024-06-26 16:18:44 +00:00
"use server"
import { Genre } from "@prisma/client"
import prisma from "./db"
import { revalidatePath } from "next/cache"
import { redirect } from "next/navigation"
export async function createStory(data) {
"use server"
const genresArray = data.genres.map((e: Genre) => { return { id: e } })
const res = await prisma.story.create({
data: {
title: data.title,
word_count: data.word_count,
}
})
console.log(res)
const genresRes = await prisma.story.update({
where: { id: res.id },
data: {
genres: { set: genresArray }
}
})
console.log(genresRes)
revalidatePath("/story")
redirect("/story")
}