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

43 lines
1.0 KiB
TypeScript
Raw Normal View History

"use server"
2024-06-19 17:46:30 +00:00
import { revalidatePath } from "next/cache";
import prisma from "./db";
2024-06-19 17:46:30 +00:00
import { redirect } from "next/navigation";
import { Pathname } from "app/types";
2024-06-27 14:08:03 +00:00
const tableMap = {
"/story": "story",
"/publication": "pub",
"/submission": "sub"
}
2024-09-25 10:23:16 +00:00
export async function deleteRecord(id: number, pathname: Pathname): Promise<undefined | boolean> {
2024-06-27 14:08:03 +00:00
const table = tableMap[pathname]
2024-09-25 10:23:16 +00:00
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
}
2024-06-20 08:35:25 +00:00
}
2024-06-20 09:39:35 +00:00
2024-09-25 10:23:16 +00:00
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}`)
2024-06-27 14:08:03 +00:00
})
2024-09-25 10:23:16 +00:00
revalidatePath(pathname)
return true
} catch (error) {
console.error(error)
return undefined
}
2024-06-27 14:08:03 +00:00
}