/** * Wishlist Controller - User favorites/saved destinations */ const { getPool } = require('../db'); /** * Get user's wishlist */ async function listWishlist(req, res) { const pool = getPool(); if (!pool) return res.status(500).json({ error: 'Database not connected' }); const userId = req.user.id || req.user.sub; try { // Create table if not exists await pool.query(` CREATE TABLE IF NOT EXISTS wishlists ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, destination_id TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), UNIQUE(user_id, destination_id) ) `); const result = await pool.query( `SELECT w.id, w.destination_id, w.created_at, d.name, d.slug, d.description, d.category, d.district, d.rating FROM wishlists w LEFT JOIN destinations d ON w.destination_id = d.id WHERE w.user_id = $1 ORDER BY w.created_at DESC`, [userId] ); res.json({ success: true, wishlist: result.rows.map(row => ({ id: row.id, destinationId: row.destination_id, createdAt: row.created_at, destination: row.name ? { id: row.destination_id, name: row.name, slug: row.slug, description: row.description, category: row.category, district: row.district, rating: row.rating } : null })) }); } catch (error) { console.error('[Wishlist List Error]', error.message); res.status(500).json({ error: 'Failed to fetch wishlist' }); } } /** * Add item to wishlist */ async function addToWishlist(req, res) { const pool = getPool(); if (!pool) return res.status(500).json({ error: 'Database not connected' }); const userId = req.user.id || req.user.sub; const { destination_id } = req.body; if (!destination_id) { return res.status(400).json({ error: 'destination_id is required' }); } try { // Create table if not exists await pool.query(` CREATE TABLE IF NOT EXISTS wishlists ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, destination_id TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), UNIQUE(user_id, destination_id) ) `); const result = await pool.query( `INSERT INTO wishlists (user_id, destination_id) VALUES ($1, $2) ON CONFLICT (user_id, destination_id) DO NOTHING RETURNING id, destination_id, created_at`, [userId, destination_id] ); if (result.rows.length === 0) { return res.json({ success: true, message: 'Already in wishlist' }); } res.status(201).json({ success: true, message: 'Added to wishlist', item: result.rows[0] }); } catch (error) { console.error('[Wishlist Add Error]', error.message); res.status(500).json({ error: 'Failed to add to wishlist' }); } } /** * Remove item from wishlist */ async function removeFromWishlist(req, res) { const pool = getPool(); if (!pool) return res.status(500).json({ error: 'Database not connected' }); const userId = req.user.id || req.user.sub; const { destinationId } = req.params; try { const result = await pool.query( 'DELETE FROM wishlists WHERE user_id = $1 AND destination_id = $2 RETURNING id', [userId, destinationId] ); if (result.rows.length === 0) { return res.status(404).json({ error: 'Item not found in wishlist' }); } res.json({ success: true, message: 'Removed from wishlist' }); } catch (error) { console.error('[Wishlist Remove Error]', error.message); res.status(500).json({ error: 'Failed to remove from wishlist' }); } } /** * Check if destination is in wishlist */ async function checkWishlist(req, res) { const pool = getPool(); if (!pool) return res.status(500).json({ error: 'Database not connected' }); const userId = req.user.id || req.user.sub; const { destinationId } = req.params; try { // Create table if not exists (to prevent 500 if checked before any add) await pool.query(` CREATE TABLE IF NOT EXISTS wishlists ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, destination_id TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), UNIQUE(user_id, destination_id) ) `); const result = await pool.query( 'SELECT id FROM wishlists WHERE user_id = $1 AND destination_id = $2', [userId, destinationId] ); res.json({ success: true, inWishlist: result.rows.length > 0 }); } catch (error) { console.error('[Wishlist Check Error]', error.message); res.status(500).json({ error: 'Failed to check wishlist' }); } } module.exports = { listWishlist, addToWishlist, removeFromWishlist, checkWishlist };