add multi delete function
This commit is contained in:
parent
d07d54731d
commit
9bf60c2282
|
@ -3,34 +3,30 @@ import { revalidatePath } from "next/cache";
|
||||||
import prisma from "./db";
|
import prisma from "./db";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
|
const tableMap = {
|
||||||
|
"/story": "story",
|
||||||
|
"/publication": "pub",
|
||||||
|
"/submission": "sub"
|
||||||
|
}
|
||||||
|
|
||||||
export async function deleteRecord(id: number, pathname: "/story" | "/publication" | "/submission") {
|
export async function deleteRecord(id: number, pathname: "/story" | "/publication" | "/submission") {
|
||||||
let res = {}
|
const table = tableMap[pathname]
|
||||||
switch (pathname) {
|
const res = await prisma[table].delete({ where: { id } })
|
||||||
case "/story":
|
console.log(`deleted from ${table}: ${res.id}`)
|
||||||
res = await prisma.story.delete({
|
|
||||||
where: { id }
|
|
||||||
})
|
|
||||||
console.log(`deleted: ${res}`)
|
|
||||||
break;
|
|
||||||
case "/publication":
|
|
||||||
res = await prisma.pub.delete({
|
|
||||||
where: { id }
|
|
||||||
})
|
|
||||||
console.log(`deleted: ${res}`)
|
|
||||||
break;
|
|
||||||
case "/submission":
|
|
||||||
res = await prisma.sub.delete({
|
|
||||||
where: { id }
|
|
||||||
})
|
|
||||||
console.log(`deleted: ${res}`)
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
console.log("revalidating: " + pathname)
|
console.log("revalidating: " + pathname)
|
||||||
revalidatePath(pathname)
|
revalidatePath(pathname)
|
||||||
redirect(pathname)
|
redirect(pathname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function deleteRecords(ids: number[], pathname: "/story" | "/publication" | "/submission") {
|
||||||
|
const table = tableMap[pathname]
|
||||||
|
ids.forEach(async (id) => {
|
||||||
|
const res = await prisma.story.delete({
|
||||||
|
where: { id }
|
||||||
|
})
|
||||||
|
console.log(`deleted from ${table}: ${res.id}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue