Skip to content

Instantly share code, notes, and snippets.

@mohrazzak
Created August 31, 2022 16:22
Show Gist options
  • Save mohrazzak/d546855b7097a41759f7b8062d755d23 to your computer and use it in GitHub Desktop.
Save mohrazzak/d546855b7097a41759f7b8062d755d23 to your computer and use it in GitHub Desktop.
Create product controller
exports.createProduct = async (req, res, next) => {
try {
if (!req.file) {
const error = new Error("No image provided.");
error.statusCode = 422;
return next(error);
}
const image = req.file.path.replace("\\", "/").replace("\\", "/");
const { title, price, category, desc } = req.body;
const product = new Product({
title: title,
price: price,
image: image,
category: category,
desc: desc,
});
await product.save();
res
.status(200)
.json({ message: `Product created succesfully!.`, product: product });
} catch (err) {
const error = new Error(`Error creating Product!.`);
error.statusCode = 500;
deleteHelper(req.file.path.replace("\\", "/").replace("\\", "/"));
return next(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment