Spaces:
Paused
Paused
Download controllers/wishlist.controller.js from imkrish/yatra-x-user: direct link, hf CLI and curl.
- Browser
- Download file 5.15 kB
-
https://huggingface.co/spaces/imkrish/yatra-x-user/resolve/main/controllers/wishlist.controller.js
- Command line
-
hf download hf://spaces/imkrish/yatra-x-user/controllers/wishlist.controller.js
-
curl -L -o wishlist.controller.js https://huggingface.co/spaces/imkrish/yatra-x-user/resolve/main/controllers/wishlist.controller.js
5.15 kB
| /** | |
| * 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 | |
| }; | |