43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
const prisma = new PrismaClient()
|
|
|
|
export const getAllComments = async (postSlug?: string) => {
|
|
try {
|
|
const comments = await prisma.comment.findMany({
|
|
orderBy: [{ createdAt: 'desc' }],
|
|
where: {
|
|
...(postSlug && { postSlug: postSlug })
|
|
},
|
|
include: { user: true }
|
|
})
|
|
return comments
|
|
} catch (error) {
|
|
console.error('Error fetching comments:', error)
|
|
throw new Error('Error fetching comments')
|
|
}
|
|
}
|
|
|
|
export const createComment = async (body: any, userEmail: any, session: any) => {
|
|
if (!session) {
|
|
throw new Error('Not Authenticated')
|
|
}
|
|
|
|
try {
|
|
const comment = await prisma.comment.create({
|
|
data: { description: body.description, postSlug: body.postSlug, userEmail: userEmail }
|
|
})
|
|
return comment
|
|
} catch (error) {
|
|
console.error('Error creating comment:', error)
|
|
throw new Error('Error creating comment')
|
|
}
|
|
}
|
|
|
|
export const deleteComment = async (id: string) => {
|
|
await prisma.comment.delete({
|
|
where: {
|
|
id: id
|
|
}
|
|
})
|
|
}
|