File size: 2,378 Bytes
d330ba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

const getWatchlist = async (req, res) => {
    const { userId } = req.query;
    if (!userId) return res.status(400).json({ error: "userId requerido" });

    try {
        const list = await prisma.watchlist.findMany({
            where: { userId: userId },
            include: { 
                content: true // Realiza el JOIN con la tabla Content en Neon
            } 
        });

        const formattedList = list.map(item => ({
            // 1. ID INTERNO: Necesario para que el bot贸n de eliminar (X) funcione
            id: item.contentId, 

            // 2. ID DE TMDB: El campo que falta en tu error actual
            // Extra铆do directamente de la relaci贸n con 'Content'
            tmdb_id: item.content?.tmdb_id, 

            // 3. METADATOS: Para mostrar correctamente el poster y t铆tulo
            title: item.content?.title || "Sin t铆tulo",
            image: item.content?.image || item.content?.posterPath || "",
            
            // 4. TIPO: Importante para que TMDB sepa si buscar 'movie' o 'tv'
            type: item.content?.type || "movie" 
        }));

        res.status(200).json(formattedList);
    } catch (error) {
        // Log para depuraci贸n en Railway
        console.error("Error en getWatchlist:", error);
        res.status(500).json({ error: error.message });
    }
};

const toggleWatchlist = async (req, res) => {
    const { userId, contentId } = req.body;
    if (!userId || !contentId) return res.status(400).json({ error: "Datos incompletos" });

    try {
        const existing = await prisma.watchlist.findFirst({
            where: { userId, contentId: parseInt(contentId) }
        });

        if (existing) {
            await prisma.watchlist.delete({ where: { id: existing.id } });
            return res.status(200).json({ message: "Eliminado" });
        } else {
            const newItem = await prisma.watchlist.create({
                data: { userId, contentId: parseInt(contentId) }
            });
            return res.status(201).json(newItem);
        }
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};

// Exportaci贸n estilo CommonJS
module.exports = {
    getWatchlist,
    toggleWatchlist
};