|
| 1 | +const asyncHandler = require("express-async-handler"); |
| 2 | +const Product = require("../models/productModel"); |
| 3 | +const { fileSizeFormatter } = require("../utils/fileUpload"); |
| 4 | +const cloudinary = require("cloudinary").v2; |
| 5 | + |
| 6 | +const createProduct = asyncHandler(async (req, res) => { |
| 7 | + const { name, sku, category, quantity, price, description } = req.body; |
| 8 | + |
| 9 | + // Validation |
| 10 | + if (!name || !category || !quantity || !price || !description) { |
| 11 | + res.status(400); |
| 12 | + throw new Error("Please fill in all fields"); |
| 13 | + } |
| 14 | + |
| 15 | + // Handle Image Upload |
| 16 | + let fileData = {}; |
| 17 | + if (req.file) { |
| 18 | + // Save image to cloudinary |
| 19 | + let uploadedFile; |
| 20 | + try { |
| 21 | + uploadedFile = await cloudinary.uploader.upload(req.file.path, { |
| 22 | + folder: "Pinvent App", |
| 23 | + resource_type: "image", |
| 24 | + }); |
| 25 | + } catch (error) { |
| 26 | + res.status(500); |
| 27 | + throw new Error("Image could not be uploaded "); |
| 28 | + } |
| 29 | + fileData = { |
| 30 | + fileName: req.file.originalname, |
| 31 | + filePath: uploadedFile.secure_url, |
| 32 | + fileType: req.file.mimetype, |
| 33 | + fileSize: fileSizeFormatter(req.file.size, 2), |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + // Create Product |
| 38 | + const product = await Product.create({ |
| 39 | + user: req.user.id, |
| 40 | + name, |
| 41 | + sku, |
| 42 | + category, |
| 43 | + quantity, |
| 44 | + price, |
| 45 | + description, |
| 46 | + image: fileData, |
| 47 | + }); |
| 48 | + res.status(201).json(product); |
| 49 | +}); |
| 50 | + |
| 51 | +// Get all Products |
| 52 | +const getProducts = asyncHandler(async (req, res) => { |
| 53 | + const products = await Product.find({ user: req.user.id }).sort("-createdAt"); |
| 54 | + res.status(200).json(products); |
| 55 | +}); |
| 56 | + |
| 57 | +// Get single product |
| 58 | +const getProduct = asyncHandler(async (req, res) => { |
| 59 | + const product = await Product.findById(req.params.id); |
| 60 | + |
| 61 | + // if product doesnt exists |
| 62 | + if (!product) { |
| 63 | + res.status(404); |
| 64 | + throw new Error("Product not found "); |
| 65 | + } |
| 66 | + //Match product to its user |
| 67 | + if (product.user.toString() !== req.user.id) { |
| 68 | + res.status(404); |
| 69 | + throw new Error("User not authorized"); |
| 70 | + } |
| 71 | + res.status(200).json(product); |
| 72 | +}); |
| 73 | + |
| 74 | +// Delete Product |
| 75 | +const deleteProduct = asyncHandler(async (req, res) => { |
| 76 | + const product = await Product.findById(req.params.id); |
| 77 | + |
| 78 | + // if product doesnt exists |
| 79 | + if (!product) { |
| 80 | + res.status(404); |
| 81 | + throw new Error("Product not found "); |
| 82 | + } |
| 83 | + //Match product to its user |
| 84 | + if (product.user.toString() !== req.user.id) { |
| 85 | + res.status(404); |
| 86 | + throw new Error("User not authorized"); |
| 87 | + } |
| 88 | + await product.remove(); |
| 89 | + res.status(200).json({ message: "Product deleted." }); |
| 90 | +}); |
| 91 | + |
| 92 | +// Update product |
| 93 | + |
| 94 | +const updateProduct = asyncHandler(async (req, res) => { |
| 95 | + const { name, category, quantity, price, description } = req.body; |
| 96 | + const { id } = req.params; |
| 97 | + |
| 98 | + const product = await Product.findById(id); |
| 99 | + |
| 100 | + // if product doesnt exists |
| 101 | + if (!product) { |
| 102 | + res.status(404); |
| 103 | + throw new Error("Product not found "); |
| 104 | + } |
| 105 | + //Match product to its user |
| 106 | + if (product.user.toString() !== req.user.id) { |
| 107 | + res.status(404); |
| 108 | + throw new Error("User not authorized"); |
| 109 | + } |
| 110 | + |
| 111 | + // Handle Image Upload |
| 112 | + let fileData = {}; |
| 113 | + if (req.file) { |
| 114 | + // Save image to cloudinary |
| 115 | + let uploadedFile; |
| 116 | + try { |
| 117 | + uploadedFile = await cloudinary.uploader.upload(req.file.path, { |
| 118 | + folder: "Pinvent App", |
| 119 | + resource_type: "image", |
| 120 | + }); |
| 121 | + } catch (error) { |
| 122 | + res.status(500); |
| 123 | + throw new Error("Image could not be uploaded "); |
| 124 | + } |
| 125 | + fileData = { |
| 126 | + fileName: req.file.originalname, |
| 127 | + filePath: uploadedFile.secure_url, |
| 128 | + fileType: req.file.mimetype, |
| 129 | + fileSize: fileSizeFormatter(req.file.size, 2), |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + // Update Product |
| 134 | + |
| 135 | + const updatedProduct = await Product.findByIdAndUpdate( |
| 136 | + { _id: id }, |
| 137 | + { |
| 138 | + name, |
| 139 | + category, |
| 140 | + quantity, |
| 141 | + price, |
| 142 | + description, |
| 143 | + image: Object.keys(fileData).length === 0 ? product?.image : fileData, |
| 144 | + }, |
| 145 | + { |
| 146 | + new: true, |
| 147 | + runValidators: true, |
| 148 | + } |
| 149 | + ); |
| 150 | + |
| 151 | + res.status(200).json(updatedProduct); |
| 152 | +}); |
| 153 | + |
| 154 | +module.exports = { |
| 155 | + createProduct, |
| 156 | + getProducts, |
| 157 | + getProduct, |
| 158 | + deleteProduct, |
| 159 | + updateProduct, |
| 160 | +}; |
0 commit comments