Untitled

 avatar
unknown
plain_text
6 days ago
1.9 kB
4
Indexable
// Update Profile Picture
router.patch('/profile/picture', auth, async(req, res) => {
    try {
        const userId = req.user.id;
        const { picture } = req.body;

        if (!picture) {
            return res.status(400).json({ error: 'No picture provided' });
        }

        // Convert picture from base64 to Buffer
        const pictureBuffer = Buffer.from(picture, 'base64');

        // Update profile picture
        const [affectedRows] = await UserProfile.update({ picture: pictureBuffer }, { where: { user_id: userId } });

        if (affectedRows === 0) {
            return res.status(404).json({ error: 'Profile not found' });
        }

        const updatedProfile = await UserProfile.findOne({ where: { user_id: userId } });

        res.json({
            message: 'Profile picture updated successfully',
            profile: sanitizeProfile(updatedProfile)
        });

    } catch (error) {
        console.error('Profile picture update error:', error);
        res.status(500).json({ error: 'Internal server error' });
    }
});
// Delete Profile Picture
router.delete('/profile/picture', auth, async(req, res) => {
    try {
        const userId = req.user.id;

        // Update profile picture to NULL (deleting it)
        const [affectedRows] = await UserProfile.update({ picture: null }, { where: { user_id: userId } });

        if (affectedRows === 0) {
            return res.status(404).json({ error: 'Profile not found' });
        }

        const updatedProfile = await UserProfile.findOne({ where: { user_id: userId } });

        res.json({
            message: 'Profile picture deleted successfully',
            profile: sanitizeProfile(updatedProfile)
        });

    } catch (error) {
        console.error('Profile picture delete error:', error);
        res.status(500).json({ error: 'Internal server error' });
    }
});
Leave a Comment