-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
197 lines (181 loc) · 5.39 KB
/
controller.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const UsersModel = require("./models/userModel");
const productModel = require("./models/productModel");
const cartModel = require("./models/cartModel");
const orderModel = require("./models/orderModel");
const bcrypt = require("bcrypt");
exports.addUser = async (req, res) => {
const { username, password, phoneNumber, email } = req.body;
const data = new UsersModel({
username,
password: await bcrypt.hash(password, 10),
phoneNumber,
email,
});
try {
const user = await UsersModel.findOne({ username });
if (user) {
res.status(400).json({
message: `User already Registered with username: ${data.username} Please Login or use different username`,
});
} else {
const dataToSave = await data.save();
dataToSave.message = `User Registration Successfull with Name:${dataToSave.username}`;
res.status(200).json(dataToSave);
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.getUser = async (req, res) => {
try {
const { username, password } = req.body;
const data = await UsersModel.findOne({ username });
if (data) {
const check = await bcrypt.compare(password, data.password);
if (!check) {
res
.status(400)
.json({ message: "Please Check your Password and try again !!!" });
} else {
res.status(200).json({ message: "Login Success" });
}
} else {
res.status(400).json({ message: "Fail Please check your Credentials" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.addProduct = async (req, res) => {
const data = new productModel({
productId: req.body.productId,
productName: req.body.productName,
productCode: req.body.productCode,
description: req.body.description,
price: req.body.price,
rating: req.body.rating,
manufacturer: req.body.manufacturer,
osType: req.body.osType,
});
try {
const dataToSave = await data.save();
res.status(200).json(dataToSave);
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.deleteProduct = async (req, res) => {
try {
const productId = req.params.productId;
const data = await productModel.findOneAndDelete({ productId: productId });
res.status(200).json(data);
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.getTablets = async (req, res) => {
try {
const data = await (
await productModel.find()
).filter((product) => {
return product.productCode.startsWith("TAB");
});
if (data && data.length) {
res.status(200).json(data);
} else {
res.status(400).json({ message: "No Tablets available" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.getMobiles = async (req, res) => {
try {
const data = await (
await productModel.find()
).filter((product) => {
return product.productCode.startsWith("MOB");
});
if (data && data.length) {
res.status(200).json(data);
} else {
res.status(400).json({ message: "No Mobiles available" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.addCart = async (req, res) => {
const data = new cartModel({
cartId: req.body.cartId,
userName: req.body.userName,
productsInCart: req.body.productsInCart,
statusOfCart: req.body.statusOfCart,
quantity: 1,
});
try {
const dataToSave = await data.save();
res.status(200).json(dataToSave);
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.getCart = async (req, res) => {
try {
const userName = req.params.userName;
const data = await cartModel.findOne({ userName });
if (data) {
res.status(200).json(data);
} else {
res.status(400).json({ message: "No cart under this username" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.putCart = async (req, res) => {
try {
const userName = req.params.userName;
const data = await cartModel.findOne({ userName });
if (data) {
const productsInCart = data.productsInCart;
const productInBody = req.body.productsInCart;
productInBody.forEach((ele) => {
const check_index = productsInCart.findIndex(
(item) => item.productId === ele.productId
);
if (check_index !== -1) {
productsInCart[check_index].quantity += ele.quantity;
} else {
productsInCart.push(ele);
}
});
data.productsInCart = productsInCart;
const updated = await cartModel.findOneAndUpdate({ userName }, data);
res.status(200).json(updated);
} else {
res.status(400).json({ message: "can not find cart with user" });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
exports.addOrder = async (req, res) => {
try {
const userName = req.params.userName;
const data = await cartModel.findOneAndUpdate(
{ userName },
{ statusOfCart: "close" }
);
const cartId = data.cartId;
const orderId = req.body.orderId;
const order = new orderModel({
orderId: orderId,
cartId: cartId,
});
const dataToSave = await order.save();
res.status(200).json(dataToSave);
} catch (error) {
res.status(400).json({ messaghe: error.message });
}
};