add category banner image
@ -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,7 +97,7 @@ 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
|
||||
@ -98,12 +108,32 @@ export const updateCategory = async (req, res) => {
|
||||
folder: "image",
|
||||
},
|
||||
function (error, result) { (result, error) });
|
||||
// console.log(myCloud)
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// console.log(myCloud)
|
||||
|
||||
}
|
||||
// console.log(newCategoryData)
|
||||
//req.user.id,
|
||||
const ModifyCategory = await Category.findByIdAndUpdate(req.params.id, newCategoryData,
|
||||
@ -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 !!"
|
||||
|
@ -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
|
||||
|
BIN
tmp/tmp-1-1666848897487
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-1-1666850906209
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
tmp/tmp-1-1666851964376
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1666848897531
Normal file
After Width: | Height: | Size: 1.7 MiB |
BIN
tmp/tmp-2-1666850932088
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-2-1666851964494
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
tmp/tmp-3-1666852001473
Normal file
After Width: | Height: | Size: 620 KiB |