Created
August 31, 2022 16:22
-
-
Save mohrazzak/d546855b7097a41759f7b8062d755d23 to your computer and use it in GitHub Desktop.
Create product controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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