add category banner image

This commit is contained in:
pawan-dot 2022-10-27 11:58:27 +05:30
parent eda538336b
commit 16b35a5c44
9 changed files with 66 additions and 18 deletions

View File

@ -6,10 +6,16 @@ import cloudinary from "cloudinary";
export const createCategory = async (req, res) => { export const createCategory = async (req, res) => {
try { try {
const files = req.files.image; const files = req.files;
// console.log(files) // ctegory upload
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { 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", folder: "cmp/image",
}, },
function (error, result) { (result, error) }); function (error, result) { (result, error) });
@ -21,6 +27,10 @@ export const createCategory = async (req, res) => {
public_id: myCloud.public_id, public_id: myCloud.public_id,
url: myCloud.secure_url, url: myCloud.secure_url,
}, },
category_banner: {
public_id: CategoryBanner.public_id,
url: CategoryBanner.secure_url,
},
}); });
res.status(201).json({ res.status(201).json({
@ -40,7 +50,7 @@ export const createCategory = async (req, res) => {
export const getAllCategory = async (req, res) => { export const getAllCategory = async (req, res) => {
try { try {
const category = await Category.find(); const category = await Category.find().sort({ createdAt: -1 });
// console.log(category) // console.log(category)
res.status(200).json({ res.status(200).json({
success: true, success: true,
@ -87,22 +97,42 @@ export const updateCategory = async (req, res) => {
if (req.files) { if (req.files) {
const categ = await Category.findById(req.params.id); 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) // console.log(myCloud)
newCategoryData.image = {
public_id: myCloud.public_id,
url: myCloud.secure_url,
};
} }
// console.log(newCategoryData) // console.log(newCategoryData)
//req.user.id, //req.user.id,
@ -135,6 +165,12 @@ export const deleteOneCategory = async (req, res) => {
try { try {
//delete image from cloudinary //delete image from cloudinary
const categ = await Category.findById(req.params.id); 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) // console.log(categ)
const imageId = categ.image.public_id; const imageId = categ.image.public_id;
await cloudinary.uploader.destroy(imageId) await cloudinary.uploader.destroy(imageId)
@ -151,6 +187,7 @@ export const deleteOneCategory = async (req, res) => {
// category, // category,
}); });
} catch (error) { } catch (error) {
// console.log(error)
res.status(500).json({ res.status(500).json({
success: false, success: false,
msg: "Failled to Delete !!" msg: "Failled to Delete !!"

View File

@ -16,6 +16,17 @@ const categorySchema = new mongoose.Schema(
required: true, required: true,
}, },
}, },
category_banner:
{
public_id: {
type: String,
required: true,
},
url: {
type: String,
required: true,
},
},
addedOn: { addedOn: {
type: Date, type: Date,
default: Date.now default: Date.now

BIN
tmp/tmp-1-1666848897487 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-1-1666850906209 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
tmp/tmp-1-1666851964376 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-2-1666848897531 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
tmp/tmp-2-1666850932088 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-2-1666851964494 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
tmp/tmp-3-1666852001473 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB