Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the doctor in the admin #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions public/js/addDoctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const name = document.querySelector('#name');
const email = document.querySelector('#email');
const password = document.querySelector('#password');
const addDoctorB = document.querySelector('#addDoctorB');
const nameErr = document.querySelector('.nameErr');
const emailErr = document.querySelector('.emailErr');
const passwordErr = document.querySelector('.passwordErr');
const doctorimg = document.querySelector('#doctorimg');
const doctordesc = document.querySelector('#doctordesc');
const descErr = document.querySelector('.descErr');

// displaying error msg
let displayErr = (errElem, errMsg) => {
errElem.textContent = errMsg;
};
// Get the text box values when invoke the function
let collectData = () => ({
name: name.value,
email: email.value,
password: password.value,
image:doctorimg.value,
description:doctordesc.value,
});

// check doctor name value
let checkName = () => {
if (name.value.trim().length == 0) {
displayErr(nameErr, 'Please Enter a Name ');
} else {
displayErr(nameErr, '');
return true;
}
};

// check doctor email validity
let checkEmail = () => {
if (email.validity.patternMismatch) {
displayErr(emailErr, 'Please enter a valid email address');
} else if (email.value.trim().length == 0) {
displayErr(emailErr, 'Please enter an email address');
} else {
displayErr(emailErr, '');
return true;
}
};

// check doctor password validity
let checkPw = () => {
if (password.validity.patternMismatch) {
displayErr( passwordErr,'Password must contain at least eight characters, including one letter and one number');
} else if (password.validity.valueMissing) {
displayErr(passwordErr, 'Please enter a password');
} else {
displayErr(passwordErr, '');
return true;
}
};

// check doctor description exist
let checkDescription = () => {
if (doctordesc.value.trim().length == 0) {
displayErr(descErr, 'Please Enter The Doctor description ');
} else {
displayErr(descErr, '');
return true;
}
};

// the function is run when all the values (-img) does exist
const send = () => {
if (checkName() && checkEmail() && checkPw() && checkDescription()) {
const obj = collectData();
fetch('/addDoctor', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(obj),
}).then(response => response.json())
.then((result)=>{
//modification here according to the css response form
})
.catch((error)={
// also modify here
})
}
};

name.addEventListener('focusout', checkName);
email.addEventListener('focusout', checkEmail);
password.addEventListener('focusout', checkPw);
doctordesc.addEventListener('focusout', checkDescription);
addDoctorB.addEventListener('click', send)
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const unlockCookie = require('./middlewares');
const app = express();

// Middlewares
app.set('port', process.env.PORT || 3000);
app.set('port', process.env.PORT || 4000);
app.use(express.static(path.join(__dirname, '..', 'public')));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
Expand Down
50 changes: 45 additions & 5 deletions src/controllers/admin.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
const { insertArticle } = require('../model/queries/addData');
const bcrypt = require('bcryptjs');
const {
insertArticle,
insertDoctor
} = require('../model/queries/addData');
const saltRounds = 10;

exports.get = (req, res) => {
console.log(res.locals.unlockCookie);
if (res.locals.unlockCookie.permission === 'admin') {
res.render('admin', {
title: 'Admin Panel', headerFound: true, footerFound: true, asideFound: true, style: ['admin', 'header', 'footer'], javascript: ['hamburger', 'admin'], login: (res.locals.unlockCookie === null), username: (res.locals.unlockCookie === null) ? 'Unkown' : res.locals.unlockCookie.username,
});
res.render('admin', {
title: 'Admin Panel',
headerFound: true,
footerFound: true,
asideFound: true,
style: ['admin', 'header', 'footer'],
javascript: ['hamburger', 'addArticle'],
login: (res.locals.unlockCookie === null), username: (res.locals.unlockCookie === null) ? 'Unkown' : res.locals.unlockCookie.username,
});
} else {
res.clearCookie('jwt');
res.redirect('/signIn');
}
};

exports.addArticle = (req, res) => {
insertArticle(req.body).then((result) => { res.json((result.rows[0])); });
insertArticle(req.body).then((result) => {
res.json((result.rows[0]));
});
};

exports.addDoctor = (req, res) => {
res.render('demoDoctor', {
title: 'Add doctor',
headerFound: false,
footerFound: false,
asideFound: false,
javascript: ['addDoctor'],
});
};

exports.addDoctorDB = (req, res) => {
let {
password,
image
} = req.body;
if (image.trim().length == 0) {
req.body.image = 'https://myblue.bluecrossma.com/sites/g/files/csphws636/files/inline-images/Doctor%20Image%20Desktop.png';
}
bcrypt.hash(password, saltRounds).then((hash) => {
req.body.password = hash;
insertDoctor(req.body).then((result) => {
res.json((result.rows[0]));
});
});

};
11 changes: 8 additions & 3 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Router = require('express').Router();
const home = require('./home');
const admin = require('./admin');
const {
get, addArticle, addDoctor, addDoctorDB
} = require('./admin');
const article = require('./article');
const articles = require('./articles');
const doctors = require('./doctors');
Expand All @@ -20,8 +22,11 @@ Router.route('/signIn')
.post(signIn.post);

// Admin Routes
Router.get('/admin', admin.get);
Router.post('/admin/addArticle', admin.addArticle);
Router.get('/admin', get);
Router.post('/admin/addArticle', addArticle);
Router.route('/addDoctor')
.get(addDoctor)
.post(addDoctorDB);

// Article Routes
Router.get('/article/:id', article.get);
Expand Down
15 changes: 14 additions & 1 deletion src/model/queries/addData.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@ const insertArticle = (article) => {
return db.query(sql, [title, img, body]);
};

module.exports = { insertArticle };
const insertDoctor = (doctor) => {
const {
name,
email,
password,
image,
description,
} = doctor;
const sql = 'INSERT INTO users (user_name,user_email,user_password,doctor_image,doctor_description,permission) VALUES ($1, $2, $3, $4, $5, $6) RETURNING user_id';
return db.query(sql, [name, email, password, image, description, 'doctor']);
};


module.exports = { insertArticle, insertDoctor };
4 changes: 2 additions & 2 deletions src/views/admin.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="box hvr-sweep-to-right">
<div class="content">
<img src="https://thumb.ibb.co/et95Pp/25ee6afa66526c6f0a8b041dc68e9762.png" alt="doctors" border="0">
<span>Add doctor</span>
<span> <a href="/addDoctor">Add Doctor</a></span>
</div>
</div>
<div class="box hvr-sweep-to-right active">
Expand Down Expand Up @@ -38,4 +38,4 @@
<button id="article" type="submit">Add Article</button>
</div>
</div>
</section>
</section>
10 changes: 10 additions & 0 deletions src/views/demoDoctor.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<input id="name" type="text" placeholder="name">
<p class="nameErr"></p>
<input id="email"type="email" placeholder="email" required pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$">
<p class="emailErr"></p>
<input id="password"type="password" placeholder="password" required pattern="^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$">
<p class="passwordErr"></p>
<input id="doctorimg"type="text" placeholder="img">
<input id="doctordesc" type="text" placeholder="description">
<p class="descErr"></p>
<button id="addDoctorB"type="submit" name="button">ADD</button>