forked from Adalab/modulo-4-evaluacion-final-Storrecu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (129 loc) · 3.33 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
144
145
146
147
const express = require('express');
const cors = require('cors');
const mysql = require('mysql2/promise');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
require('dotenv').config();
// create and config server
const server = express();
server.use(cors());
server.use(express.json());
// init express aplication
const serverPort = 2105;
server.listen(serverPort, () => {
console.log(`Server listening at http://localhost:${serverPort}`);
});
// conexion con la BD
async function getConnection() {
const connection = await mysql.createConnection({
host: process.env.DBHOST,
user: process.env.DBUSER,
password: process.env.PASS,
database: process.env.DBNAME,
// host: 'host_de_tu_bd',
// user: 'user_de_tu_bd',
// password: 'tu_contras',
// database: 'tu_bd',
});
await connection.connect();
console.log(
`Conexión establecida con la base de datos (identificador=${connection.threadId})`
);
return connection;
}
// creamos nuestros endpoints
//get, post, put, delete
//Obtenemos todas las recetas
server.get('/recetas', async (req, res) => {
try {
const sql = 'SELECT * FROM recetas';
const conn = await getConnection();
const [results] = await conn.query(sql);
const items = results.length;
res.json({
success: true,
info: { count: items },
results: results,
});
conn.end();
} catch (error) {
res.json({
success: false,
message: error,
});
}
});
//Obtenemos receta por su ID
server.get('/recetas/:id', async (req, res) => {
try {
const id = req.params.id;
const sql = 'SELECT * FROM recetas WHERE id = ?';
const conn = await getConnection();
const [results] = await conn.query(sql, id);
const recetas = results[0];
res.json({
nombre: recetas.nombre,
ingredientes: recetas.ingredientes,
instrucciones: recetas.intrucciones,
});
conn.end();
} catch (error) {
res.json({
success: false,
message: error,
});
}
});
//Añadir nueva receta
server.post('/recetas', async (req, res) => {
try {
const nuevaReceta = req.body;
const sql =
'INSERT INTO recetas (nombre, ingredientes, instrucciones) VALUES (?, ?, ?)';
const conn = await getConnection();
const [results] = await conn.query(sql, [
nuevaReceta.nombre,
nuevaReceta.ingredientes,
nuevaReceta.instrucciones,
]);
const nuevaRecetaID = results.insertId;
res.json({
success: true,
id: nuevaRecetaID,
});
conn.end();
} catch (error) {
res.json({
success: false,
message: error,
});
}
});
//Modificar una receta existente
server.put('/recetas/:id', async (req, res) => {
try {
const id = req.params.id;
const { nombre, ingredientes, instrucciones } = req.body;
const sql =
'UPDATE recetas SET nombre = ?, ingredientes = ?, instrucciones = ? WHERE id= ?';
const conn = await getConnection();
const [results] = await conn.query(sql, [
nombre,
ingredientes,
instrucciones,
id,
]);
res.json({
success: true,
message: `Se ha modificado la receta con el ID ${id}`,
});
conn.end();
} catch (error) {
res.json({
success: false,
message: error,
});
}
});
//Eliminar una receta
server.delete('/recetas/:id', async (req, res) => {});