import Playlist from '../models/Playlist.js'; import Track from '../models/Track.js'; export const playlistController = { async list(req, res, next) { try { const playlists = await Playlist.find({ user: req.userId }) .sort({ createdAt: -1 }) .lean(); res.json({ success: true, data: playlists }); } catch (error) { next(error); } }, async create(req, res, next) { try { const { name } = req.body; if (!name) { return res .status(400) .json({ success: false, message: 'Playlist name is required' }); } const playlist = await Playlist.create({ name, user: req.userId, }); return res.json({ success: true, data: playlist }); } catch (error) { next(error); } }, async addTrack(req, res, next) { try { const { playlistId } = req.params; const { trackId } = req.body; if (!trackId) { return res .status(400) .json({ success: false, message: 'Track id is required' }); } const exists = await Track.exists({ _id: trackId }); if (!exists) { return res.status(404).json({ success: false, message: 'Track not found' }); } const playlist = await Playlist.findOneAndUpdate( { _id: playlistId, user: req.userId }, { $addToSet: { tracks: trackId } }, { new: true } ).lean(); if (!playlist) { return res.status(404).json({ success: false, message: 'Playlist not found' }); } return res.json({ success: true, data: playlist }); } catch (error) { next(error); } }, };