-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproductModel.js
114 lines (97 loc) · 3.62 KB
/
productModel.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
const connection = require('./dbConnection');
const jwtUtils = require("./jwtUtils");
async function viewProducts() {
try {
return new Promise((resolve, reject) => {
connection.query('SELECT * FROM Products', (error, results) => {
if (error) {
reject({ success: false, message: 'Error getting products' });
} else {
resolve({ success: true, message:results});
}
}
)
});
} catch (error) {
console.error('Error getting products:', error);
}
}
// async function changePassword(userId,password) {
// const hashedPassword = await bcrypt.hash(password, 10); // Hash password with bcrypt
// return new Promise((resolve, reject) => {
// connection.query('UPDATE user SET password = ? WHERE id = ?', [hashedPassword, userId], (error, results)=> {
// if (error) {
// console.error('Error creating user:', error);
// reject( { success: false, message: 'Error changing password' });
// } else {
// resolve({ success: true, message: 'Password changed successfully' });
// }
// });
// });
// }
async function addProduct(name,type,description,price,quantity) {
try {
return new Promise((resolve, reject) => {
connection.query('INSERT INTO Products (name,type,description,price,quantity) VALUES (?, ?, ?,?,?)', [name, type, description, price,quantity],(error, results) => {
if (error) {
reject({ success: false, message: 'Error adding products' });
} else {
resolve({ success: true, message:'Product added successfully'});
}
}
)
});
} catch (error) {
console.error('Error adding products:', error);
}
}
// Function to add a product
// async function addProduct(name,type,description,price,quantity) {
// return new Promise((resolve, reject) => {
// connection.query('INSERT INTO Products (name,type,description,price,quantity) VALUES (?, ?, ?,?)', [name, type, description, price,quantity], (error, results) => {
// if (error) {
// reject({ success: false, message: 'Error adding product' });
// } else {
// resolve({ success: true, message: 'Product added successfully' });
// }
// });
// });
// }
// Function to edit a product
async function editProduct(productId, name, price, description) {
try {
return new Promise((resolve, reject) => {
connection.query('UPDATE Products SET name = ?, price = ?, description = ? WHERE id = ?', [name, price, description, productId], (error, results) => {
if (error) {
reject({ success: false, message: 'Error editing product' });
} else {
resolve({ success: true, message: 'Product edited successfully' });
}
});
});
} catch (error) {
console.error('Error editing product:', error);
}
}
// Function to delete a product
async function deleteProduct(productId) {
try {
return new Promise((resolve, reject) => {
connection.query('DELETE FROM Products WHERE id = ?', [productId], (error, results) => {
if (error) {
reject({ success: false, message: 'Error deleting product' });
} else {
resolve({ success: true, message: 'Product deleted successfully' });
}
});
});
} catch (error) {
console.error('Error deleting product:', error);
}
}
module.exports ={
viewProducts,
addProduct,
editProduct,
deleteProduct
}