-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59c1e70
commit cdbd0e2
Showing
15 changed files
with
770 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var Schema = mongoose.Schema; | ||
|
||
var hospitalSchema = new Schema({ | ||
|
||
nombre: { type: String, required: [true, 'El nombre es necesario'] }, | ||
img: { type: String, required: false }, | ||
usuario: { type: Schema.Types.ObjectId, ref: 'Usuario' } | ||
}, { collection: 'hospitales' }); | ||
|
||
module.exports = mongoose.model('Hospital', hospitalSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var mongoose = require('mongoose'); | ||
var Schema = mongoose.Schema; | ||
var medicoSchema = new Schema({ | ||
|
||
nombre: { type: String, required: [true, 'El nombre es necesario'] }, | ||
img: { type: String, required: false }, | ||
usuario: { type: Schema.Types.ObjectId, ref: 'Usuario', required: true }, | ||
hospital: { type: Schema.Types.ObjectId, ref: 'Hospital', required: [true, 'El id hospital es un campo obligatorio'] } | ||
|
||
}); | ||
module.exports = mongoose.model('Medico', medicoSchema); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
var express = require('express'); | ||
|
||
var app = express(); | ||
|
||
var Hospital = require('../models/hospital') | ||
var Medicos = require('../models/medico') | ||
var Usuarios = require('../models/usuario') | ||
|
||
// =================================== | ||
// Busqueda por coleccion | ||
// =================================== | ||
|
||
app.get('/coleccion/:tabla/:busqueda', (req, res) => { | ||
var tabla = req.params.tabla; | ||
var busqueda = req.params.busqueda; | ||
var regex = new RegExp(busqueda, 'i') | ||
|
||
var promesa; | ||
|
||
switch (tabla) { | ||
case 'medico': | ||
promesa = buscarMedicos(regex); | ||
break; | ||
case 'usuario': | ||
promesa = buscarUsuarios(regex); | ||
break; | ||
case 'hospital': | ||
promesa = buscarHospitales(regex); | ||
break; | ||
default: | ||
return res.status(400).json({ | ||
ok: false, | ||
mensaje: 'Los tipos de busqueda sólo son: usuarios, medicos y hospitales', | ||
error: { message: 'Tipo de tabla no válido' } | ||
}); | ||
|
||
} | ||
promesa.then(data => { | ||
res.status(200).json({ | ||
ok: true, | ||
[tabla]: data, | ||
}); | ||
}) | ||
}) | ||
// =================================== | ||
// Busqueda general | ||
// =================================== | ||
|
||
app.get('/todo/:busqueda', (req, res) => { | ||
|
||
var busqueda = req.params.busqueda; | ||
var regex = new RegExp(busqueda, 'i') | ||
|
||
Promise.all([ | ||
buscarHospitales(regex), | ||
buscarMedicos(regex), | ||
buscarUsuarios(regex) | ||
]) | ||
.then(respuestas => { | ||
res.status(200).json({ | ||
ok: true, | ||
hospitales: respuestas[0], | ||
medicos: respuestas[1], | ||
usuarios: respuestas[2] | ||
}); | ||
}) | ||
}); | ||
|
||
|
||
function buscarHospitales(regex) { | ||
|
||
return new Promise((resolve, reject) => { | ||
Hospital.find({ nombre: regex }) | ||
.populate('usuario', 'nombre email') | ||
.exec((err, hospitales) => { | ||
|
||
if (err) { | ||
reject('Error al cargar hospitales', err); | ||
} else { | ||
resolve(hospitales); | ||
} | ||
}); | ||
}) | ||
|
||
} | ||
|
||
function buscarMedicos(regex) { | ||
|
||
return new Promise((resolve, reject) => { | ||
Medicos.find({ nombre: regex }) | ||
.populate('usuario', 'nombre email') | ||
.populate('hospital') | ||
.exec((err, medicos) => { | ||
if (err) { | ||
reject('Error al cargar medicos', err); | ||
} else { | ||
resolve(medicos); | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
function buscarUsuarios(regex) { | ||
|
||
return new Promise((resolve, reject) => { | ||
Usuarios.find({}, 'nombre email role') | ||
.or([{ 'nombre': regex }, { 'email': regex }]) | ||
.exec((err, usuarios) => { | ||
if (err) { | ||
reject('Error al cargar usuarios', err) | ||
} else { | ||
resolve(usuarios); | ||
} | ||
}) | ||
}) | ||
|
||
} | ||
|
||
module.exports = app; |
Oops, something went wrong.