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.118.30.58
User: allssztx (535) | Group: allssztx (533)
Safe Mode: OFF
Disable Function:
NONE

name : artist.js
const express = require("express")
const Artist = require("../schema/Artist")
const Music = require("../schema/Music")
const VideoSong = require("../schema/VideoSong")
const router = express.Router()

router.post('/new', async (req, res) => {
    try {
        const { firstName, lastName, avatar, tags } = req.body
        const artist = await Artist.create({ firstName, lastName, avatar, tags })
        console.log('artist', artist)
        if (artist) {
            return res.json({
                success: true,
                artist
            })
        }
        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, firstName, lastName, avatar, tags } = req.body
        const artist = await Artist.findById(id)
        if (artist) {
            artist.firstName = firstName
            artist.lastName = lastName
            artist.avatar = avatar
            artist.tags = tags
            await artist.save()
            return res.json({
                success: true,
                artist
            })
        }
        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 artists = await Artist.find()
        console.log('artists', artists)
        if (artists) {
            return res.json({
                success: true,
                artists
            })
        }
        return res.json({
            success: false,
            error: 'Something Went Wrong'
        })
    } catch (error) {
        return res.json({
            success: false,
            error: error.message
        })
    }
})
router.post('/fetch-video-artist', async (req, res) => {
    try {
        // const { page } = req.query
        const videoSongArtist = await VideoSong.find().select('artist');
        const videoArtistId = videoSongArtist.map(item => item.artist.toString())
        console.log('videoArtistId', videoArtistId)
        const artists = await Artist.find({_id: videoArtistId})
        if (artists) {
            return res.json({
                success: true,
                artists
            })
        }
        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 artist = await Artist.findById(id)
        if (artist) {
            const music = await Music.find({ artist: artist._id }).populate('artist').populate('album')
            const videoSongs = await VideoSong.find({ artist: artist._id }).populate('artist').populate('album')
            return res.json({
                success: true,
                artist,
                music,
                videoSongs
            })
        }
        // const artists = await Artist.find()
        // console.log('artists', artists)
        // if (artists) {
        //     return res.json({
        //         success: true,
        //         artists
        //     })
        // }
        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 artists = await Artist.findByIdAndDelete(id)
        console.log('artists', artists)
        if (artists) {
            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('/search', async (req, res) => {
    try {
        const { searchQuery } = req.body
        const artists = await Artist.find({
            $or: [
                { firstName: { $regex: searchQuery, $options: "i" } },
                { lastName: { $regex: searchQuery, $options: "i" } },
                { tags: { $regex: searchQuery, $options: "i" } },
            ],
        })
        console.log('artists', artists)
        if (artists) {
            return res.json({
                success: true,
                artists
            })
        }
        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