Product page Done
This commit is contained in:
parent
07fb71f117
commit
428df6f30c
13
package-lock.json
generated
13
package-lock.json
generated
@ -26,6 +26,7 @@
|
|||||||
"nodemailer": "^6.9.4",
|
"nodemailer": "^6.9.4",
|
||||||
"nodemon": "^3.0.1",
|
"nodemon": "^3.0.1",
|
||||||
"secure-random-password": "^0.2.3",
|
"secure-random-password": "^0.2.3",
|
||||||
|
"uuid": "^9.0.1",
|
||||||
"validator": "^13.7.0"
|
"validator": "^13.7.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -2283,6 +2284,18 @@
|
|||||||
"node": ">= 0.4.0"
|
"node": ">= 0.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/uuid": {
|
||||||
|
"version": "9.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz",
|
||||||
|
"integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==",
|
||||||
|
"funding": [
|
||||||
|
"https://github.com/sponsors/broofa",
|
||||||
|
"https://github.com/sponsors/ctavan"
|
||||||
|
],
|
||||||
|
"bin": {
|
||||||
|
"uuid": "dist/bin/uuid"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/validator": {
|
"node_modules/validator": {
|
||||||
"version": "13.7.0",
|
"version": "13.7.0",
|
||||||
"resolved": "https://registry.npmjs.org/validator/-/validator-13.7.0.tgz",
|
"resolved": "https://registry.npmjs.org/validator/-/validator-13.7.0.tgz",
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
"nodemailer": "^6.9.4",
|
"nodemailer": "^6.9.4",
|
||||||
"nodemon": "^3.0.1",
|
"nodemon": "^3.0.1",
|
||||||
"secure-random-password": "^0.2.3",
|
"secure-random-password": "^0.2.3",
|
||||||
|
"uuid": "^9.0.1",
|
||||||
"validator": "^13.7.0"
|
"validator": "^13.7.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ export const updateCategory = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||||
const { _id } = req.params;
|
const { _id } = req.params;
|
||||||
console.log(_id);
|
|
||||||
const { categoryName } = req.body;
|
const { categoryName } = req.body;
|
||||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||||
return res.status(404).json({ error: "Can not find the document " });
|
return res.status(404).json({ error: "Can not find the document " });
|
||||||
@ -90,11 +90,9 @@ export const deleteCategory = async (req, res) => {
|
|||||||
|
|
||||||
const deleteCategory = await CategoryModel.findOneAndDelete({ _id: _id });
|
const deleteCategory = await CategoryModel.findOneAndDelete({ _id: _id });
|
||||||
if (!deleteCategory) {
|
if (!deleteCategory) {
|
||||||
return res
|
return res.status(404).json({
|
||||||
.status(404)
|
error: "Can not find the document with the provided id to delete ",
|
||||||
.json({
|
});
|
||||||
error: "Can not find the document with the provided id to delete ",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
res.status(200).json({ success: true, deleteCategory });
|
res.status(200).json({ success: true, deleteCategory });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Product } from "./ProductModel.js";
|
import { Product } from "./ProductModel.js";
|
||||||
import cloudinary from "../../Utils/cloudinary.js";
|
import cloudinary from "../../Utils/cloudinary.js";
|
||||||
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
|
||||||
export const createProduct = async (req, res) => {
|
export const createProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@ -34,6 +35,8 @@ export const createProduct = async (req, res) => {
|
|||||||
|
|
||||||
req.body.image = imagesLinks;
|
req.body.image = imagesLinks;
|
||||||
req.body.addedBy = req.user.id;
|
req.body.addedBy = req.user.id;
|
||||||
|
const newUniquid = uuidv4();
|
||||||
|
req.body.uniqueId = newUniquid.replace(/-/g, "").substring(0, 10);
|
||||||
|
|
||||||
const data = await Product.create({ ...req.body });
|
const data = await Product.create({ ...req.body });
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
@ -42,7 +45,6 @@ export const createProduct = async (req, res) => {
|
|||||||
msg: " create Product Successfully!!",
|
msg: " create Product Successfully!!",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(error)
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: error.message,
|
msg: error.message,
|
||||||
@ -87,42 +89,30 @@ export const getOneProduct = async (req, res) => {
|
|||||||
|
|
||||||
// 3.update Product
|
// 3.update Product
|
||||||
export const updateProduct = async (req, res) => {
|
export const updateProduct = async (req, res) => {
|
||||||
|
const { name, description, price, category, image } = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const newProductData = {
|
// Prepare an array for the images
|
||||||
// name: req.body.name,
|
const jsonArray = JSON.parse(image);
|
||||||
// description: req.body.description,
|
const AllImages = jsonArray.map(({ public_id, url }) => ({
|
||||||
// price: req.body.base_Price,
|
public_id,
|
||||||
|
url,
|
||||||
|
}));
|
||||||
|
|
||||||
// }
|
if (req.files && req.files.newImages) {
|
||||||
|
const newuploadImages = Array.isArray(req.files.newImages)
|
||||||
if (req.files) {
|
? req.files.newImages
|
||||||
// req.body.addedBy = req.user.id;
|
: [req.files.newImages];
|
||||||
// const image_file = req.files.image;
|
|
||||||
const getProduct = await Product.findById(req.params.id);
|
|
||||||
|
|
||||||
if (getProduct) {
|
|
||||||
// Deleting Images From Cloudinary
|
|
||||||
for (let i = 0; i < getProduct.image.length; i++) {
|
|
||||||
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let images = [];
|
|
||||||
let Allfiles = req.files.image;
|
|
||||||
if (typeof Allfiles.tempFilePath === "string") {
|
|
||||||
let filepath = Allfiles.tempFilePath;
|
|
||||||
|
|
||||||
images.push(filepath);
|
|
||||||
} else {
|
|
||||||
Allfiles.map((item) => {
|
|
||||||
images.push(item.tempFilePath);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const imagesLinks = [];
|
const imagesLinks = [];
|
||||||
for (let i = 0; i < images.length; i++) {
|
|
||||||
const result = await cloudinary.v2.uploader.upload(images[i], {
|
for (let i = 0; i < newuploadImages.length; i++) {
|
||||||
folder: "jatinMor/product",
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
});
|
newuploadImages[i].tempFilePath,
|
||||||
|
{
|
||||||
|
folder: "jatinMor/product",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
imagesLinks.push({
|
imagesLinks.push({
|
||||||
public_id: result.public_id,
|
public_id: result.public_id,
|
||||||
@ -130,24 +120,47 @@ export const updateProduct = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
req.body.image = imagesLinks;
|
// Combine the existing images and the newly uploaded images
|
||||||
|
const updatedImages = [...AllImages, ...imagesLinks];
|
||||||
|
|
||||||
|
// Perform the product update
|
||||||
|
const ModifyProduct = await Product.findOneAndUpdate(
|
||||||
|
{ _id: req.params.id },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
price,
|
||||||
|
category,
|
||||||
|
image: updatedImages,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
ModifyProduct,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const ModifyProduct = await Product.findOneAndUpdate(
|
||||||
|
{ _id: req.params.id },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
price,
|
||||||
|
category,
|
||||||
|
image: AllImages,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
ModifyProduct,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const ModifyProduct = await Product.findByIdAndUpdate(
|
|
||||||
req.params.id,
|
|
||||||
req.body,
|
|
||||||
|
|
||||||
{ new: true }
|
|
||||||
// runValidators: true,
|
|
||||||
// useFindAndModify: false,
|
|
||||||
);
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
ModifyProduct,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(error)
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
msg: error.message ? error.message : "Something went wrong!",
|
||||||
@ -155,6 +168,30 @@ export const updateProduct = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteImageFromCloudinary = async (req, res) => {
|
||||||
|
const { public_id } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!public_id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
msg: "Please Provide Product ID!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const response = await cloudinary.v2.uploader.destroy(public_id);
|
||||||
|
if (response) {
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Product Deleted Successfully!!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
msg: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
//delete one Product
|
//delete one Product
|
||||||
export const deleteProduct = async (req, res) => {
|
export const deleteProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
@ -9,6 +9,10 @@ const productSchema = new Schema(
|
|||||||
required: [true, "Please Enter product Name"],
|
required: [true, "Please Enter product Name"],
|
||||||
trim: true,
|
trim: true,
|
||||||
},
|
},
|
||||||
|
uniqueId: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
description: {
|
description: {
|
||||||
type: String,
|
type: String,
|
||||||
maxLength: [100, "description cannot exceed 100 characters"],
|
maxLength: [100, "description cannot exceed 100 characters"],
|
||||||
|
@ -1,21 +1,31 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import {
|
import {
|
||||||
createProduct,
|
createProduct,
|
||||||
getAllProduct,
|
getAllProduct,
|
||||||
updateProduct,
|
updateProduct,
|
||||||
deleteProduct,
|
deleteProduct,
|
||||||
getOneProduct,
|
getOneProduct,
|
||||||
|
deleteImageFromCloudinary,
|
||||||
|
} from "./ProductController.js";
|
||||||
} from "./ProductController.js"
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"
|
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||||
router.route("/product/create/").post(isAuthenticatedUser, authorizeRoles("admin"), createProduct)
|
router
|
||||||
router.route("/product/getAll/").get(getAllProduct)
|
.route("/product/create/")
|
||||||
router.route("/product/getOne/:id").get(getOneProduct)
|
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
|
||||||
router.route("/product/update/:id").put(isAuthenticatedUser, authorizeRoles("admin"), updateProduct);
|
router.route("/product/getAll/").get(getAllProduct);
|
||||||
router.route("/product/delete/:id").delete(isAuthenticatedUser, authorizeRoles("admin"), deleteProduct);
|
router.route("/product/getOne/:id").get(getOneProduct);
|
||||||
|
router
|
||||||
|
.route("/product/update/:id")
|
||||||
|
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateProduct);
|
||||||
|
router
|
||||||
|
.route("/product/delete/:id")
|
||||||
|
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteProduct);
|
||||||
|
router
|
||||||
|
.route("/product/deleteImage/jatinMor/product/:public_id")
|
||||||
|
.delete(
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
deleteImageFromCloudinary
|
||||||
|
);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user