Skip to content

Commit

Permalink
Section 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Candoyeya committed Aug 1, 2021
1 parent 79ab6cc commit 4870b8a
Show file tree
Hide file tree
Showing 16 changed files with 613 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
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/*
87 changes: 87 additions & 0 deletions controllers/doctors.js
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
}
86 changes: 86 additions & 0 deletions controllers/hospitals.js
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
}
76 changes: 76 additions & 0 deletions controllers/searches.js
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
}
108 changes: 108 additions & 0 deletions controllers/uploads.js
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
}
12 changes: 10 additions & 2 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ const {generateJWT} = require('../helpers/jwt');

const getUsers = async (req, res = response) => {
try {
const users = await Users.find({}, 'name email role google');
const from = Number(req.query.from) || 0;

const [users, total] = await Promise.all([
Users
.find({}, 'name email role google img')
.skip(from)
.limit(5),
Users.countDocuments()
]);

res.json({
ok:true,
users,
uid: req.uid
total
});
} catch (error) {
console.log(error);
Expand Down
Loading

0 comments on commit 4870b8a

Please sign in to comment.