product apis updated

This commit is contained in:
print-signs 2023-10-17 16:47:24 +05:30
parent d3312c3fec
commit 07fb71f117
2 changed files with 187 additions and 216 deletions

View File

@ -1,224 +1,195 @@
import { Product } from "./ProductModel.js"; import { Product } from "./ProductModel.js";
import cloudinary from "../../Utils/cloudinary.js"; import cloudinary from "../../Utils/cloudinary.js";
export const createProduct = async (req, res) => { export const createProduct = async (req, res) => {
try {
if (!req.files) {
return res.status(400).json({
msg: " PLease Provide Product image",
});
}
let images = [];
let Allfiles = req.files.image;
if (typeof Allfiles.tempFilePath === "string") {
let filepath = Allfiles.tempFilePath;
try { images.push(filepath);
if (!req.files) { } else {
return res.status(400).json({ Allfiles.map((item) => {
images.push(item.tempFilePath);
msg: " PLease Provide Product image", });
});
}
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 = [];
for (let i = 0; i < images.length; i++) {
const result = await cloudinary.v2.uploader.upload(images[i], {
folder: "jatinMor/product",
});
imagesLinks.push({
public_id: result.public_id,
url: result.secure_url,
});
}
req.body.image = imagesLinks;
req.body.addedBy = req.user.id;
const data = await Product.create({ ...req.body });
res.status(201).json({
success: true,
data,
msg: " create Product Successfully!!",
});
} catch (error) {
// console.log(error)
res.status(500).json({
success: false,
msg: error.message
});
} }
const imagesLinks = [];
for (let i = 0; i < images.length; i++) {
const result = await cloudinary.v2.uploader.upload(images[i], {
folder: "jatinMor/product",
});
imagesLinks.push({
public_id: result.public_id,
url: result.secure_url,
});
}
req.body.image = imagesLinks;
req.body.addedBy = req.user.id;
const data = await Product.create({ ...req.body });
res.status(201).json({
success: true,
data,
msg: " create Product Successfully!!",
});
} catch (error) {
// console.log(error)
res.status(500).json({
success: false,
msg: error.message,
});
}
}; };
//get All Product //get All Product
export const getAllProduct = async (req, res) => { export const getAllProduct = async (req, res) => {
try {
try { const product = await Product.find().sort({ createdAt: -1 });
const product = await Product.find().sort({ createdAt: -1 }); if (product) {
if (product) { return res.status(200).json({
return res.status(200).json({ success: true,
success: true, product,
product, });
});
}
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!"
});
} }
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
}; };
//get One Product //get One Product
export const getOneProduct = async (req, res) => { export const getOneProduct = async (req, res) => {
try {
try { const product = await Product.findById(req.params.id);
const product = await Product.findById(req.params.id); if (product) {
if (product) { return res.status(200).json({
return res.status(200).json({ success: true,
success: true, product,
product, });
});
}
} catch (error) {
// console.log(error)
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!"
});
} }
} catch (error) {
// console.log(error)
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
}; };
// 3.update Product // 3.update Product
export const updateProduct = async (req, res) => { export const updateProduct = async (req, res) => {
try { try {
// const newProductData = { // const newProductData = {
// name: req.body.name, // name: req.body.name,
// description: req.body.description, // description: req.body.description,
// price: req.body.base_Price, // price: req.body.base_Price,
// } // }
if (req.files) { if (req.files) {
// req.body.addedBy = req.user.id;
// const image_file = req.files.image;
const getProduct = await Product.findById(req.params.id);
if (getProduct) {
// req.body.addedBy = req.user.id; // Deleting Images From Cloudinary
// const image_file = req.files.image; for (let i = 0; i < getProduct.image.length; i++) {
const getProduct = await Product.findById(req.params.id); await cloudinary.v2.uploader.destroy(getProduct.image[i].public_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 = [];
for (let i = 0; i < images.length; i++) {
const result = await cloudinary.v2.uploader.upload(images[i], {
folder: "jatinMor/product",
});
imagesLinks.push({
public_id: result.public_id,
url: result.secure_url,
});
}
req.body.image = imagesLinks;
} }
}
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 ModifyProduct = await Product.findByIdAndUpdate(req.params.id, req.body, const imagesLinks = [];
for (let i = 0; i < images.length; i++) {
{ new: true } const result = await cloudinary.v2.uploader.upload(images[i], {
// runValidators: true, folder: "jatinMor/product",
// useFindAndModify: false,
);
res.status(200).json({
success: true,
ModifyProduct
}); });
} catch (error) { imagesLinks.push({
// console.log(error) public_id: result.public_id,
res.status(500).json({ url: result.secure_url,
success: false,
msg: error.message ? error.message : "Something went wrong!",
}); });
}
req.body.image = imagesLinks;
} }
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) {
// console.log(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 { if (!req.params.id) {
return res.status(400).json({
if (!req.params.id) { success: false,
return res.status(400).json({ msg: "Please Provide Product ID!",
success: false, });
msg: "Please Provide Product ID!" }
}); const getProduct = await Product.findById(req.params.id);
} if (!getProduct) {
const getProduct = await Product.findById(req.params.id); return res.status(404).json({
if (!getProduct) { success: false,
return res.status(404).json({ msg: "Product not Found!",
success: false, });
msg: "Product not Found!" }
}); // Deleting Images From Cloudinary
for (let i = 0; i < getProduct.image.length; i++) {
} await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
// Deleting Images From Cloudinary
for (let i = 0; i < getProduct.image.length; i++) {
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
}
//-------------------------//
const product = await Product.findByIdAndDelete(req.params.id)
if (!product) {
return res.status(404).json({ message: 'Product Not Found' });
}
await product.remove();
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!"
});
} }
//-------------------------//
const product = await Product.findByIdAndDelete(req.params.id);
if (!product) {
return res.status(404).json({ message: "Product Not Found" });
}
await product.remove();
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!",
});
}
}; };

View File

@ -1,46 +1,46 @@
import mongoose from "mongoose"; import mongoose from "mongoose";
const { Schema, model } = mongoose; const { Schema, model } = mongoose;
const productSchema = new Schema({ const productSchema = new Schema(
{
name: { name: {
type: String, type: String,
maxLength: [25, "name cannot exceed 25 characters"], maxLength: [25, "name cannot exceed 25 characters"],
required: [true, "Please Enter product Name"], required: [true, "Please Enter product Name"],
trim: true, trim: true,
}, },
description: { description: {
type: String, type: String,
maxLength: [100, "description cannot exceed 100 characters"], maxLength: [100, "description cannot exceed 100 characters"],
required: [true, "Please Enter product Description"], required: [true, "Please Enter product Description"],
}, },
price: { price: {
type: Number, type: Number,
required: [true, "Please Enter product Price"], required: [true, "Please Enter product Price"],
maxLength: [8, "Price cannot exceed 8 characters"], maxLength: [8, "Price cannot exceed 8 characters"],
},
category: {
type: String,
}, },
image: [ image: [
{ {
public_id: { public_id: {
type: String, type: String,
required: true, required: true,
},
url: {
type: String,
required: true,
},
}, },
url: {
type: String,
required: true,
},
},
], ],
addedBy: { addedBy: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'User' ref: "User",
} },
},
{ timestamps: true }
);
}, { timestamps: true });
export const Product = model("Product", productSchema); export const Product = model("Product", productSchema);