Uname: Linux business55.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64
Software: LiteSpeed
PHP version: 8.1.31 [ PHP INFO ] PHP os: Linux
Server Ip: 162.213.251.212
Your Ip: 18.227.190.93
User: allssztx (535) | Group: allssztx (533)
Safe Mode: OFF
Disable Function:
NONE

name : playlist.js
const express = require("express")
const Artist = require("../schema/Artist")
const Playlist = require('../schema/Playlist')
const Music = require("../schema/Music")
const router = express.Router()

router.post('/new', async (req, res) => {
    try {
        const { user, thumbnail, title, music } = req.body
        console.log('req.body', req.body)
        const playlist = await Playlist.create({ user, thumbnail, title, music })
        if (playlist) {
            return res.json({
                success: true,
                playlist
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/update', async (req, res) => {
    try {
        const { id, thumbnail, title } = req.body
        const playlist = await Playlist.findById(id)
        if (playlist) {
            playlist.thumbnail = thumbnail
            playlist.title = title
            await playlist.save()
            return res.json({
                success: true,
                playlist
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/addmusic', async (req, res) => {
    try {
        const { playlist, music } = req.body
        const playlistObj = await Playlist.findById(playlist)
        if (playlistObj) {
            if (!playlistObj.music?.includes(music)) {
                playlistObj.music.push(music)
                await playlistObj.save()
                return res.json({
                    success: true,
                    playlist: playlistObj
                })
            }
            return res.json({
                success: false,
                error: `Music Already in Playlist`
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/removemusic', async (req, res) => {
    try {
        const { playlist, music } = req.body
        const playlistObj = await Playlist.findById(playlist)
        if (playlistObj) {
            if (playlistObj.music?.includes(music)) {
                const index = playlistObj.music.indexOf(music)
                playlistObj.music.splice(index, 1)
                await playlistObj.save()
                return res.json({
                    success: true,
                    playlist: playlistObj
                })
            }
            return res.json({
                success: false,
                error: `Music Not in Playlist`
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/fetch', async (req, res) => {
    try {
        // const { page } = req.query
        const playlists = await Playlist.find().populate('music').lean()
        if (playlists) {
            console.log(playlists)
            const formatedPlaylists = playlists.map((plyList) => ({ ...plyList, musicIds: plyList.music?.map((mus) => mus._id) }))
            console.log('playlists', formatedPlaylists)
            return res.json({
                success: true,
                playlists: formatedPlaylists
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/delete', async (req, res) => {
    try {
        const { id } = req.body
        const playlist = await Playlist.findByIdAndDelete(id)
        console.log('playlist', playlist)
        if (playlist) {
            return res.json({
                success: true
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/:id', async (req, res) => {
    try {
        // const { page } = req.query
        const { id } = req.params
        const playlist = await Playlist.findById(id).populate('music')
        if (playlist) {
            return res.json({
                success: true,
                playlist
            })
        }
        return res.json({
            success: false,
            error: 'Playlist Not Found!'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

router.post('/search', async (req, res) => {
    try {
        const { searchQuery } = req.body
        const musics = await Music.find({
            $or: [
                { title: { $regex: searchQuery, $options: "i" } }
            ]
        })
        const musicIds = musics.map((music) => music._id)
        const playlists = await Artist.find({
            $or: [
                { title: { $regex: searchQuery, $options: "i" } },
                { music: { $in: musicIds } },
            ],
        })
        console.log('playlists', playlists)
        if (playlists) {
            return res.json({
                success: true,
                playlists
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})

module.exports = router
© 2025 GrazzMean-Shell