|
| 1 | +const createError = require('http-errors'); |
| 2 | +const mongoose = require('mongoose'); |
| 3 | + |
| 4 | +const Product = require('../Models/Product.model'); |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + getAllProducts: async (req, res, next) => { |
| 8 | + try { |
| 9 | + const results = await Product.find({}, { __v: 0 }); |
| 10 | + // const results = await Product.find({}, { name: 1, price: 1, _id: 0 }); |
| 11 | + // const results = await Product.find({ price: 699 }, {}); |
| 12 | + res.send(results); |
| 13 | + } catch (error) { |
| 14 | + console.log(error.message); |
| 15 | + } |
| 16 | + }, |
| 17 | + |
| 18 | + createNewProduct: async (req, res, next) => { |
| 19 | + try { |
| 20 | + const product = new Product(req.body); |
| 21 | + const result = await product.save(); |
| 22 | + res.send(result); |
| 23 | + } catch (error) { |
| 24 | + console.log(error.message); |
| 25 | + if (error.name === 'ValidationError') { |
| 26 | + next(createError(422, error.message)); |
| 27 | + return; |
| 28 | + } |
| 29 | + next(error); |
| 30 | + } |
| 31 | + |
| 32 | + /*Or: |
| 33 | + If you want to use the Promise based approach*/ |
| 34 | + /* |
| 35 | + const product = new Product({ |
| 36 | + name: req.body.name, |
| 37 | + price: req.body.price |
| 38 | + }); |
| 39 | + product |
| 40 | + .save() |
| 41 | + .then(result => { |
| 42 | + console.log(result); |
| 43 | + res.send(result); |
| 44 | + }) |
| 45 | + .catch(err => { |
| 46 | + console.log(err.message); |
| 47 | + }); |
| 48 | + */ |
| 49 | + }, |
| 50 | + |
| 51 | + findProductById: async (req, res, next) => { |
| 52 | + const id = req.params.id; |
| 53 | + try { |
| 54 | + const product = await Product.findById(id); |
| 55 | + // const product = await Product.findOne({ _id: id }); |
| 56 | + if (!product) { |
| 57 | + throw createError(404, 'Product does not exist.'); |
| 58 | + } |
| 59 | + res.send(product); |
| 60 | + } catch (error) { |
| 61 | + console.log(error.message); |
| 62 | + if (error instanceof mongoose.CastError) { |
| 63 | + next(createError(400, 'Invalid Product id')); |
| 64 | + return; |
| 65 | + } |
| 66 | + next(error); |
| 67 | + } |
| 68 | + }, |
| 69 | + |
| 70 | + updateAProduct: async (req, res, next) => { |
| 71 | + try { |
| 72 | + const id = req.params.id; |
| 73 | + const updates = req.body; |
| 74 | + const options = { new: true }; |
| 75 | + |
| 76 | + const result = await Product.findByIdAndUpdate(id, updates, options); |
| 77 | + if (!result) { |
| 78 | + throw createError(404, 'Product does not exist'); |
| 79 | + } |
| 80 | + res.send(result); |
| 81 | + } catch (error) { |
| 82 | + console.log(error.message); |
| 83 | + if (error instanceof mongoose.CastError) { |
| 84 | + return next(createError(400, 'Invalid Product Id')); |
| 85 | + } |
| 86 | + |
| 87 | + next(error); |
| 88 | + } |
| 89 | + }, |
| 90 | + |
| 91 | + deleteAProduct: async (req, res, next) => { |
| 92 | + const id = req.params.id; |
| 93 | + try { |
| 94 | + const result = await Product.findByIdAndDelete(id); |
| 95 | + // console.log(result); |
| 96 | + if (!result) { |
| 97 | + throw createError(404, 'Product does not exist.'); |
| 98 | + } |
| 99 | + res.send(result); |
| 100 | + } catch (error) { |
| 101 | + console.log(error.message); |
| 102 | + if (error instanceof mongoose.CastError) { |
| 103 | + next(createError(400, 'Invalid Product id')); |
| 104 | + return; |
| 105 | + } |
| 106 | + next(error); |
| 107 | + } |
| 108 | + } |
| 109 | +}; |
0 commit comments