Add .gitignore file and include image URLs for albums and artists

This commit is contained in:
Aniket Yadav 2024-03-12 11:09:06 +05:30
parent b8a5323689
commit fc5abc0432
5 changed files with 74 additions and 42 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

View File

@ -1,6 +1,6 @@
const Artist = require('../models/artist.model'); const Artist = require('../models/artist.model');
// Create Artist
const createArtist = async (req, res) => { const createArtist = async (req, res) => {
try { try {
const { ArtistName } = req.body; const { ArtistName } = req.body;
@ -18,23 +18,30 @@ const createArtist = async (req, res) => {
}; };
if (req.file) { 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"] = { obj["image"] = {
fileName: req.file.filename, fileName: req.file.filename,
fileAddress: req.file.path fileAddress: req.file.path
}; };
obj["imageUrl"] = imageUrl;
} }
// Save 'obj' to the database // Save 'obj' to the database
const newArtist = await Artist.create(obj); const newArtist = await Artist.create(obj);
console.log("🚀 ~ createArtist ~ newArtist:", newArtist);
// Return a success response with the newly created Artist // Return a success response with the newly created Artist
return res.status(201).send({ return res.status(201).send({
error_code: 200, error_code: 201,
message: "Artist created", message: "Artist created",
Artist: newArtist Artist: newArtist
}); });
} catch (err) { } catch (err) {
console.log("Error inside createArtist Controller", err); console.error("Error inside createArtist Controller", err);
return res.status(500).send({ return res.status(500).send({
error_code: 500, error_code: 500,
message: "Internal Server Error" message: "Internal Server Error"
@ -42,30 +49,48 @@ const createArtist = async (req, res) => {
} }
}; };
// Update Artist
const updateArtist = async (req, res) => { const updateArtist = async (req, res) => {
try { try {
const artistId = req.params.id; 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 }); const updatedArtist = await Artist.findByIdAndUpdate(artistId, updates, { new: true });
// Check if the artist exists
if (!updatedArtist) { if (!updatedArtist) {
return res.status(404).send({ return res.status(404).send({
error_code: 404, error_code: 404,
message: "Artist not found" message: "Artist not found"
}); });
} }
return res.status(200).send({ return res.status(200).send({
error_code: 200, error_code: 200,
message: "Artist updated successfully", message: "Artist updated successfully",
Artist: updatedArtist Artist: updatedArtist
}); });
} catch (err) { } catch (err) {
console.log("Error inside updateArtist Controller", err); console.error("Error inside updateArtist Controller", err);
return res.status(500).send({ return res.status(500).send({
error_code: 500, error_code: 500,
message: "Internal Server Error" message: "Internal Server Error"
@ -73,6 +98,7 @@ const updateArtist = async (req, res) => {
} }
}; };
// Delete Artist // Delete Artist
const deleteArtist = async (req, res) => { const deleteArtist = async (req, res) => {
try { try {
@ -139,27 +165,27 @@ const changeArtistStatus = async (req, res) => {
const { id } = req.params; const { id } = req.params;
const artistData = await Artist.findById(id); const artistData = await Artist.findById(id);
if (!artistData) { if (!artistData) {
return res.status(400).send({ return res.status(400).send({
error_code: 400, error_code: 400,
message: 'Ads not found' message: 'Ads not found'
}); });
} }
artistData.status = artistData.status === 'activate' ? 'deactivate' : 'activate'; artistData.status = artistData.status === 'activate' ? 'deactivate' : 'activate';
await artistData.save(); await artistData.save();
res.status(200).send({ res.status(200).send({
message: `ads status toggled successfully to ${artistData.status}`, message: `ads status toggled successfully to ${artistData.status}`,
artistData: artistData artistData: artistData
}); });
} catch (err) { } catch (err) {
console.error('Error inside update ', err); console.error('Error inside update ', err);
res.status(500).send({ res.status(500).send({
error_code: 500, error_code: 500,
message: 'Internal Server Error' message: 'Internal Server Error'
}); });
} }
}; };
const getAllArtist = async (req, res) => { const getAllArtist = async (req, res) => {
@ -179,7 +205,7 @@ const getAllArtist = async (req, res) => {
} }
const artists = await artistsQuery.skip(skip).limit(limit); const artists = await artistsQuery.skip(skip).limit(limit);
// Count total number of artists for pagination // Count total number of artists for pagination
const totalCount = await Artist.countDocuments(artistsQuery.getQuery()); const totalCount = await Artist.countDocuments(artistsQuery.getQuery());

View File

@ -30,7 +30,7 @@ const createAlbum = async (req, res) => {
subcategoryId: subcategoryId, subcategoryId: subcategoryId,
albumName, albumName,
shortDescription, shortDescription,
imageUrl:imageUrl, imageUrl: imageUrl,
image: { image: {
fileName: req.file.filename, fileName: req.file.filename,
fileAddress: req.file.path, fileAddress: req.file.path,
@ -48,9 +48,9 @@ const createAlbum = async (req, res) => {
}; };
const deletMany = async (req, res) => { const deletMany = async (req, res) => {
try { try {
const albums = await Album.deleteMany({ });
const albums = await Album.deleteMany({});
console.log("<22><><EFBFBD> ~ deletMany ~ albums:", albums); console.log("<22><><EFBFBD> ~ deletMany ~ albums:", albums);
return res.status(200).json({ error_code: 200, message: "Albums deleted successfully", 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 // Handle image upload and update imageUrl
let imageUrl = album.image ? album.image.imageUrl : undefined; let imageUrl = album.imageUrl;
if (req.file) { if (req.file) {
const baseUrl = `${req.protocol}://${req.get('host')}`; const baseUrl = `${req.protocol}://${req.get('host')}`;
const imagePath = `uploads/${req.file.filename}`; const imagePath = `uploads/${req.file.filename}`;
imageUrl = `${baseUrl}/${imagePath}`; imageUrl = `${baseUrl}/${imagePath}`;
} }
// Update album fields // Update album object with all fields
album.categoryId = categoryId; const updatedAlbum = {
album.subcategoryId = subcategoryId; categoryId: categoryId,
album.albumName = albumName; subcategoryId: subcategoryId,
album.shortDescription = shortDescription; albumName: albumName,
album.imageUrl = imageUrl; shortDescription: shortDescription,
album.image = req.file ? { imageUrl: imageUrl,
fileName: req.file.filename, image: req.file ? {
fileAddress: req.file.path, fileName: req.file.filename,
} : album.image; fileAddress: req.file.path,
} : album.image
};
// Save the updated album // Save the updated album
await album.save(); await Album.findByIdAndUpdate(id, updatedAlbum);
return res.status(200).json({ return res.status(200).json({
error_code: 200, error_code: 200,
message: "Album updated successfully", message: "Album updated successfully",
album: album album: updatedAlbum
}); });
} catch (err) { } catch (err) {
console.error("Error inside UpdateAlbum Controller", err); console.error("Error inside UpdateAlbum Controller", err);
@ -273,6 +275,7 @@ const updateAlbum = async (req, res) => {
} }
}; };
// ------------------------------------------------------------------ // ------------------------------------------------------------------
const deleteAlbum = async (req, res) => { const deleteAlbum = async (req, res) => {
@ -364,7 +367,7 @@ const getAlbums = async (req, res) => {
_id: album._id, _id: album._id,
status: album.status, status: album.status,
image: album.image, image: album.image,
imageUrl:album.imageUrl, imageUrl: album.imageUrl,
albumName: album.albumName, albumName: album.albumName,
shortDescription: album.shortDescription, shortDescription: album.shortDescription,
category: album.categoryId ? album.categoryId.name : null, category: album.categoryId ? album.categoryId.name : null,

View File

@ -1,5 +1,6 @@
const SubCategories = require('../models/subcategories.model'); // Import SubCategories model 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) => { // const createsubCategories = async (req, res) => {
// try { // try {
@ -207,7 +208,7 @@ const updateSubCategories = async (req, res) => {
const deleteMany = async (req, res) => { const deleteMany = async (req, res) => {
try { 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({ res.status(200).json({
error_code: 200, error_code: 200,
message: 'All categories deleted successfully', message: 'All categories deleted successfully',

View File

@ -11,6 +11,7 @@ const artistSchema = new mongoose.Schema({
fileName: String, fileName: String,
fileAddress: String fileAddress: String
}, },
imageUrl: { type: String },
status: { status: {
type: String, type: String,
enum: ['activate', 'deactivate'], enum: ['activate', 'deactivate'],