-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.js
153 lines (141 loc) · 4.37 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
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
const dotenv = require('dotenv');
dotenv.config();
const express = require('express')
const app = express()
const port = process.env.PORT_PRODUCT || 5000
const jwt = require("jsonwebtoken");
const PASSPHRASE = process.env.PASSPHRASE;
const cors = require('cors')
const corsOption = {
origin: '*'
}
app.use(cors(corsOption))
const { executeRequest } = require('./database/database_utils')
app.get('/', (req, res) => {
res.send('YA RIEN ICI');
})
/***** PRODUCT *****/
app.get('/api/products', (req, res) => {
executeRequest('SELECT * FROM product', (err, rows) => {
if(err != null) {
console.log(err);
}
res.json(rows);
});
})
app.get('/api/products/filterCategories/:idCategorie', (req, res) => {
executeRequest(`SELECT * FROM product WHERE id_categorie = ${req.params.idCategorie}`, (err, rows) => {
if(err != null) {
console.log(err);
}
res.json(rows);
});
})
app.get('/api/products/filterName/:name', (req, res) => {
executeRequest(`SELECT * FROM product WHERE name LIKE '%${req.params.name}%'`, (err, rows) => {
if(err != null) {
console.log(err);
}
res.json(rows);
});
})
app.get('/api/products/filterPrice/:price', (req, res) => {
executeRequest(`SELECT * FROM product WHERE price <= ${req.params.price}`, (err, rows) => {
if(err != null) {
console.log(err);
}
res.json(rows);
});
})
app.get('/api/product/:id', (req, res) => {
let id = req.params.id;
if (id == null) {
res.json({
message: "id is null"
})
return;
}
let sql = `SELECT * FROM product WHERE id = ${id}`;
executeRequest(sql, (err, rows) => {
if (err) {
res.json({
message: err
})
} else {
res.json(rows[0])
}
});
})
app.post('/api/addProduct', (req, res) => {
let name = req.query.name;
let price = parseFloat(req.query.price);
let description = req.query.feature;
let id_categorie = req.query.id_categorie;
let image = req.query.image;
let quantity = req.query.quantity
//test name,price,description,id_categorie are not null
if (name == null || price == null || description == null || id_categorie == null || image == null || quantity == null) {
res.json({
message: "name,price,description,id_categorie,quantity are null"
})
return;
}
let sql = `INSERT INTO product (name, price, quantity, feature, image, id_categorie) VALUES ('${name}', '${price}', '${quantity}', '${description}', '${image}', ${id_categorie})`;
executeRequest(sql, (err, rows) => {
if (err) {
res.json({
message: err
})
} else {
res.json({ message: "success" })
}
});
})
app.put('/api/product/:id', (req, res) => {
let id = req.params.id;
let name = req.query.name;
let price = parseFloat(req.query.price);
let description = req.query.feature;
let quantity = parseFloat(req.query.quantity);
let id_categorie = req.query.id_categorie;
let image = req.query.image;
//test name,price,description,id_categorie are not null
if (name == null || price == null || description == null || id_categorie == null || image == null || quantity == null) {
res.json({
message: "name,price,description,id_categorie are null"
})
return;
}
let sql = `UPDATE product SET name = '${name}', price = '${price}', quantity = '${quantity}', feature = '${description}', image = '${image}', id_categorie = ${id_categorie} WHERE id = ${id}`;
executeRequest(sql, (err, rows) => {
if (err) {
res.json({
message: err
})
} else {
res.json({ message: "success" })
}
});
})
app.delete('/api/product/:id', (req, res) => {
let id = req.params.id;
if (id == null) {
res.json({
message: "id is null"
})
return;
}
let sql = `DELETE FROM product WHERE id = ${id}`;
executeRequest(sql, (err, rows) => {
if (err) {
res.json({
message: err
})
} else {
res.json({ message: "success" })
}
});
})
app.listen(port, () => {
console.log(`Adresse du serveur : http://localhost:${port}`)
})