|
| 1 | +const sqlRequestProduct = require("../sql/sqlProduct"); |
| 2 | + |
| 3 | +const auth = require("../middleware/auth"); |
| 4 | + |
| 5 | +const appRouterProduct = async function(app, connection) { |
| 6 | + // GET /products/ ⇒ Return the list of registered products (return only Names and Ids, Prices) |
| 7 | + await app.get("/products/", function(req, res) { |
| 8 | + // let getProductsInfo = "SELECT * FROM products"; |
| 9 | + connection.query(sqlRequestProduct.getProductsInfo, function(err, results) { |
| 10 | + results.forEach((element) => { |
| 11 | + element.url = element.url.split(","); |
| 12 | + }); |
| 13 | + if (err) throw err; |
| 14 | + res.send(results); |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + // POST /products/ ⇒ Will add a product in the Products table (only if the user who create the product has a good JWT...) |
| 19 | + await app.post("/products/", auth, function(req, res) { |
| 20 | + let category = req.body.category; |
| 21 | + let prices = req.body.prices; |
| 22 | + let name = req.body.name; |
| 23 | + let description = req.body.description; |
| 24 | + let url = ""; |
| 25 | + let id_user_affiliate = req.body.id_user_affiliate; |
| 26 | + |
| 27 | + for (let i = 0; i < req.body.url.length; i++) { |
| 28 | + if (i === 0) { |
| 29 | + url = req.body.url[i]; |
| 30 | + } else { |
| 31 | + url = url + "," + req.body.url[i]; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + const productObject = { |
| 36 | + category: category, |
| 37 | + name: name, |
| 38 | + description: description, |
| 39 | + url: url, |
| 40 | + prices: prices, |
| 41 | + id_user_affiliate: id_user_affiliate, |
| 42 | + }; |
| 43 | + |
| 44 | + connection.query(sqlRequestProduct.postProduct, productObject, function( |
| 45 | + err, |
| 46 | + results |
| 47 | + ) { |
| 48 | + if (err) throw err; |
| 49 | + res.send(results); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + //GET /products/:id ⇒ Return all the datas of this specific Product |
| 54 | + //(including the name of the user who created it, the category, the description etc...) |
| 55 | + await app.get("/products/:id", function(req, res) { |
| 56 | + let id = req.params.id; |
| 57 | + connection.query(sqlRequestProduct.productInfo(id), function(err, results) { |
| 58 | + results.forEach((element) => { |
| 59 | + element.url = element.url.split(","); |
| 60 | + }); |
| 61 | + if (err) throw err; |
| 62 | + res.send(results); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + await app.get("/productid/:id", function(req, res) { |
| 67 | + let id = req.params.id; |
| 68 | + connection.query( |
| 69 | + sqlRequestProduct.poductInfoWithIdUserAffiliate(id), |
| 70 | + function(err, results) { |
| 71 | + if (err) throw err; |
| 72 | + res.send(results); |
| 73 | + } |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + // POST /product/:id => Delete this specific product from the database |
| 78 | + await app.post("/product/:id", auth, (req, res) => { |
| 79 | + connection.query(sqlRequestProduct.deleteProduct(req.params.id), (err) => { |
| 80 | + if (err) { |
| 81 | + console.log(err); |
| 82 | + res.sendStatus(500); |
| 83 | + } else res.send("Deleted"); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + // POST /productEdit/:id => Update this specific product from the database |
| 88 | + await app.post("/productEdit/:id", auth, (req, res) => { |
| 89 | + req.body.id = req.params.id; |
| 90 | + if (req.body.idUser === req.body.id_user_affiliate) { |
| 91 | + connection.query(sqlRequestProduct.editProduct(req.body), (err) => { |
| 92 | + if (err) { |
| 93 | + console.log(err); |
| 94 | + res.sendStatus(500); |
| 95 | + } else res.send("Updated"); |
| 96 | + }); |
| 97 | + } |
| 98 | + }); |
| 99 | +}; |
| 100 | +module.exports = appRouterProduct; |
0 commit comments