-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
458 lines (380 loc) · 14.7 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
const port = process.env.PORT ? process.env.PORT : 5000;
app.use(express.urlencoded());
app.use(express.json());
/*******************************************************************/
/******************** CONEXION CON LA BD ***************************/
/*******************************************************************/
const mysql = require('mysql');
const util = require('util');
const conexion = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'biblioteca',
});
conexion.connect((error) => {
if (error) {
console.log('ERROR:NO SE PUDO CONECTAR CON LA BASE DE DATOS.')
throw error;
}
console.log('Conexion con la base de datos mysql establecida')
});
const qy = util.promisify(conexion.query).bind(conexion);
/*****************************************************************/
/******************** RUTAS CATEGORIAS ****************************/
/*****************************************************************/
/* Crear nueva categoria */
app.post('/categoria', async (req, res) => {
try {
if (req.body.nombre.match(/^ *$/)) {
throw new Error("Faltan datos.");
}
let query = 'select * from categorias where nombre=?';
const existeCategoria = await qy(query, req.body.nombre.toUpperCase());
if (existeCategoria.length > 0) {
throw new Error("Ese nombre de categoria ya existe.")
}
query = 'INSERT INTO categorias (nombre) VALUE (?)';
let respuesta = await qy(query, req.body.nombre.toUpperCase());
query = 'select * from categorias where nombre=?';
const consultaNuevaCategoria = await qy(query, req.body.nombre.toUpperCase());
res.status(200).send(consultaNuevaCategoria[0]);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/***/
/* Consultar categorias */
app.get('/categoria', async (req, res) => {
try {
let query = 'SELECT * FROM CATEGORIAS';
let respuesta = await qy(query);
res.status(200).send(respuesta);
}
catch (e) {
res.status(413).send([]);
}
});
/***/
/* Consultar una categoria usando id numerico*/
app.get('/categoria/:id', async (req, res) => {
try {
let query = 'SELECT * FROM CATEGORIAS where id=?';
let respuesta = await qy(query, req.params.id);
if (respuesta.length == 0) {
throw new Error("Categoria no encontrada.");
}
res.status(200).send(respuesta[0]);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/***/
/* Borrar categoria */
app.delete('/categoria/:id', async (req, res) => {
try {
let query = 'SELECT * FROM libros WHERE categoria_id=?';
let respuesta = await qy(query, req.params.id);
if (respuesta.length > 0) {
throw new Error("Categoria con libros asociados, no se puede eliminar.");
}
query = 'SELECT * FROM categorias WHERE id=?';
respuesta = await qy(query, req.params.id);
if (respuesta.length == 0) {
throw new Error("No existe la categoria indicada.");
}
query = 'DELETE FROM categorias WHERE id=?';
respuesta = await qy(query, req.params.id);
res.status(200).send({ "mensaje": "La categoria se borro correctamente." });
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/***/
/****************************************************************/
/******************** RUTAS PERSONA *******************************/
/****************************************************************/
/* Hacerse socio de la Biblio */
app.post('/persona', async (req, res) => {
try {
if (req.body.alias.match(/^ *$/) || req.body.nombre.match(/^ *$/) || req.body.apellido.match(/^ *$/) || req.body.email.match(/^ *$/)) {
throw new Error("Debe llenar todos los campos obligatorios!");
}
const persona = {
alias: req.body.alias.toUpperCase(),
nombre: req.body.nombre.toUpperCase(),
apellido: req.body.apellido.toUpperCase(),
email: req.body.email.toUpperCase()
};
let query = 'SELECT * FROM personas WHERE email=?';
let respuesta = await qy(query, persona.email);
if (respuesta.length > 0) {
throw new Error("El email ya se ecuentra registrado.")
}
query = 'INSERT INTO personas (alias,nombre,apellido,email) VALUE (?,?,?,?)';
respuesta = await qy(query, [persona.alias, persona.nombre, persona.apellido, persona.email]);
query = 'SELECT * FROM personas WHERE email=?';
respuesta = await qy(query, persona.email);
res.status(200).send(respuesta[0]);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/*consultar personas */
app.get('/persona', async (req, res) => {
try {
let query = 'SELECT * FROM personas';
let respuesta = await qy(query);
if (respuesta.length > 0) {
res.status(200).send(respuesta);
} else {
res.status(200).send({ mensaje: 'No hay ninguna persona registrada.' })
}
}
catch (e) {
res.status(413).send({ mensaje: 'Error inesperado.' })
};
});
/***/
/*consultar datos de una persona usando id numerico. */
app.get('/persona/:id', async (req, res) => {
try {
let query = 'SELECT * FROM personas WHERE id=?';
let respuesta = await qy(query, req.params.id);
if (respuesta.length == 0) {
throw new Error("No se encuentra esa persona.");
}
res.status(200).send(respuesta[0])
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/***/
/*modificar persona usando id numerico*/
app.put('/persona/:id', async (req, res) => {
try {
let query = 'SELECT * FROM personas WHERE id=?';
let respuesta = await qy(query, req.params.id);
if (respuesta.length == 0) {
throw new Error("No se encuentra esa persona.");
}
//email no puede ser modificado
if (req.body.alias.match(/^ *$/) || req.body.nombre.match(/^ *$/) || req.body.apellido.match(/^ *$/)) {
throw new Error("Para modificar debe enviar nuevos alias,nombre y apellido.El email no puede ser modificado.");
}
const nombre = req.body.nombre.toUpperCase();
const apellido = req.body.apellido.toUpperCase();
const alias = req.body.alias.toUpperCase();
respuesta = await qy('UPDATE personas SET nombre=?, apellido =?, alias=? WHERE id=?', [nombre, apellido, alias, req.params.id]);
const personaModificada = await qy('SELECT * FROM personas where id=?', [req.params.id]);
res.status(200).send(personaModificada[0]);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/*Borrar persona usando id numerico*/
app.delete('/persona/:id', async (req, res) => {
try {
let query = 'SELECT * FROM personas WHERE id=?';
let respuesta = await qy(query, req.params.id);
if (respuesta.length == 0) {
throw new Error("No se encuentra esa persona.");
}
query = 'SELECT * FROM libros WHERE persona_id=?';
respuesta = await qy(query, req.params.id);
if (respuesta.length > 0) {
throw new Error("Esa persona tiene libros asociados, no se puede eliminar.")
}
respuesta = await qy('DELETE FROM personas WHERE id=?', [req.params.id]);
res.status(200).send({ mensaje: 'Persona borrada correctamente.' });
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****************************************************************/
/******************** RUTAS LIBRO *******************************/
/****************************************************************/
/* Ingresar nuevo libro */
app.post('/libro', async (req, res) => {
try {
if (req.body.nombre.match(/^ *$/) || !req.body.categoria_id) {
throw new Error("Nombre y Categoria son datos obligatorios.");
}
let descripcion = req.body.descripcion;
if (!req.body.descripcion) {
descripcion = "Sin descripción.";
}
let libro = {
nombre: req.body.nombre.toUpperCase(),
descripcion: descripcion.toUpperCase(),
categoria_id: req.body.categoria_id,
persona_id: null
};
if (req.body.persona_id) {
libro.persona_id = req.body.persona_id;
}
const queryLibros = 'select * from libros where nombre=?';
const existeLibro = await qy(queryLibros, [libro.nombre]);
const queryCategoria = 'select * from categorias where id=?';
const existeCategoria = await qy(queryCategoria, [libro.categoria_id]);
const queryPersona = 'select * from personas where id=?';
const existePersona = await qy(queryPersona, [libro.persona_id]);
if (existeLibro.length > 0) {
throw new Error("Ese libro ya existe.");
} else if (existeCategoria.length == 0) {
throw new Error("No existe la categoria indicada.");
} else if (existePersona.length == 0 && req.body.persona_id) {
throw new Error("No existe la persona indicada.");
} else {
let guardoLibro = 'INSERT INTO libros (nombre,descripcion,categoria_id,persona_id) VALUE (?,?,?,?)';
let respuesta = await qy(guardoLibro, [libro.nombre, libro.descripcion, libro.categoria_id, libro.persona_id]);
const consultarLibro = 'select * from libros where nombre=?';
const muestroLibro = await qy(consultarLibro, [libro.nombre]);
res.status(200).send(muestroLibro[0]);
}
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/*consultar catalogo */
app.get('/libro', async (req, res) => {
try {
let query = 'select * from libros';
const todosLosLibros = await qy(query);
res.status(200).send(todosLosLibros);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/*Consultar por un libro en particular usando su id numérica. */
app.get('/libro/:id', async (req, res) => {
try {
let query = 'select * from libros where id=?';
const consultaLibro = await qy(query, req.params.id);
if (consultaLibro.length == 0) {
throw new Error("No se encuentra el libro solicitado.");
}
res.status(200).send(consultaLibro[0]);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/*Modificar descripcion del libro */
app.put('/libro/:id', async (req, res) => {
try {
if (req.body.nombre || req.body.categoria_id || req.body.persona_id) {
throw new Error("Solo se puede modificar la descripcion del libro.");
}
if (req.body.descripcion.match(/^ *$/)) {
throw new Error("Debe mandar una nueva descripción.");
}
let query = 'SELECT * FROM libros where id=?';
const existeLibro = await qy(query, req.params.id);
if (existeLibro.length == 0) {
throw new Error("No se econtro el libro.");
}
const nuevaDescripcion = req.body.descripcion.toUpperCase();
const libro_id = req.params.id.toUpperCase();
query = 'UPDATE libros SET descripcion=? WHERE id = ?;';
let respuesta = await qy(query, [nuevaDescripcion, libro_id]);
query = 'SELECT * FROM libros where id=?';
const consultaLibro = await qy(query, libro_id);
res.status(200).send(consultaLibro[0]);
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/*Prestar libro */
app.put('/libro/prestar/:id', async (req, res) => {
try {
let query = 'SELECT * FROM libros where id=?';
const existeLibro = await qy(query, req.params.id);
if (existeLibro.length == 0) {
throw new Error("No se econtro el libro.");
}
query = 'SELECT persona_id FROM libros where id=?';
const estaPrestado = await qy(query, req.params.id);
if (estaPrestado[0].persona_id) {
throw new Error("El libro ya se encuentra prestado, no se puede prestar hasta que no se devuelva.");
}
query = 'SELECT * FROM personas where id=?';
const existePersona = await qy(query, [req.body.persona_id]);
if (existePersona.length == 0) {
throw new Error("No se encontro la persona a la que se quiere prestar el libro.");
}
query = 'UPDATE libros SET persona_id=? WHERE id = ?;';
let respuesta = await qy(query, [req.body.persona_id, req.params.id]);
res.status(200).send({ mensaje: "El libro se presto correctamente." });
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/*Devolver libro */
app.put('/libro/devolver/:id', async (req, res) => {
try {
let query = 'SELECT * FROM libros WHERE id=?';
const existeLibro = await qy(query, [req.params.id]);
if (existeLibro.length == 0) {
throw new Error("Ese libro no existe!");
}
query = 'SELECT persona_id FROM libros where id=?';
const estaPrestado = await qy(query, [req.params.id]);
if (!estaPrestado[0].persona_id) {
throw new Error("Ese libro no estaba prestado!");
}
query = 'UPDATE libros SET persona_id=NULL WHERE id = ?';
let respuesta = await qy(query, [req.params.id]);
res.status(200).send({ mensaje: "Se realizó la devolución correctamente." });
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/*Borrar libro */
app.delete('/libro/:id', async (req, res) => {
try {
let query = 'SELECT * FROM libros where id=?';
const existeLibro = await qy(query, req.params.id);
if (existeLibro.length == 0) {
throw new Error("No se encuentra ese libro.");
}
query = 'SELECT persona_id FROM libros where id=?';
const estaPrestado = await qy(query, req.params.id);
if (estaPrestado[0].persona_id) {
throw new Error("Ese libro esta prestado, no se puede borrar!!");
}
query = 'DELETE FROM libros WHERE id = ?;';
let respuesta = await qy(query, [req.params.id]);
res.status(200).send({ mensaje: "El libro fue borrado correctamente." });
}
catch (e) {
res.status(413).send({ mensaje: e.message });
}
});
/****/
/****SERVER****/
app.listen(port, () => {
console.log("Servidor escuchando en el puerto", port);
});