import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() export const addFavorite = async (postId: any, userEmail: any, session: any) => { try { if (!session) { throw new Error('Not Authenticated') } const existingFavorite = await prisma.favorite.findUnique({ where: { postId_userEmail: { postId, userEmail } } }) if (existingFavorite) { throw new Error('El usuario ya marcó este post como favorito.') } await prisma.favorite.create({ data: { postId, userEmail } }) const updatedFavorites = await prisma.post .findUnique({ where: { id: postId } }) .Favorite() return updatedFavorites } catch (error) { console.error('Error al añadir a favoritos:', error) throw new Error('Error al añadir a favoritos') } } export const deleteFavorite = async (postId: any, userEmail: string, session: any) => { try { // Verificar si el usuario ha marcado el post como favorito const existingFavorite = await prisma.favorite.findUnique({ where: { postId_userEmail: { postId, userEmail } } }) if (!existingFavorite) { throw new Error('El usuario no ha marcado este post como favorito.') } // Eliminar de favoritos await prisma.favorite.delete({ where: { postId_userEmail: { postId, userEmail } } }) const updatedFavorites = await prisma.post .findUnique({ where: { id: postId } }) .Favorite() return updatedFavorites } catch (error) { console.error('Error al quitar de favoritos:', error) throw new Error('Error al quitar de favoritos') } }