This repository has been archived by the owner on Mar 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
334 lines (268 loc) · 12.6 KB
/
routes.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
var express = require('express');
var bodyParser = require('body-parser');
var _ = require('underscore');
var s_ = require("underscore.string");
var mysql = require('mysql');
var cors = require('cors');
var app = express();
//conexion a mysql
var host_mysql, user_mysql, password_mysql, database_mysql;
host_mysql = process.env.OPENSHIFT_MYSQL_DB_HOST || 'localhost';
user_mysql = process.env.OPENSHIFT_MYSQL_DB_USERNAME || 'root';
password_mysql = process.env.OPENSHIFT_MYSQL_DB_PASSWORD || '';
database_mysql = process.env.OPENSHIFT_APP_NAME || 'api';
var con = mysql.createConnection({
host: host_mysql,
user: user_mysql,
password: password_mysql,
database: database_mysql
});
// create application/json parser
var jsonParser = bodyParser.json();
app.use(bodyParser.json({limit: '10mb'}));
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(cors());
// middleware that is specific to this app
app.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
// define the home page route
app.get('/', function(req, res) {
res.send('API home page');
});
//rutas de las peliculas
app.get('/peliculas', function (req, res) {
con.query('SELECT Pelicula.*, contaRepro(Pelicula.idPelicula) AS numReproducciones, GROUP_CONCAT(Genero.nombre) AS Generos FROM Pelicula INNER JOIN Genero_Pelicula ON Pelicula.idPelicula = Genero_Pelicula.Pelicula_idPelicula INNER JOIN Genero ON Genero_Pelicula.Genero_idGenero = Genero.idGenero GROUP BY Pelicula.idPelicula', function(err, rows) {
if(err) throw err;
res.setHeader('Content-Type', 'application/json');
res.send(rows);
});
});
app.get('/peliculas/:id_pelicula', function (req, res) {
res.setHeader('Content-Type', 'application/json');
con.query('SELECT Pelicula.*, contaRepro(Pelicula.idPelicula) AS numReproducciones, GROUP_CONCAT(Genero.nombre) AS Generos FROM Pelicula INNER JOIN Genero_Pelicula ON Pelicula.idPelicula = Genero_Pelicula.Pelicula_idPelicula INNER JOIN Genero ON Genero_Pelicula.Genero_idGenero = Genero.idGenero WHERE Pelicula.idPelicula = " ' + req.params.id_pelicula + ' "', function(err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.send({ "mensaje": "No existe el id '" + req.params.id_pelicula + "' en la base de datos." });
} else {
res.send(rows);
}
});
});
app.get("/generos", function (req, res) {
res.setHeader('Content-Type', 'application/json');
con.query('SELECT Genero.* FROM Genero', function (err, rows) {
if (err) throw err;
if (_.isEmpty(rows)) {
res.send({ "mensaje": "No hay generos en la base de datos" });
} else {
res.send(rows);
}
})
})
app.get("/generos/:id_genero", function (req, res) {
res.setHeader('Content-Type', 'application/json');
con.query('SELECT Pelicula.*, Genero.* FROM Genero INNER JOIN Genero_Pelicula ON Genero.idGenero = Genero_Pelicula.Genero_idGenero INNER JOIN Pelicula ON Genero_Pelicula.Pelicula_idPelicula = Pelicula.idPelicula WHERE Genero.idGenero = " '+ req.params.id_genero +' "', function (err, rows) {
if (err) throw err;
if (_.isEmpty(rows)) {
res.send({ "mensaje": "No hay pelicula con ese genero en la base de datos" });
} else {
res.send(rows);
}
})
})
app.get('/pelicula-generos/:id_pelicula', function (req, res) {
res.setHeader('Content-Type', 'application/json');
con.query('SELECT Genero.Nombre FROM Genero INNER JOIN Genero_Pelicula ON Genero.idGenero = Genero_Pelicula.Genero_idGenero INNER JOIN Pelicula ON Genero_Pelicula.Pelicula_idPelicula = Pelicula.idPelicula WHERE Pelicula.idPelicula = " ' + req.params.id_pelicula + ' "', function(err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.send({ "mensaje": "No existe el id '" + req.params.id_pelicula + "' en la base de datos." });
} else {
res.send(rows);
}
});
});
app.get('/pelicula-actores/:id_pelicula', function (req, res) {
res.setHeader('Content-Type', 'application/json');
con.query('SELECT Actor.*, Actor_Pelicula.Personaje FROM Actor INNER JOIN Actor_Pelicula ON Actor.idActor = Actor_Pelicula.Actor_idActor INNER JOIN Pelicula ON Actor_Pelicula.Pelicula_idPelicula = Pelicula.idPelicula WHERE Pelicula.idPelicula = " ' + req.params.id_pelicula + ' "', function(err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.send({ "mensaje": "No existe el id '" + req.params.id_pelicula + "' en la base de datos." });
} else {
res.send(rows);
}
});
});
app.get('/pelicula-directores/:id_pelicula', function (req, res) {
res.setHeader('Content-Type', 'application/json');
con.query('SELECT Director.* FROM Director INNER JOIN Director_Pelicula ON Director.idDirector = Director_Pelicula.Director_idDirector INNER JOIN Pelicula ON Director_Pelicula.Pelicula_idPelicula = Pelicula.idPelicula WHERE Pelicula.idPelicula = " ' + req.params.id_pelicula + ' "', function(err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.send({ "mensaje": "No existe el id '" + req.params.id_pelicula + "' en la base de datos." });
} else {
res.send(rows);
}
});
});
app.post('/validar-cliente', urlencodedParser, function (req, res) {
if (!req.body) return res.sendStatus(400);
var cliente = "";
console.log("mail: "+req.body.email_login+" - pass: "+req.body.password_login);
con.query('SELECT Cliente.* FROM Cliente WHERE Cliente.email = "'+req.body.email_login+'" AND Cliente.password = MD5("'+req.body.password_login+'")', function(err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.sendStatus(401);
} else {
cliente = rows;
con.query('SELECT Empleado.* FROM Empleado INNER JOIN Cliente ON Empleado.Cliente_idCliente = Cliente.idCliente WHERE Cliente.idCliente = " ' + cliente[0].idCliente + ' "', function (error, row) {
if(error) throw error;
if (_.isEmpty(row)) {
res.send(cliente);
} else {
cliente[0].empleado_info = row;
res.send(cliente);
}
})
}
});
});
app.get('/users/cliente/:id_cliente', function (req, res) {
con.query('SELECT Usuario.* FROM Cliente INNER JOIN Usuario ON Cliente.idCliente = Usuario.Cliente_idCliente WHERE Cliente.idCliente = "' + req.params.id_cliente + '"', function(err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.sendStatus(404);
} else {
res.setHeader('Content-Type', 'application/json');
res.send(rows);
}
});
});
app.get('/users/:id_usuario', function (req, res) {
con.query('SELECT Usuario.* FROM Usuario WHERE Usuario.idUsuario = "' + req.params.id_usuario + '"', function (err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.sendStatus(404);
} else {
res.setHeader('Content-Type', 'application/json');
res.send(rows);
}
});
});
app.get('/peliculas/:id_pelicula/subtitulos', function (req, res) {
con.query('SELECT Subtitulo.* FROM Pelicula INNER JOIN Subtitulo ON Pelicula.idPelicula = Subtitulo.Pelicula_idPelicula WHERE Pelicula.idPelicula = "' + req.params.id_pelicula + '"', function (err, rows) {
if(err) throw err;
if (_.isEmpty(rows)) {
res.sendStatus(404);
} else {
res.setHeader('Content-Type', 'application/json');
res.send(rows);
}
});
})
app.get('/empleado', function (req, res) {
con.query('SELECT Empleado.idEmpleado, Empleado.rango, Cliente.* FROM Empleado INNER JOIN Cliente ON Empleado.Cliente_idCliente = Cliente.idCliente', function (err, rows) {
if (err) throw err;
if (_.isEmpty(rows)) {
res.sendStatus(404);
} else {
res.setHeader('Content-Type', 'application/json');
res.send(rows);
}
})
})
app.get('/empleado/:id_empleado', function (req, res) {
con.query('SELECT Empleado.idEmpleado, Empleado.rango, Cliente.* FROM Empleado INNER JOIN Cliente ON Empleado.Cliente_idCliente = Cliente.idCliente WHERE Empleado.idEmpleado = "'+ req.params.id_empleado +'"', function (err, rows) {
if (err) throw err;
if (_.isEmpty(rows)) {
res.sendStatus(404);
} else {
res.setHeader('Content-Type', 'application/json');
res.send(rows);
}
})
})
app.post('/peliculas/reproduccion/:id_pelicula', jsonParser, urlencodedParser, function (req, res) {
//peticion
if (!req.body) return res.status(400).json({"msg":"Error"})
con.query("INSERT INTO Historial (idHistorial, fecha_hora_reproduccion, Pelicula_idPelicula, Usuario_idUsuario, Usuario_Cliente_idCliente) VALUES (DEFAULT , NOW(), '"+ req.params.id_pelicula +"', '"+ req.body.datos.id_usuario +"', '"+ req.body.datos.id_cliente +"')", function (err, rows) {
if(err) throw err
if (!_.isEmpty(rows)) {
res.status(200).json(rows)
} else {
res.status(400).json({"msg": "No se ha podido agregar la reproduccion"})
}
})
})
app.get('/clientes/:id_cliente', function (req, res) {
if (!req.params.id_cliente) return res.status(400).json({"msg":"No se ha especificado ID de cliente"})
con.query("SELECT Cliente.* FROM Cliente WHERE Cliente.idCliente = '"+req.params.id_cliente+"'", function (err, rows) {
if(err) throw err
if (!_.isEmpty(rows)) {
res.status(200).json(rows)
} else {
res.status(400).json({"msg": "No se ha encontrado un cliente con el id especificado"})
}
})
})
app.get('/clientes', function (req, res) {
con.query("SELECT Cliente.* FROM Cliente", function (err, rows) {
if(err) throw err
if (!_.isEmpty(rows)) {
res.status(200).json(rows)
} else {
res.status(400).json({"msg": "error"})
}
})
})
app.post('/clientes/nuevo', jsonParser, urlencodedParser, function (req, res) {
if (!req.body) return res.status(400).json({"msg":"No se ha ejecutado correctamente."})
var datos = req.body.registro;
//creamos el cliente en la base de datos
con.query("INSERT INTO Cliente (idCliente, email, password, fecha_nacimiento, nombre, apellido, telefono, direccion, tipo_suscripcion_id_tipo_suscripcion, fecha_registro) VALUES (DEFAULT, '"+datos.profile.email+"', '"+datos.profile.password+"', '"+datos.profile.fecha_nacimiento+"', '"+datos.profile.nombre+"', '"+datos.profile.apellido+"', '"+datos.profile.telefono+"', '"+datos.profile.direccion+"', '"+datos.subscription.id_tipo_suscripcion+"', NOW())", function (err, result) {
if (err) throw err;
if (!_.isEmpty(result)) {
//guardamos el id del nuevo cliente creado
let id_cliente = result.insertId;
//creamos usuario
con.query("INSERT INTO Usuario (idUsuario, nickname, biografia, imagen_perfil, Cliente_idCliente, admin) VALUES (DEFAULT, '"+datos.user.nickname+"', '"+datos.user.bio+"', '"+datos.user.imagen+"', '"+id_cliente+"', 'true')", function (err, result) {
if (err) throw err;
if (!_.isEmpty(result)) {
//guardar gustos
let id_usuario = result.insertId;
let gustos = datos.user.gustos;
_.each(gustos, function (data) {
con.query("INSERT INTO Genero_Usuario_Gustos (Genero_Gustos, Usuario_Gustos) VALUES ('"+data.idGenero+"', '"+id_usuario+"')", function (err, result) {
if (err) throw err;
});
});
} else {
res.status(400).json({"msg":"Error"});
}
});
//insertamos los datos de pago
let nombre_tarjeta = datos.profile.nombre + " " + datos.profile.apellido;
let fecha_exp = datos.payment.vencimiento.mes + "/" + datos.payment.vencimiento.ano;
con.query("INSERT INTO detalles_pago_cliente (iddetalles_pago_cliente, codigo_tarjeta, ccv_tarjeta, nombre_tarjeta, fecha_exp_tarjeta, tipo_tarjeta, Cliente_idCliente) VALUES (DEFAULT, '"+datos.payment.card+"', '"+datos.payment.cvv+"', '"+nombre_tarjeta+"','"+fecha_exp+"', '"+datos.payment.tipo+"', '"+id_cliente+"')", function (err, result) {
if (err) throw err;
})
//enviamos estado de registro exitoso
res.status(200).json({"msg":"Registro exitoso."})
} else {
res.status(500 ).json({"msg":"Ha ocurrido un error inesperado."});
}
})
})
app.get("/clientes/:id_cliente/pagos", function (req, res) {
if (!req.params.id_cliente) return res.status(400).json({"msg":"No se ha podido procesar su petición."})
con.query("SELECT Cliente.*, detalles_pago_cliente.* FROM Cliente INNER JOIN detalles_pago_cliente ON Cliente.idCliente = detalles_pago_cliente.Cliente_idCliente WHERE Cliente.idCliente = '"+req.params.id_cliente+"'", function (err, rows) {
if (err) throw err
if (!_.isEmpty(rows)) {
res.status(200).json(rows)
} else {
res.status(400).json({"msg":"No se ha encontrado el cliente solicitado."})
}
})
})
module.exports = app;