diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/controllers/Artist.controller.js b/controllers/Artist.controller.js index 9d5e35f..4177fba 100644 --- a/controllers/Artist.controller.js +++ b/controllers/Artist.controller.js @@ -1,6 +1,6 @@ const Artist = require('../models/artist.model'); -// Create Artist + const createArtist = async (req, res) => { try { const { ArtistName } = req.body; @@ -18,23 +18,30 @@ const createArtist = async (req, res) => { }; if (req.file) { + const baseUrl = `${req.protocol}://${req.get('host')}`; + const imagePath = `uploads/${req.file.filename}`; + const imageUrl = `${baseUrl}/${imagePath}`; + console.log("🚀 ~ createArtist ~ imageUrl:", imageUrl); + obj["image"] = { fileName: req.file.filename, fileAddress: req.file.path }; + obj["imageUrl"] = imageUrl; } // Save 'obj' to the database const newArtist = await Artist.create(obj); + console.log("🚀 ~ createArtist ~ newArtist:", newArtist); // Return a success response with the newly created Artist return res.status(201).send({ - error_code: 200, + error_code: 201, message: "Artist created", Artist: newArtist }); } catch (err) { - console.log("Error inside createArtist Controller", err); + console.error("Error inside createArtist Controller", err); return res.status(500).send({ error_code: 500, message: "Internal Server Error" @@ -42,30 +49,48 @@ const createArtist = async (req, res) => { } }; -// Update Artist + const updateArtist = async (req, res) => { try { const artistId = req.params.id; - const updates = req.body; + const { ArtistName } = req.body; - // Update artist by ID + if (!ArtistName) { + return res.status(400).send({ + error_code: 400, + message: "Artist name is required" + }); + } + + const updates = { ArtistName }; + + if (req.file) { + const baseUrl = `${req.protocol}://${req.get('host')}`; + const imagePath = `uploads/${req.file.filename}`; + const imageUrl = `${baseUrl}/${imagePath}`; + console.log("🚀 ~ updateArtist ~ imageUrl:", imageUrl); + + updates.image = { + fileName: req.file.filename, + fileAddress: req.file.path + }; + updates.imageUrl = imageUrl; + } const updatedArtist = await Artist.findByIdAndUpdate(artistId, updates, { new: true }); - // Check if the artist exists if (!updatedArtist) { return res.status(404).send({ error_code: 404, message: "Artist not found" }); } - return res.status(200).send({ error_code: 200, message: "Artist updated successfully", Artist: updatedArtist }); } catch (err) { - console.log("Error inside updateArtist Controller", err); + console.error("Error inside updateArtist Controller", err); return res.status(500).send({ error_code: 500, message: "Internal Server Error" @@ -73,6 +98,7 @@ const updateArtist = async (req, res) => { } }; + // Delete Artist const deleteArtist = async (req, res) => { try { @@ -139,27 +165,27 @@ const changeArtistStatus = async (req, res) => { const { id } = req.params; const artistData = await Artist.findById(id); if (!artistData) { - return res.status(400).send({ - error_code: 400, - message: 'Ads not found' - }); + return res.status(400).send({ + error_code: 400, + message: 'Ads not found' + }); } - + artistData.status = artistData.status === 'activate' ? 'deactivate' : 'activate'; - + await artistData.save(); res.status(200).send({ - message: `ads status toggled successfully to ${artistData.status}`, - artistData: artistData + message: `ads status toggled successfully to ${artistData.status}`, + artistData: artistData }); - } catch (err) { + } catch (err) { console.error('Error inside update ', err); res.status(500).send({ - error_code: 500, - message: 'Internal Server Error' - + error_code: 500, + message: 'Internal Server Error' + }); - } + } }; const getAllArtist = async (req, res) => { @@ -179,7 +205,7 @@ const getAllArtist = async (req, res) => { } const artists = await artistsQuery.skip(skip).limit(limit); - + // Count total number of artists for pagination const totalCount = await Artist.countDocuments(artistsQuery.getQuery()); diff --git a/controllers/album.controller.js b/controllers/album.controller.js index a33fc21..cf2c408 100644 --- a/controllers/album.controller.js +++ b/controllers/album.controller.js @@ -30,7 +30,7 @@ const createAlbum = async (req, res) => { subcategoryId: subcategoryId, albumName, shortDescription, - imageUrl:imageUrl, + imageUrl: imageUrl, image: { fileName: req.file.filename, fileAddress: req.file.path, @@ -48,9 +48,9 @@ const createAlbum = async (req, res) => { }; const deletMany = async (req, res) => { try { - - const albums = await Album.deleteMany({ }); + + const albums = await Album.deleteMany({}); console.log("��� ~ deletMany ~ albums:", albums); return res.status(200).json({ error_code: 200, message: "Albums deleted successfully", albums: albums }); @@ -238,31 +238,33 @@ const updateAlbum = async (req, res) => { } // Handle image upload and update imageUrl - let imageUrl = album.image ? album.image.imageUrl : undefined; + let imageUrl = album.imageUrl; if (req.file) { const baseUrl = `${req.protocol}://${req.get('host')}`; const imagePath = `uploads/${req.file.filename}`; imageUrl = `${baseUrl}/${imagePath}`; } - // Update album fields - album.categoryId = categoryId; - album.subcategoryId = subcategoryId; - album.albumName = albumName; - album.shortDescription = shortDescription; - album.imageUrl = imageUrl; - album.image = req.file ? { - fileName: req.file.filename, - fileAddress: req.file.path, - } : album.image; + // Update album object with all fields + const updatedAlbum = { + categoryId: categoryId, + subcategoryId: subcategoryId, + albumName: albumName, + shortDescription: shortDescription, + imageUrl: imageUrl, + image: req.file ? { + fileName: req.file.filename, + fileAddress: req.file.path, + } : album.image + }; // Save the updated album - await album.save(); + await Album.findByIdAndUpdate(id, updatedAlbum); return res.status(200).json({ error_code: 200, message: "Album updated successfully", - album: album + album: updatedAlbum }); } catch (err) { console.error("Error inside UpdateAlbum Controller", err); @@ -273,6 +275,7 @@ const updateAlbum = async (req, res) => { } }; + // ------------------------------------------------------------------ const deleteAlbum = async (req, res) => { @@ -364,7 +367,7 @@ const getAlbums = async (req, res) => { _id: album._id, status: album.status, image: album.image, - imageUrl:album.imageUrl, + imageUrl: album.imageUrl, albumName: album.albumName, shortDescription: album.shortDescription, category: album.categoryId ? album.categoryId.name : null, diff --git a/controllers/subcategories.controller.js b/controllers/subcategories.controller.js index 4b54b9c..c0d8e50 100644 --- a/controllers/subcategories.controller.js +++ b/controllers/subcategories.controller.js @@ -1,5 +1,6 @@ const SubCategories = require('../models/subcategories.model'); // Import SubCategories model -const Categories = require('../models/categories.model') +const Categories = require('../models/categories.model'); +const Artist = require('../models/artist.model'); // const createsubCategories = async (req, res) => { // try { @@ -207,7 +208,7 @@ const updateSubCategories = async (req, res) => { const deleteMany = async (req, res) => { try { - const deleted = await SubCategories.deleteMany({}); // Passing an empty filter object deletes all documents + const deleted = await Artist.deleteMany({}); // Passing an empty filter object deletes all documents res.status(200).json({ error_code: 200, message: 'All categories deleted successfully', diff --git a/models/artist.model.js b/models/artist.model.js index 43e0dda..2c822f5 100644 --- a/models/artist.model.js +++ b/models/artist.model.js @@ -11,6 +11,7 @@ const artistSchema = new mongoose.Schema({ fileName: String, fileAddress: String }, + imageUrl: { type: String }, status: { type: String, enum: ['activate', 'deactivate'],