-
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
Showing
16 changed files
with
613 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
node_modules/ | ||
package-lock.json | ||
uploads/users/* | ||
uploads/hospitals/* | ||
uploads/doctors/* |
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,87 @@ | ||
const {response} = require('express'); | ||
|
||
const Doctors = require('../models/doctors'); | ||
const {generateJWT} = require('../helpers/jwt'); | ||
|
||
|
||
const getDoctors = async (req, res = response) => { | ||
try { | ||
const doctors = await Doctors.find().populate('user', 'name img') | ||
.populate('hospital', 'name img'); | ||
|
||
res.json({ | ||
ok:true, | ||
doctors | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
const createDoctor = async (req, res = response) => { | ||
try { | ||
const uid = req.uid; | ||
const doctor = new Doctors({ | ||
...req.body, | ||
user: uid | ||
}); | ||
|
||
// Save hospital | ||
const newDoctor = await doctor.save(); | ||
|
||
res.json({ | ||
ok:true, | ||
doctor: newDoctor | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
const updateDoctor = async (req, res = response) => { | ||
const uid = req.params.id | ||
|
||
try { | ||
res.json({ | ||
ok:true, | ||
msg: "updateDoctor" | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}) | ||
} | ||
}; | ||
|
||
const deleteDoctor = async (req, res = response) => { | ||
|
||
try { | ||
res.json({ | ||
ok:true, | ||
msg: "Doctor deleted successfully" | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}) | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getDoctors, | ||
createDoctor, | ||
updateDoctor, | ||
deleteDoctor | ||
} |
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,86 @@ | ||
const {response} = require('express'); | ||
|
||
const Hospitals = require('../models/hospitals'); | ||
const {generateJWT} = require('../helpers/jwt'); | ||
|
||
|
||
const getHospitals = async (req, res = response) => { | ||
try { | ||
const hospitals = await Hospitals.find().populate('user', 'name img'); | ||
|
||
res.json({ | ||
ok:true, | ||
hospitals | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
const createHospital = async (req, res = response) => { | ||
try { | ||
const uid = req.uid; | ||
const hospital = new Hospitals({ | ||
...req.body, | ||
user: uid | ||
}); | ||
|
||
// Save hospital | ||
const newHospital = await hospital.save(); | ||
|
||
res.json({ | ||
ok:true, | ||
hospital: newHospital | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
const updateHospital = async (req, res = response) => { | ||
const uid = req.params.id | ||
|
||
try { | ||
res.json({ | ||
ok:true, | ||
msg: "updateHospital" | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}) | ||
} | ||
}; | ||
|
||
const deleteHospital = async (req, res = response) => { | ||
|
||
try { | ||
res.json({ | ||
ok:true, | ||
msg: "Hospital deleted successfully" | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}) | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getHospitals, | ||
createHospital, | ||
updateHospital, | ||
deleteHospital | ||
} |
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,76 @@ | ||
const {response} = require('express'); | ||
const Users = require('../models/users'); | ||
const Hospitals = require('../models/hospitals'); | ||
const Doctors = require('../models/doctors'); | ||
|
||
const getAll = async (req, res = response) => { | ||
try { | ||
const value = req.params.value; | ||
const regex = new RegExp(value, 'i'); | ||
|
||
const [ users, hospitals, doctors ] = await Promise.all([ | ||
Users.find({ name: regex }), | ||
Hospitals.find({ name: regex }), | ||
Doctors.find({ name: regex }) | ||
]); | ||
|
||
res.json({ | ||
ok:true, | ||
users, | ||
hospitals, | ||
doctors | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
const getAllDocument = async (req, res = response) => { | ||
try { | ||
// Get Params | ||
const table = req.params.table; | ||
const value = req.params.value; | ||
const regex = new RegExp(value, 'i'); | ||
|
||
let results = []; | ||
// Check collection | ||
switch(table) { | ||
case "users": | ||
results = await Users.find({ name: regex }); | ||
break; | ||
case "hospitals": | ||
results = await Hospitals.find({ name: regex }).populate('user', 'name img'); | ||
break; | ||
case "doctors": | ||
results = await Doctors.find({ name: regex }).populate('user', 'name img') | ||
.populate('hospital', 'name img'); | ||
break; | ||
default: | ||
return res.status(400).json({ | ||
ok: false, | ||
msg: "The collection must be users/hospitals/doctors" | ||
}); | ||
} | ||
|
||
// return data | ||
res.json({ | ||
ok:true, | ||
results, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getAll, | ||
getAllDocument | ||
} |
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,108 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const {response} = require('express'); | ||
const { v4: uuidv4 } = require('uuid'); | ||
const Users = require('../models/users'); | ||
const Hospitals = require('../models/hospitals'); | ||
const Doctors = require('../models/doctors'); | ||
const { updateImage } = require('../helpers/update-image'); | ||
|
||
const fileUpload = async (req, res = response) => { | ||
try { | ||
const type = req.params.type; | ||
const id = req.params.id; | ||
|
||
// Validate type | ||
const arrayTypes = ['users', 'hospitals', 'doctors']; | ||
if(!arrayTypes.includes(type)) { | ||
return res.status(400).json({ | ||
ok: false, | ||
msg: "Type not correct" | ||
}); | ||
} | ||
|
||
// Validate file | ||
if (!req.files || Object.keys(req.files).length === 0) { | ||
return res.status(400).json({ | ||
ok: false, | ||
msg: "No files were uploaded." | ||
}); | ||
} | ||
|
||
// Process image | ||
const file = req.files.img | ||
const splitName = file.name.split('.') // e.g. wolverine . jpg | ||
const extensionFile = splitName[splitName.length -1]; | ||
|
||
// validate file | ||
const arrayExtensions = ['png','jpg', 'jpeg', 'gif']; | ||
if(!arrayExtensions.includes(extensionFile)) { | ||
return res.status(400).json({ | ||
ok: false, | ||
msg: "Extension not allowed" | ||
}); | ||
} | ||
|
||
//Generate name | ||
const nameFile = `${uuidv4()}.${extensionFile}`; | ||
|
||
// Path for save file | ||
const path = `./uploads/${type}/${nameFile}`; | ||
|
||
// Move img | ||
file.mv(path, (err) => { | ||
if (err) { | ||
console.log(err); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Image move error" | ||
}); | ||
} | ||
|
||
// Update BD | ||
updateImage(type, id, nameFile); | ||
|
||
res.json({ | ||
ok:true, | ||
msg: "File Upload", | ||
nameFile | ||
}); | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
const returnImg = async (req, res = response) => { | ||
try { | ||
const type = req.params.type; | ||
const img = req.params.img; | ||
|
||
const pathImg = path.join(__dirname, `../uploads/${type}/${img}`); | ||
|
||
if(fs.existsSync(pathImg)) { | ||
res.sendFile(pathImg); | ||
} else { | ||
const pathImg = path.join(__dirname, `../uploads/no-img.jpg`); | ||
res.sendFile(pathImg); | ||
} | ||
|
||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
ok: false, | ||
msg: "Unexpected error, check logs" | ||
}); | ||
} | ||
}; | ||
|
||
|
||
|
||
module.exports = { | ||
fileUpload, | ||
returnImg | ||
} |
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
Oops, something went wrong.