-
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.
Seccion 11 terminada - CRUD IMAGENES
- Loading branch information
Showing
26 changed files
with
665 additions
and
11 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,11 @@ | ||
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,12 @@ | ||
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 hospita 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,116 @@ | ||
var express = require('express'); | ||
|
||
var app = express() | ||
var Hospital = require('../models/hospital'); | ||
var Medico = require('../models/medico'); | ||
var Usuario = require('../models/usuario'); | ||
|
||
|
||
// ======================================== | ||
// Busqueda por coleccion | ||
// ======================================== | ||
app.get('/coleccion/:tabla/:busqueda', (req, res, next) => { | ||
var tabla = req.params.tabla; | ||
var busqueda = req.params.busqueda; | ||
var regex = new RegExp(busqueda, 'i') | ||
|
||
if (tabla == 'medico') { | ||
Promise.all([buscarMedicos(busqueda, regex)]).then(respuestas => { | ||
res.status(200).json({ | ||
ok: true, | ||
medicos: respuestas[0] | ||
}); | ||
}); | ||
} else if (tabla == 'hospital') { | ||
Promise.all([buscarHospitales(busqueda, regex)]).then(respuestas => { | ||
res.status(200).json({ | ||
ok: true, | ||
hospitales: respuestas[0] | ||
}); | ||
}); | ||
} else if (tabla == 'usuario') { | ||
Promise.all([buscarUsuarios(busqueda, regex)]).then(respuestas => { | ||
res.status(200).json({ | ||
ok: true, | ||
usuarios: respuestas[0] | ||
}); | ||
}); | ||
} else { | ||
res.status(400).json({ | ||
ok: false, | ||
mensaje: 'Los tipos de busqueda solo son: usuarios, medicos y hospitales', | ||
error: { message: 'Tipo de tabla/coleccion no valido' } | ||
}) | ||
} | ||
|
||
}) | ||
|
||
app.get('/todo/:busqueda', (req, res, next) => { | ||
|
||
var busqueda = req.params.busqueda; | ||
var regex = new RegExp(busqueda, 'i'); | ||
|
||
Promise.all( | ||
[ | ||
buscarHospitales(busqueda, regex), | ||
buscarMedicos(busqueda, regex), | ||
buscarUsuarios(busqueda, regex) | ||
] | ||
).then(respuestas => { | ||
res.status(200).json({ | ||
ok: true, | ||
hospitales: respuestas[0], | ||
medicos: respuestas[1], | ||
usuarios: respuestas[2] | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
function buscarHospitales(busqueda, 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) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
// ======================================== | ||
// Buscar Medicos | ||
// ======================================== | ||
function buscarMedicos(busqueda, regex) { | ||
|
||
return new Promise((resolve, reject) => { | ||
Medico.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(busqueda, regex) { | ||
return new Promise((resolve, reject) => { | ||
Usuario.find({}, 'nombre email role').or([{ 'nombre': regex }, { 'email': regex }]).exec((err, usuario) => { | ||
if (err) { | ||
reject('Error al cargar usuarios', err) | ||
} else { | ||
resolve(usuario) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
|
||
|
||
module.exports = app |
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,127 @@ | ||
var express = require('express'); | ||
var app = express(); | ||
|
||
var Hospital = require('../models/hospital'); | ||
var mdAutenticacion = require('../middleware/autenticacion'); | ||
|
||
app.get('/', (req, res, next) => { | ||
|
||
var desde = req.query.desde || 0; | ||
desde = Number(desde); | ||
|
||
Hospital.find({}) | ||
.skip(desde) | ||
.limit(5) | ||
.populate('usuario', 'nombre email') | ||
.exec((err, hospital) => { | ||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'error cargando hospitales', | ||
errors: err | ||
}); | ||
} | ||
|
||
Hospital.count({}, (err, conteo) => { | ||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'error contando hospitales', | ||
errors: err | ||
}); | ||
} | ||
res.status(200).json({ | ||
ok: true, | ||
hospitales: hospital, | ||
total: conteo | ||
}); | ||
}) | ||
|
||
}); | ||
}); | ||
|
||
app.put('/:id', mdAutenticacion.verificaToken, (req, res) => { | ||
var id = req.params.id; | ||
Hospital.findById(id, (err, hospital) => { | ||
var body = req.body | ||
|
||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'error al buscar hospital', | ||
errors: err | ||
}); | ||
} | ||
if (!hospital) { | ||
return res.status(400).json({ | ||
ok: false, | ||
mensaje: 'El hospital con el' + id + 'no existe', | ||
errors: { message: 'No existe un hospital con ese ID' } | ||
}); | ||
} | ||
hospital.nombre = body.nombre; | ||
hospital.usuario = req.usuario.id | ||
hospital.save((err, hospitalGuardado) => { | ||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'Error al actualizar hospital', | ||
errors: err | ||
}); | ||
} | ||
res.status(200).json({ | ||
ok: true, | ||
Hospital: hospitalGuardado | ||
}); | ||
}) | ||
}) | ||
}) | ||
|
||
app.post('/', mdAutenticacion.verificaToken, (req, res) => { | ||
var body = req.body | ||
var hospital = new Hospital({ | ||
nombre: body.nombre, | ||
usuario: req.usuario._id | ||
}); | ||
hospital.save((err, hospitalGuardado) => { | ||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
mensaje: 'error cargando hospital', | ||
errors: err | ||
}); | ||
} | ||
res.status(200).json({ | ||
ok: true, | ||
Hospital: hospitalGuardado, | ||
usuarioToken: req.usuario | ||
}) | ||
}); | ||
}); | ||
|
||
app.delete('/:id', mdAutenticacion.verificaToken, (req, res) => { | ||
var id = req.params.id; | ||
Hospital.findByIdAndRemove(id, (err, hospitalBorrado) => { | ||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'Error borrar usuario', | ||
errors: err | ||
}); | ||
} | ||
res.status(200).json({ | ||
ok: true, | ||
hospital: hospitalBorrado | ||
}) | ||
if (!hospitalBorrado) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'No existe un hospital con ese id', | ||
errors: "no existe el hospital para borrar" | ||
}); | ||
} | ||
}) | ||
}) | ||
|
||
|
||
module.exports = app; |
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,26 @@ | ||
var express = require('express'); | ||
|
||
var app = express() | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
app.get('/:tipo/:img', (req, res, next) => { | ||
|
||
var tipo = req.params.tipo; | ||
var img = req.params.img; | ||
|
||
var pathImagen = path.resolve(__dirname, `../uploads/${ tipo }/${ img }`) | ||
if (fs.existsSync(pathImagen)) { | ||
res.sendFile(pathImagen); | ||
} else { | ||
var pathNoImagen = path.resolve(__dirname, '../assets/no-img.jpg'); | ||
res.sendFile(pathNoImagen); | ||
} | ||
// res.status(200).json({ | ||
// ok: true, | ||
// messaje: 'Peticion realizada correctamente' | ||
// }) | ||
}) | ||
|
||
|
||
module.exports = app |
Oops, something went wrong.