diff --git a/controllers/categoryController.js b/controllers/categoryController.js index bd1bb00..b9dda23 100644 --- a/controllers/categoryController.js +++ b/controllers/categoryController.js @@ -6,10 +6,16 @@ import cloudinary from "cloudinary"; export const createCategory = async (req, res) => { try { - const files = req.files.image; + const files = req.files; - // console.log(files) - const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + // ctegory upload + const myCloud = await cloudinary.uploader.upload(files.image.tempFilePath, { + folder: "cmp/image", + }, + function (error, result) { (result, error) }); + + // ctegory(banner)upload + const CategoryBanner = await cloudinary.uploader.upload(files.category_banner.tempFilePath, { folder: "cmp/image", }, function (error, result) { (result, error) }); @@ -21,6 +27,10 @@ export const createCategory = async (req, res) => { public_id: myCloud.public_id, url: myCloud.secure_url, }, + category_banner: { + public_id: CategoryBanner.public_id, + url: CategoryBanner.secure_url, + }, }); res.status(201).json({ @@ -40,7 +50,7 @@ export const createCategory = async (req, res) => { export const getAllCategory = async (req, res) => { try { - const category = await Category.find(); + const category = await Category.find().sort({ createdAt: -1 }); // console.log(category) res.status(200).json({ success: true, @@ -87,22 +97,42 @@ export const updateCategory = async (req, res) => { if (req.files) { const categ = await Category.findById(req.params.id); + if (req.files.image) { + const imageId = categ.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 files = req.files.image; + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "image", + }, + function (error, result) { (result, error) }); + newCategoryData.image = { + public_id: myCloud.public_id, + url: myCloud.secure_url, + }; + } + if (req.files.category_banner) { + const imageId = categ.category_banner.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 files = req.files.category_banner; + const BannerImage = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "image", + }, + function (error, result) { (result, error) }); + newCategoryData.category_banner = { + public_id: BannerImage.public_id, + url: BannerImage.secure_url, + }; + } + - const imageId = categ.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 files = req.files.image; - const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { - folder: "image", - }, - function (error, result) { (result, error) }); // console.log(myCloud) - newCategoryData.image = { - public_id: myCloud.public_id, - url: myCloud.secure_url, - }; + } // console.log(newCategoryData) //req.user.id, @@ -135,6 +165,12 @@ export const deleteOneCategory = async (req, res) => { try { //delete image from cloudinary const categ = await Category.findById(req.params.id); + if (categ.category_banner.public_id) { + const bannerImageId = categ.category_banner.public_id; + await cloudinary.uploader.destroy(bannerImageId) + + } + // console.log(categ) const imageId = categ.image.public_id; await cloudinary.uploader.destroy(imageId) @@ -151,6 +187,7 @@ export const deleteOneCategory = async (req, res) => { // category, }); } catch (error) { + // console.log(error) res.status(500).json({ success: false, msg: "Failled to Delete !!" diff --git a/models/categoryModel.js b/models/categoryModel.js index 782dfca..556dd88 100644 --- a/models/categoryModel.js +++ b/models/categoryModel.js @@ -16,6 +16,17 @@ const categorySchema = new mongoose.Schema( required: true, }, }, + category_banner: + { + public_id: { + type: String, + required: true, + }, + url: { + type: String, + required: true, + }, + }, addedOn: { type: Date, default: Date.now diff --git a/tmp/tmp-1-1666848897487 b/tmp/tmp-1-1666848897487 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-1-1666848897487 differ diff --git a/tmp/tmp-1-1666850906209 b/tmp/tmp-1-1666850906209 new file mode 100644 index 0000000..b9a863b Binary files /dev/null and b/tmp/tmp-1-1666850906209 differ diff --git a/tmp/tmp-1-1666851964376 b/tmp/tmp-1-1666851964376 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-1-1666851964376 differ diff --git a/tmp/tmp-2-1666848897531 b/tmp/tmp-2-1666848897531 new file mode 100644 index 0000000..25c7510 Binary files /dev/null and b/tmp/tmp-2-1666848897531 differ diff --git a/tmp/tmp-2-1666850932088 b/tmp/tmp-2-1666850932088 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-2-1666850932088 differ diff --git a/tmp/tmp-2-1666851964494 b/tmp/tmp-2-1666851964494 new file mode 100644 index 0000000..b9a863b Binary files /dev/null and b/tmp/tmp-2-1666851964494 differ diff --git a/tmp/tmp-3-1666852001473 b/tmp/tmp-3-1666852001473 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-3-1666852001473 differ