-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
186 lines (161 loc) · 6.03 KB
/
app.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const express = require('express');
const { pool, getProductoras, getFloristerias, getCatalogoProductoraById, getDetalleFlores,getFloresValoraciones,getInformacionFlor,getFacturas, getFlorCortes, getContratosProductora } = require('./db');
const path = require('path');
const app = express();
// Configurar el motor de plantillas EJS (si es necesario)
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'view'));
// Servir archivos estáticos desde la carpeta 'view'
app.use(express.static(path.join(__dirname, 'view')));
app.get('/test', async (req, res) => {
try {
const result = await pool.query('SELECT NOW()');
res.json(result.rows);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).send('Error querying the database');
}
});
// Ruta para servir el archivo HTML principal
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'view', 'index.html'));
});
// Nueva ruta para obtener datos de las productoras
app.get('/api/productoras', async (req, res) => {
try {
const productoras = await getProductoras();
res.json(productoras);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/catalogoProductor/:id', async (req, res) => {
const { id } = req.params;
try {
const catalogoProductor = await getCatalogoProductoraById(id);
res.json(catalogoProductor);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/detalleFlores/:florId/:productorId', async (req, res) => {
const { florId,productorId } = req.params;
try {
const detalleFlores = await getDetalleFlores(florId,productorId);
res.json(detalleFlores);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
// Nueva ruta para obtener datos de las floristerias
app.get('/api/floristerias', async (req, res) => {
try {
const floristerias = await getFloristerias();
res.json(floristerias);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/floresValoraciones/:idFloristeria', async (req, res) => {
const { idFloristeria } = req.params;
try {
console.log(`Fetching flores con valoraciones for floristeria ID: ${idFloristeria}`);
const flores = await getFloresValoraciones(idFloristeria);
res.json(flores);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/informacionFlor/:idFloristeria/:idFlor', async (req, res) => {
const { idFloristeria, idFlor } = req.params;
try {
const informacionFlor = await getInformacionFlor(idFloristeria, idFlor);
res.json(informacionFlor);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/facturas', async (req, res) => {
try {
const facturas = await getFacturas();
res.json(facturas);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/florCortes', async (req, res) => {
try {
console.log('Received request to get flor cortes');
const florCortes = await getFlorCortes();
res.json(florCortes);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/coloresDeCorte/:corteId', async (req, res) => {
const { corteId } = req.params;
try {
const result = await pool.query('SELECT colores FROM FLOR_CORTES WHERE corteId = $1', [corteId]);
if (result.rows.length === 0) {
return res.status(404).json({ error: 'Corte no encontrado' });
}
const colores = result.rows[0].colores.split(',').map(color => color.trim());
res.json(colores);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/coloresDeCortePorNombre/:nombre', async (req, res) => {
const { nombre } = req.params;
try {
const result = await pool.query('SELECT colores FROM FLOR_CORTES WHERE LOWER(nombrecomun) = LOWER($1)', [nombre]);
if (result.rows.length === 0) {
return res.status(404).json({ error: 'Corte no encontrado' });
}
const colores = result.rows[0].colores.split(',').map(color => color.trim());
res.json(colores);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/obtener-contratos-productora', async (req, res) => {
const { productoraId } = req.query;
if (!productoraId) {
return res.status(400).json({ error: 'productoraId is required' });
}
try {
const contratos = await getContratosProductora(productoraId);
res.json(contratos);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
app.get('/api/informacionFactura/:facturaId', async (req, res) => {
const { facturaId } = req.params;
try {
const result = await pool.query('SELECT * FROM obtener_informacion_factura($1)', [facturaId]);
const facturaInfo = result.rows[0];
const lotesResult = await pool.query('SELECT * FROM traer_lotes($1)', [facturaId]);
facturaInfo.lotes = lotesResult.rows;
res.json(facturaInfo);
} catch (err) {
console.error('Error querying the database:', err);
res.status(500).json({ error: 'Error querying the database' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Click here to open: http://localhost:${PORT}`);
});