-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathproduct.js
69 lines (62 loc) · 1.55 KB
/
product.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const model = require('../model/product')
console.log(model.Product);
const Product = model.Product;
exports.createProduct = async (req, res) => {
try {
const product = new Product(req.body);
const dbResponse = await product.save();
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
}
exports.getAllProducts = async (req, res) => {
try {
const dbResponse = await Product.find();
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
};
exports.getProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findById(id);
res.status(201).json(dbResponse);
}
catch (error) {
res.status(201).json(error);
}
};
exports.replaceProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findOneAndReplace({ _id: id }, req.body, { new: true });
res.status(201).json(dbResponse);
}
catch (error) {
res.status(400).json(error);
}
};
exports.updateProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findOneAndUpdate({ _id: id }, req.body, { new: true });
res.status(201).json(dbResponse);
}
catch (error) {
res.status(400).json(error);
}
};
exports.deleteProduct = async (req, res) => {
try {
const id = req.params.id;
const dbResponse = await Product.findOneAndDelete({ _id: id });
res.status(201).json(dbResponse);
}
catch (error) {
res.status(400).json(error);
}
};