-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (123 loc) · 3.82 KB
/
index.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
const express = require('express');
const bd = require("./config/bd");
const cors = require("cors");
const app = express();
const path = require("path");
const PORT = process.env.PORT || 3003;
app.use(cors());
app.use(express.json());
app.get("/api/getDatosMesas", async (req, res) => {
const sqlGetDatosMenu = await `select * from tb_mesa`
bd.query(sqlGetDatosMenu, (err, result)=>{
if(err){
console.log(err);
}
res.send(result);
})
})
app.get("/api/getDatosMenu", async (req, res) => {
const sqlGetDatosMenu = await `select * from tb_menu`
bd.query(sqlGetDatosMenu, (err, result)=>{
if(err){
console.log(err);
}
res.send(result);
})
})
app.get("/api/getOrdenes", (req, res) => {
const sqlGetOrdenes = `select o.id id_orden, men.nombre nom_menu, mes.nombre nom_mesa, o.cantidad
from tb_ordenes o
inner join tb_menu men on men.id = o.id_menu
inner join tb_mesa mes on mes.id = o.id_mesa
order by o.id`
bd.query(sqlGetOrdenes, (err, result)=>{
if(err){
console.log(err);
}
res.send(result);
})
})
app.post("/api/crearOrden", (req, res) => {
const mesa = req.body.mesa;
const menu = req.body.menu;
const cantidad = req.body.cantidad;
console.log("creando! "+mesa+" - "+menu);
const sqlCrearOrden = "insert into tb_ordenes (id_mesa, id_menu, cantidad) values (?,?,?)"
bd.query(sqlCrearOrden,
[mesa,menu,cantidad],
(err, result)=>{
if(err){
console.log(err);
}
console.log(result);
console.log("creada");
res.send(result)
})
})
app.post("/api/getEditarOrden", async (req, res) => {
const id = req.body.id;
console.log("editando! "+id);
const sqlBorrarOrden = await `select * from tb_ordenes where id = ${id}`
bd.query(sqlBorrarOrden,
(err, result)=>{
if(err){
console.log(err);
}
console.log(result);
console.log("orden editada");
res.send(result)
})
})
app.post("/api/editarOrden", (req, res) => {
const id = req.body.id;
const mesa = req.body.mesa
const menu = req.body.menu
const cantidad = req.body.cantidad;
var update_values = [];
if(mesa != ""){
update_values.push(`id_mesa=${mesa}`);
}
if(menu){
update_values.push(`id_menu=${menu}`);
}
if(cantidad != ""){
update_values.push(`cantidad="${cantidad}"`);
}
const sqlEditarOrden = `update tb_ordenes set ${update_values} where id = ${id}`
bd.query(sqlEditarOrden,
(err, result)=>{
if(err){
console.log(err);
}
console.log(result);
console.log("orden editada");
res.send(result)
})
})
app.post("/api/borrarOrden", (req, res) => {
const id = req.body.id;
console.log("borrando! "+id);
const sqlBorrarOrden = `delete from tb_ordenes where id = ${id}`
bd.query(sqlBorrarOrden,
(err, result)=>{
if(err){
console.log(err);
}
console.log(result);
console.log("orden borrada");
res.send(result)
})
})
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"))
app.get('*', function(req, res) {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'), err => {
if (err) {
res.status(500).send(err);
}
})
})
}
app.listen(PORT, () => {
console.log(`corriendo en ${PORT}`);
})