make update function concise/flexible
This commit is contained in:
parent
8c4b9d27f2
commit
b6a56fca2b
|
@ -5,27 +5,12 @@ import prisma from "./db"
|
||||||
import { revalidatePath } from "next/cache"
|
import { revalidatePath } from "next/cache"
|
||||||
import { redirect } from "next/navigation"
|
import { redirect } from "next/navigation"
|
||||||
|
|
||||||
export async function updateStory(data: Story & { genres: number[] }) {
|
|
||||||
const genresArray = data.genres.map((e) => { return { id: e } })
|
|
||||||
|
|
||||||
const res = await prisma.story.update({
|
export async function updateField({ text, number, table, column, id, pathname }: { text?: string, number?: number, table: string, column: string, id: number, pathname: string }) {
|
||||||
where: { id: data.id },
|
|
||||||
data: {
|
|
||||||
title: data.title,
|
|
||||||
word_count: data.word_count,
|
|
||||||
genres: { set: genresArray }
|
|
||||||
}
|
|
||||||
})
|
|
||||||
console.log(`updated story: ${res}`)
|
|
||||||
revalidatePath("/story")
|
|
||||||
redirect("/story")
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function updateTextField({ text, table, column, id, pathname }: { text: string, table: string, column: string, id: number, pathname: string }) {
|
|
||||||
const res = await prisma[table].update({
|
const res = await prisma[table].update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
[column]: text
|
[column]: text ?? number
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log(`updated record in ${table}: ${res}`)
|
console.log(`updated record in ${table}: ${res}`)
|
||||||
|
|
Loading…
Reference in New Issue