7fife-backend/controllers/Artist.controller.js

261 lines
7.1 KiB
JavaScript
Raw Normal View History

2024-03-07 13:01:44 +00:00
const Artist = require('../models/artist.model');
2024-03-07 13:01:44 +00:00
const createArtist = async (req, res) => {
try {
console.log(req,"=============================>123");
console.log(req.body,"hooooo")
2024-03-07 13:01:44 +00:00
const { ArtistName } = req.body;
// Check if the required fields are present in the request body
if (!ArtistName) {
return res.status(400).send({
error_code: 400,
message: "Artist name is required"
});
}
let obj = {
ArtistName: ArtistName,
};
if (req.file) {
const baseUrl = `${req.protocol}://${req.get('host')}`;
const imagePath = `uploads/${req.file.filename}`;
const imageUrl = `${baseUrl}/${imagePath}`;
2024-03-07 13:01:44 +00:00
obj["image"] = {
fileName: req.file.filename,
fileAddress: req.file.path
};
obj["imageUrl"] = imageUrl;
2024-03-07 13:01:44 +00:00
}
// Save 'obj' to the database
const newArtist = await Artist.create(obj);
console.log("🚀 ~ createArtist ~ newArtist:", newArtist);
2024-03-07 13:01:44 +00:00
// Return a success response with the newly created Artist
return res.status(200).send({
error_code: 200,
2024-03-07 13:01:44 +00:00
message: "Artist created",
Artist: newArtist
});
} catch (err) {
console.error("Error inside createArtist Controller", err);
2024-03-07 13:01:44 +00:00
return res.status(500).send({
error_code: 500,
message: "Internal Server Error"
});
}
};
2024-03-07 13:01:44 +00:00
const updateArtist = async (req, res) => {
try {
const artistId = req.params.id;
const { ArtistName } = req.body;
2024-03-07 13:01:44 +00:00
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;
}
2024-03-07 13:01:44 +00:00
const updatedArtist = await Artist.findByIdAndUpdate(artistId, updates, { new: true });
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.error("Error inside updateArtist Controller", err);
2024-03-07 13:01:44 +00:00
return res.status(500).send({
error_code: 500,
message: "Internal Server Error"
});
}
};
2024-03-07 13:01:44 +00:00
// Delete Artist
const deleteArtist = async (req, res) => {
try {
const artistId = req.params.id;
// Delete artist by ID
const deletedArtist = await Artist.findByIdAndDelete(artistId);
// Check if the artist exists
if (!deletedArtist) {
return res.status(404).send({
error_code: 404,
message: "Artist not found"
});
}
return res.status(200).send({
error_code: 200,
message: "Artist deleted successfully"
});
} catch (err) {
console.log("Error inside deleteArtist Controller", err);
return res.status(500).send({
error_code: 500,
message: "Internal Server Error"
});
}
};
// Get Artist by ID
const getArtistById = async (req, res) => {
try {
const { id } = req.params;
// Find artist by ID
const artist = await Artist.findById(id);
console.log("🚀 ~ getArtistById ~ artist:", artist)
// Check if the artist exists
if (!artist) {
return res.status(404).send({
error_code: 404,
message: "Artist not found"
});
}
return res.status(200).send({
error_code: 200,
message: "Artist found",
Artist: artist
});
} catch (err) {
console.log("Error inside getArtistById Controller", err);
return res.status(500).send({
error_code: 500,
message: "Internal Server Error"
});
}
};
// Change Artist Status
const changeArtistStatus = async (req, res) => {
try {
const { id } = req.params;
const artistData = await Artist.findById(id);
if (!artistData) {
return res.status(400).send({
error_code: 400,
message: 'Ads not found'
});
2024-03-07 13:01:44 +00:00
}
2024-03-07 13:01:44 +00:00
artistData.status = artistData.status === 'activate' ? 'deactivate' : 'activate';
2024-03-07 13:01:44 +00:00
await artistData.save();
res.status(200).send({
message: `ads status toggled successfully to ${artistData.status}`,
artistData: artistData
2024-03-07 13:01:44 +00:00
});
} catch (err) {
2024-03-11 12:29:54 +00:00
console.error('Error inside update ', err);
2024-03-07 13:01:44 +00:00
res.status(500).send({
error_code: 500,
message: 'Internal Server Error'
2024-03-07 13:01:44 +00:00
});
}
2024-03-07 13:01:44 +00:00
};
const allArtist =async(req,res)=>{
try {
const artists = await Artist.find();
return res.status(200).json({
error_code: 200,
message: 'Artists fetched successfully',
artists
});
} catch (error) {
console.error('Error inside getAllArtist controller:', error);
return res.status(500).json({
error_code: 500,
message: 'Internal Server Error'
});
}
}
2024-03-07 13:01:44 +00:00
const getAllArtist = async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 5;
2024-03-07 13:01:44 +00:00
const skip = (page - 1) * limit;
const searchQuery = req.query.search || '';
// Build the query for fetching artists with pagination and search
const artistQuery = Artist.find({
ArtistName: { $regex: searchQuery, $options: 'i' }
});
2024-03-07 13:01:44 +00:00
// Execute the query with pagination
const artists = await artistQuery.skip(skip).limit(limit);
2024-03-07 13:01:44 +00:00
// Count total number of artists for pagination
const totalCount = await Artist.countDocuments({
ArtistName: { $regex: searchQuery, $options: 'i' }
});
2024-03-07 13:01:44 +00:00
res.json({
2024-03-07 13:01:44 +00:00
error_code: 200,
message: 'Artists retrieved successfully',
2024-03-07 13:01:44 +00:00
artists,
total_count: totalCount,
page,
limit
2024-03-07 13:01:44 +00:00
});
} catch (error) {
console.error('Error inside getAllArtist controller:', error);
return res.status(500).json({
error_code: 500,
message: 'Internal Server Error'
});
}
};
2024-03-07 13:01:44 +00:00
module.exports = {
createArtist,
updateArtist,
deleteArtist,
getArtistById,
changeArtistStatus, getAllArtist,
allArtist
2024-03-07 13:01:44 +00:00
};