-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_options.js
156 lines (137 loc) · 5.42 KB
/
db_options.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
import express from "express"
import mysql from "mysql2"
import cors from "cors"
const app_express = express()
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "1234",
database: "createb"
})
db.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
app_express.use(express.json())
app_express.use(cors())
app_express.get ("/options/bread", (req, res) => {
const q = "SELECT id, bread, bread_price FROM bread"
db.query(q,(error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.get ("/options/meat", (req, res) => {
const q = "SELECT id, meat, meat_price FROM meat"
db.query(q,(error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.get ("/options/meat_state", (req, res) => {
const q = "SELECT id, meat_state, meat_state_price FROM meat_state"
db.query(q,(error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.get ("/options/salads", (req, res) => {
const q = "SELECT id, salads, salads_price FROM salads"
db.query(q,(error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.get ("/options/cheese", (req, res) => {
const q = "SELECT id, cheese, cheese_price FROM cheese"
db.query(q,(error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.post("/register", (req, res) => {
const q = "INSERT INTO login(`username`,`password`) VALUES (?)"
const values = [req.body.username, req.body.password]
db.query(q, [values], (error,data) => {
if(error) {
return res.json("A inserção de elementos deu o seguinte erro: "+error)
}
return res.json(data)
})
})
app_express.get ("/login", (req, res) => {
const q = "SELECT username, password, idLogin FROM login"
db.query(q,(error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.get ("/ordersID", (req, res) => {
const q = "SELECT MAX(id) FROM orders"
db.query(q, (error,data)=> {
if(error) {
return res.json("A consulta deu o seguinte erro: "+error)
}
return res.json(data)
})
});
app_express.get ("/stackPerOrder/:idLogged", (req, res) => {
const idLogged = req.params.idLogged
const q = "select orders.id, orders.bread, orders.meat, orders.meat_state, orders.salads, orders.cheese, orders.name, orders.image, orders.price, orders.quantity, orders.OrderStack, login.idLogin, customerinformation.responsible, customerinformation.cpf, customerinformation.localization, customerinformation.phoneNumber, customerinformation.observations, customerinformation.paymentForm from orders INNER JOIN stack ON orders.OrderStack = stack.idStack INNER JOIN login ON stack.idLogin = login.idLogin INNER JOIN customerinformation ON stack.idCustomInformation = customerinformation.idCustomerInformation WHERE login.idLogin = ?";
db.query(q, [idLogged], (error, data) => {
if (error) {
return res.json("A consulta deu o seguinte erro: " + error);
}
return res.json(data);
});
});
app_express.post("/addstack", (req, res) => {
const q = "INSERT INTO stack(`idLogin`,`idCustomInformation`) VALUES (?)"
const values = [req.body.idLogin,req.body.idCustomInformation]
db.query(q, [values], (error,data) => {
if(error) {
return res.json("A inserção de elementos deu o seguinte erro: "+error)
}
console.log(data)
return res.json(data)
})
})
app_express.post("/customerinformation", (req, res) => {
const q = "INSERT INTO customerinformation(`responsible`,`cpf`,`localization`,`phoneNumber`,`observations`,`paymentForm`) VALUES (?)"
const values = [req.body.responsible,req.body.cpf, req.body.localization,req.body.phoneNumber,req.body.observations,req.body.paymentForm]
db.query(q, [values], (error,data) => {
if(error) {
return res.json("A inserção de elementos deu o seguinte erro: "+error)
}
console.log(data)
return res.json(data)
})
})
app_express.post("/orders", (req, res) => {
const q = "INSERT INTO orders(`bread`,`meat`,`meat_state`,`salads`,`cheese`,`name`,`image`,`price`, `quantity`,`OrderStack`) VALUES (?)"
const values = [req.body.bread,req.body.meat,req.body.meat_state,req.body.salads,req.body.cheese,req.body.name,req.body.image,req.body.price,req.body.quantity,req.body.OrderStack]
db.query(q, [values], (error,data) => {
if(error) {
return res.json("A inserção de elementos deu o seguinte erro: "+error)
}
console.log(data)
return res.json(data)
})
})
app_express.listen(8800, ()=> {
console.log("Coneccted with backend!!")
} )