product apis updated
This commit is contained in:
parent
d3312c3fec
commit
07fb71f117
@ -1,17 +1,11 @@
|
|||||||
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 {
|
try {
|
||||||
if (!req.files) {
|
if (!req.files) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
|
|
||||||
msg: " PLease Provide Product image",
|
msg: " PLease Provide Product image",
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let images = [];
|
let images = [];
|
||||||
@ -19,11 +13,11 @@ export const createProduct = async (req, res) => {
|
|||||||
if (typeof Allfiles.tempFilePath === "string") {
|
if (typeof Allfiles.tempFilePath === "string") {
|
||||||
let filepath = Allfiles.tempFilePath;
|
let filepath = Allfiles.tempFilePath;
|
||||||
|
|
||||||
images.push(filepath)
|
images.push(filepath);
|
||||||
} else {
|
} else {
|
||||||
Allfiles.map(item => {
|
Allfiles.map((item) => {
|
||||||
images.push(item.tempFilePath);
|
images.push(item.tempFilePath);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const imagesLinks = [];
|
const imagesLinks = [];
|
||||||
@ -38,7 +32,6 @@ 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;
|
||||||
|
|
||||||
@ -47,20 +40,17 @@ export const createProduct = async (req, res) => {
|
|||||||
success: true,
|
success: true,
|
||||||
data,
|
data,
|
||||||
msg: " create Product Successfully!!",
|
msg: " create Product Successfully!!",
|
||||||
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(error)
|
// console.log(error)
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: error.message
|
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) {
|
||||||
@ -69,19 +59,15 @@ export const getAllProduct = async (req, res) => {
|
|||||||
product,
|
product,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (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!",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
//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) {
|
||||||
@ -94,10 +80,9 @@ export const getOneProduct = async (req, res) => {
|
|||||||
// console.log(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!",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 3.update Product
|
// 3.update Product
|
||||||
@ -111,13 +96,10 @@ export const updateProduct = async (req, res) => {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
if (req.files) {
|
if (req.files) {
|
||||||
|
|
||||||
|
|
||||||
// req.body.addedBy = req.user.id;
|
// req.body.addedBy = req.user.id;
|
||||||
// const image_file = req.files.image;
|
// const image_file = req.files.image;
|
||||||
const getProduct = await Product.findById(req.params.id);
|
const getProduct = await Product.findById(req.params.id);
|
||||||
|
|
||||||
|
|
||||||
if (getProduct) {
|
if (getProduct) {
|
||||||
// Deleting Images From Cloudinary
|
// Deleting Images From Cloudinary
|
||||||
for (let i = 0; i < getProduct.image.length; i++) {
|
for (let i = 0; i < getProduct.image.length; i++) {
|
||||||
@ -129,11 +111,11 @@ export const updateProduct = async (req, res) => {
|
|||||||
if (typeof Allfiles.tempFilePath === "string") {
|
if (typeof Allfiles.tempFilePath === "string") {
|
||||||
let filepath = Allfiles.tempFilePath;
|
let filepath = Allfiles.tempFilePath;
|
||||||
|
|
||||||
images.push(filepath)
|
images.push(filepath);
|
||||||
} else {
|
} else {
|
||||||
Allfiles.map(item => {
|
Allfiles.map((item) => {
|
||||||
images.push(item.tempFilePath);
|
images.push(item.tempFilePath);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const imagesLinks = [];
|
const imagesLinks = [];
|
||||||
@ -148,12 +130,12 @@ export const updateProduct = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
req.body.image = imagesLinks;
|
req.body.image = imagesLinks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ModifyProduct = await Product.findByIdAndUpdate(
|
||||||
const ModifyProduct = await Product.findByIdAndUpdate(req.params.id, req.body,
|
req.params.id,
|
||||||
|
req.body,
|
||||||
|
|
||||||
{ new: true }
|
{ new: true }
|
||||||
// runValidators: true,
|
// runValidators: true,
|
||||||
@ -162,63 +144,52 @@ export const updateProduct = async (req, res) => {
|
|||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
ModifyProduct
|
ModifyProduct,
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(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!",
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//delete one Product
|
//delete one Product
|
||||||
export const deleteProduct = async (req, res) => {
|
export const deleteProduct = async (req, res) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (!req.params.id) {
|
if (!req.params.id) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: "Please Provide Product ID!"
|
msg: "Please Provide Product ID!",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const getProduct = await Product.findById(req.params.id);
|
const getProduct = await Product.findById(req.params.id);
|
||||||
if (!getProduct) {
|
if (!getProduct) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
msg: "Product not Found!"
|
msg: "Product not Found!",
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
// Deleting Images From Cloudinary
|
// Deleting Images From Cloudinary
|
||||||
for (let i = 0; i < getProduct.image.length; i++) {
|
for (let i = 0; i < getProduct.image.length; i++) {
|
||||||
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
|
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------//
|
//-------------------------//
|
||||||
const product = await Product.findByIdAndDelete(req.params.id)
|
const product = await Product.findByIdAndDelete(req.params.id);
|
||||||
if (!product) {
|
if (!product) {
|
||||||
return res.status(404).json({ message: 'Product Not Found' });
|
return res.status(404).json({ message: "Product Not Found" });
|
||||||
}
|
}
|
||||||
await product.remove();
|
await product.remove();
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Product Deleted Successfully!!",
|
msg: "Product Deleted Successfully!!",
|
||||||
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (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!",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
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"],
|
||||||
@ -18,8 +19,9 @@ const productSchema = new Schema({
|
|||||||
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: [
|
||||||
{
|
{
|
||||||
@ -35,12 +37,10 @@ const productSchema = new Schema({
|
|||||||
],
|
],
|
||||||
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user