diff --git a/app.js b/app.js index b33e4b3..2ad18e7 100644 --- a/app.js +++ b/app.js @@ -40,4 +40,7 @@ app.use("/api", Event); //Offers import Offer from "./routes/OffersRoute.js" app.use("/api", Offer); +//Offers +import banner from "./routes/bannerRoute.js" +app.use("/api", banner); export default app; \ No newline at end of file diff --git a/controllers/bannerController.js b/controllers/bannerController.js new file mode 100644 index 0000000..a4594a8 --- /dev/null +++ b/controllers/bannerController.js @@ -0,0 +1,165 @@ +import Banners from "../models/bannerModel.js" +import cloudinary from "cloudinary"; +// import cloudinary from "../Utils/cloudinary.js" +//import { v2 as cloudinary } from 'cloudinary' + +export const createBanner = async (req, res) => { + + try { + const files = req.files.image; + + // console.log(files) + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "cmp/image", + }, + function (error, result) { (result, error) }); + const { title, section, startDate, endDate } = req.body; + + const data = await Banners.create({ + title, + image: { + public_id: myCloud.public_id, + url: myCloud.secure_url, + }, + section, + startDate, + endDate + + }); + res.status(201).json({ + success: true, + msg: " create Banner Successfully!!", + data, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to create Banner !!" + }); + } + +}; +//get All Banners +export const getAllBanner = async (req, res) => { + + try { + const banner = await Banners.find(); + // console.log(category) + res.status(200).json({ + success: true, + msg: " fetch Successfully!!", + banner, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to fetch !!" + }); + } + +}; +//get One Banner +export const getOneBanner = async (req, res) => { + + try { + const banner = await Banners.findById(req.params.id); + // console.log(category) + res.status(200).json({ + success: true, + msg: " fetch Successfully!!", + banner, + }); + } catch (error) { + // console.log(error) + res.status(500).json({ + success: false, + msg: "Failled to fetch !!" + }); + } + +}; + +// 3.update Event +export const updateBanner = async (req, res) => { + try { + const newBannerData = { + title: req.body.title, + section: req.body.section, + startDate: req.body.startDate, + endDate: req.body.endDate, + }; + + + if (req.files) { + const files = req.files.image; + const getBanner = await Banners.findById(req.params.id); + + const imageId = getBanner.image.public_id; + // console.log(imageId) + //delete image from claudinary + await cloudinary.uploader.destroy(imageId) + // await cloudinary.uploader.destroy(imageId, function (result) { console.log(result) }); + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "image", + }, + function (error, result) { (result, error) }); + // console.log(myCloud) + newBannerData.image = { + public_id: myCloud.public_id, + url: myCloud.secure_url, + }; + } + + //req.user.id, + const ModifyBanner = await Banners.findByIdAndUpdate(req.params.id, newBannerData, + + { new: true } + // runValidators: true, + // useFindAndModify: false, + ); + + res.status(200).json({ + success: true, + ModifyBanner + }); + + } catch (error) { + // console.log(error) + res.status(500).json({ + success: false, + msg: "Failled to UpDate Banner!!" + + }); + } + +}; + +//delete one Banner +export const deleteBanner = async (req, res) => { + + try { + //delete image from cloudinary + const getBanner = await Banners.findById(req.params.id); + // console.log(categ) + const imageId = getBanner.image.public_id; + await cloudinary.uploader.destroy(imageId) + + //-------------------------// + const banner = await Banners.findByIdAndDelete(req.params.id) + if (!banner) { + return res.status(400).json({ message: 'banner Not Found' }); + } + await banner.remove(); + res.status(200).json({ + success: true, + msg: "banner Deleted Successfully!!", + // category, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to Delete banner !!" + }); + } + +}; diff --git a/controllers/directoryController.js b/controllers/directoryController.js index 5f20924..30102e7 100644 --- a/controllers/directoryController.js +++ b/controllers/directoryController.js @@ -4,11 +4,11 @@ export const createDirectory = async (req, res) => { try { let images; - console.log(req.body) - console.log(req.files) + // console.log(req.body) + // console.log(req.body.image) if (req.files) { const files = req.files.image; - console.log(files) + // console.log(files) const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { folder: "cmp/image", }, diff --git a/models/bannerModel.js b/models/bannerModel.js new file mode 100644 index 0000000..1b4439e --- /dev/null +++ b/models/bannerModel.js @@ -0,0 +1,39 @@ +import mongoose from "mongoose" +const bannerSchema = new mongoose.Schema( + { + title: { + type: String, + required: true + }, + image: + { + public_id: { + type: String, + required: true, + }, + url: { + type: String, + required: true, + }, + }, + section: { + type: String, + required: true + }, + startDate: { + type: Date, + required: true + }, + endDate: { + type: Date, + required: true + }, + addedOn: { + type: Date, + default: Date.now + }, + + }, { timestamps: true } +); +const bannerModel = mongoose.model("banner", bannerSchema); +export default bannerModel \ No newline at end of file diff --git a/routes/bannerRoute.js b/routes/bannerRoute.js new file mode 100644 index 0000000..8ab39d4 --- /dev/null +++ b/routes/bannerRoute.js @@ -0,0 +1,16 @@ +import express from "express"; +import { + createBanner, + getAllBanner, + updateBanner, + deleteBanner, + getOneBanner +} from "../controllers/bannerController.js" +const router = express.Router(); + +router.route("/banner/create/").post(createBanner) +router.route("/banner/getAll/").get(getAllBanner) +router.route("/banner/getOne/:id").get(getOneBanner) +router.route("/banner/update/:id").put(updateBanner); +router.route("/banner/delete/:id").delete(deleteBanner); +export default router; \ No newline at end of file diff --git a/server.js b/server.js index d8e985c..e51d529 100644 --- a/server.js +++ b/server.js @@ -8,7 +8,7 @@ import cloudinary from "cloudinary" // Connecting to database connectDatabase(); -// console.log(process.env.CLOUDINARY_API_KEY) +//console.log(process.env.CLOUDINARY_API_KEY) //cloudenary uses cloudinary.config({ cloud_name: process.env.CLOUDINARY_NAME, diff --git a/tmp/tmp-1-1655704343318 b/tmp/tmp-1-1655704343318 new file mode 100644 index 0000000..bf08304 Binary files /dev/null and b/tmp/tmp-1-1655704343318 differ diff --git a/tmp/tmp-1-1655704714841 b/tmp/tmp-1-1655704714841 new file mode 100644 index 0000000..bf08304 Binary files /dev/null and b/tmp/tmp-1-1655704714841 differ diff --git a/tmp/tmp-1-1655707290465 b/tmp/tmp-1-1655707290465 new file mode 100644 index 0000000..5276b0e Binary files /dev/null and b/tmp/tmp-1-1655707290465 differ diff --git a/tmp/tmp-2-1655704484188 b/tmp/tmp-2-1655704484188 new file mode 100644 index 0000000..bf08304 Binary files /dev/null and b/tmp/tmp-2-1655704484188 differ diff --git a/tmp/tmp-2-1655704962606 b/tmp/tmp-2-1655704962606 new file mode 100644 index 0000000..bf08304 Binary files /dev/null and b/tmp/tmp-2-1655704962606 differ diff --git a/tmp/tmp-3-1655705709722 b/tmp/tmp-3-1655705709722 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-3-1655705709722 differ diff --git a/tmp/tmp-4-1655705797633 b/tmp/tmp-4-1655705797633 new file mode 100644 index 0000000..b9a863b Binary files /dev/null and b/tmp/tmp-4-1655705797633 differ diff --git a/tmp/tmp-5-1655705927779 b/tmp/tmp-5-1655705927779 new file mode 100644 index 0000000..25c7510 Binary files /dev/null and b/tmp/tmp-5-1655705927779 differ diff --git a/tmp/tmp-6-1655706570178 b/tmp/tmp-6-1655706570178 new file mode 100644 index 0000000..25c7510 Binary files /dev/null and b/tmp/tmp-6-1655706570178 differ diff --git a/tmp/tmp-7-1655706620303 b/tmp/tmp-7-1655706620303 new file mode 100644 index 0000000..25c7510 Binary files /dev/null and b/tmp/tmp-7-1655706620303 differ