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

192 lines
5.9 KiB
JavaScript

const constant = require('../util/ads.constant');
const Ads = require('../models/Ads.model')
const create = async (req, res) => {
try {
console.log('create add')
const fileName = req.files[0].filename ;
let obj = {
adsTitle: req.body.adsTitle ? req.body.adsTitle : undefined,
redirectLink: req.body.redirectLink ? req.body.redirectLink : undefined,
advertiseAs: req.body.advertiseAs ? req.body.advertiseAs : undefined,
status: req.body.status ? req.body.status : undefined
}
if (req.body.advertiseAs == constant.adsType.image) {
obj[constant.field.advertiseImage] = "/uploads/" + fileName
}
else {
obj[constant.field.advertiseVideo] = "/uploads/" + fileName
}
const ad = await Ads.create(obj);
return res.status(201).send({
error_code : 200,
message: `Ad with ${req.body.advertiseAs} got created`
})
} catch (err) {
console.log('Error inside create of Ads Controller', err);
res.status(500).send({
error_code : 500,
message: "Internal Server Error"
})
}
}
const getad = async(req,res) => {
try{
let obj = {}
const ads = await Ads.find(obj);
return res.status(200).send(constant.adsGenerator(ads, req));
}catch(err){
console.log('Error inside getad Controller',err);
res.status(500).send({
error_code : 500,
message : 'Internal Server Error'
})
}
}
const updateAds = async(req,res) => {
try{
const ad = await Ads.findById(req.params.id);
let obj = {
adsTitle: req.body.adsTitle ? req.body.adsTitle : undefined,
redirectLink: req.body.redirectLink ? req.body.redirectLink : undefined,
status: req.body.status ? req.body.status : undefined
}
if(req.body.advertiseAs)
{
const fileName = req.files[0].filename ;
if (req.body.advertiseAs == constant.adsType.image) {
obj[constant.field.advertiseImage] = "/uploads/" + fileName
delete ad.advertiseVideo;
}
else {
obj[constant.field.advertiseVideo] = "/uploads/" + fileName
delete ad.advertiseImage;
}
}
await ad.updateOne(obj);
res.status(201).send({
error_code : 200,
message : 'Ads got updated'
})
}catch(err){
console.log('Error inside updateAds Controller',err);
return res.status(500).send({
error_code : 500,
message : 'Internal Server Error'
})
}
}
const deletead = async(req,res) => {
try{
let id = req.params.id;
await Ads.deleteOne({_id :id});
return res.status(201).send({
error_code : 200,
message : 'ads got deleted'
})
}catch(err){
console.log('Error Occured inside deletead of ads Controller',err);
return res.status(500).send({
error_code : 500,
message : 'Internal Server Error'
})
}
};
const updateAdsTime = async (req, res) => {
try {
const { id } = req.params;
const { adsTiming } = req.body; // Assuming adsTiming is provided in minutes
// Find the advertisement by ID
const ad = await Ads.findById(id);
// If the advertisement is not found, return 404 Not Found
if (!ad) {
return res.status(404).json({ error_code: 404, message: 'Advertisement not found' });
}
// Update the advertisement's show timing
ad.adsTime = adsTiming; // Assuming adsTiming is in minutes
await ad.save();
// Send success response
return res.status(200).json({ error_code: 200, message: 'Advertisement show timing updated successfully' });
} catch (err) {
console.error('Error inside updateAdsTime Controller:', err);
return res.status(500).json({ error_code: 500, message: 'Internal Server Error' });
}
};
// const updateAdsStatus = async (req, res) => {
// try {
// const { id } = req.params;
// // Find the advertisement by ID
// const ad = await Ads.findById(id);
// // If the advertisement is not found, return 404 Not Found
// if (!ad) {
// return res.status(404).json({ error_code: 404, message: 'Advertisement not found' });
// }
// // Toggle the advertisement status between active and inactive
// ad.status = ad.status === constant.status.active ? constant.status.inactive : constant.status.active;
// await ad.save();
// // Send success response
// return res.status(200).json({ error_code: 200, message: 'Advertisement status updated successfully' });
// } catch (err) {
// console.error('Error inside updateAdsStatus Controller:', err);
// return res.status(500).json({ error_code: 500, message: 'Internal Server Error' });
// }
// };
const updateAdsStatus = async (req, res) => {
try {
const { id } = req.params;
const adsData = await Ads.findById(id);
if (!adsData) {
return res.status(400).send({
error_code: 400,
message: 'Ads not found'
});
}
adsData.status = adsData.status === 'activate' ? 'deactivate' : 'activate';
await adsData.save();
res.status(200).send({
message: `ads status toggled successfully to ${adsData.status}`,
adsData: adsData
});
} catch (err) {
console.error('Error inside update admin', err);
res.status(500).send({
error_code: 500,
message: 'Internal Server Error'
});
}
};
module.exports = {
create,
getad,
updateAds,
deletead,
updateAdsTime,
// updateAdsStatus
updateAdsStatus
}