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

43 lines
1.0 KiB
TypeScript

"use server"
import { revalidatePath } from "next/cache";
import prisma from "./db";
import { redirect } from "next/navigation";
import { Pathname } from "app/types";
const tableMap = {
"/story": "story",
"/publication": "pub",
"/submission": "sub"
}
export async function deleteRecord(id: number, pathname: Pathname): Promise<undefined | boolean> {
const table = tableMap[pathname]
try {
const res = await prisma[table].delete({ where: { id } })
console.log(`deleted from ${table}: ${res.id}`)
revalidatePath(pathname)
return true
} catch (error) {
console.error(error)
return undefined
}
}
export async function deleteRecords(ids: number[], pathname: "/story" | "/publication" | "/submission"): Promise<boolean | undefined> {
try {
const table = tableMap[pathname]
ids.forEach(async (id) => {
const res = await prisma[table].delete({
where: { id }
})
console.log(`deleted from ${table}: ${res.id}`)
})
revalidatePath(pathname)
return true
} catch (error) {
console.error(error)
return undefined
}
}