Commit 6ddabf22 authored by Dylan Holden's avatar Dylan Holden

Initial commit

parents
Pipeline #5472 failed with stages
in 4 seconds

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.
module.exports = function(app){
const redirectLogin = (req, res, next) => {
if (!req.session.userId ) {
res.redirect('./login')
} else { next (); }
}
//this renders the home page
app.get('/',function(req,res){
res.render('index.html')
});//this renders the about page
app.get('/about',function(req,res){
res.render('about.html');
});//this renders the search page
app.get('/search',redirectLogin,function(req,res){
res.render("search.html");
});
//this is the code that makes the form for the search page work
app.get('/search-result', function (req, res) {
//searching in the database
res.send('This is the keyword you entered: '+ req.query.keyword+ ' and this is the result of the search:' );
});
//this renders the register page
app.get('/register', function (req,res) {
res.render('register.html');
});//this makes the form for the register page work
app.post('/registered', function (req,res) {
const bcrypt = require('bcrypt');
const saltRounds = 10
const plainPassword = req.body.password;
var hashedPassword = req.body.password;
//saving data in database
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost';
MongoClient.connect(url, function(err, client) {
if (err) throw err;
var db = client.db('listusersdb');
db.collection('users').insertOne({
first : req.body.first,
last : req.body.last,
email: req.body.email,
name: req.body.name,
password: req.body.password
});
client.close();
bcrypt.hash(plainPassword, saltRounds, function(err, hashedPassword) {
// Store hashed password in your database.
res.send('You are now registered, Your user name is: '+ req.body.name + ' your password is: '+ req.body.password +' and your hashed password is: '+ hashedPassword + ' ' + '<br /> '+'<a href='+'./'+'>Home</a>');
});
});
});
//makes the form for the list page work
app.get('/list', redirectLogin, function(req, res) {
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost';
MongoClient.connect(url, function (err, client) {
if (err) throw err;
var db = client.db('mybookshopdb');
db.collection('books').find().toArray((findErr, results) => {
if (findErr) throw findErr;
else
res.render('list.ejs', {availablebooks:results});
client.close();
});
});
});
//makes the form for the listusers page work
app.get('/listusers',redirectLogin, function(req, res) {
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost';
MongoClient.connect(url, function (err, client) {
if (err) throw err;
var db = client.db('listusersdb');
db.collection('users').find().toArray((findErr, results) => {
if (findErr) throw findErr;
else
res.render('listusers.ejs', {availableusers:results});
client.close();
});
});
});
//renders the addbook page
app.get('/addbook',redirectLogin, function(req,res){
res.render('addbook.html');
});//makes the form for the addbook page work correctly
app.post('/bookadded', function (req,res) {
// saving data in database
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost';
MongoClient.connect(url, function(err, client) {
if (err) throw err;
var db = client.db ('mybookshopdb');
db.collection('books').insertOne({
name: req.body.name,
price: req.body.price
});
client.close();
res.send(' This book is added to the database, name: '+ req.body.name + ' price '+ req.body.price + '<br />'+'<a href='+'./'+'>Home</a>');
});//prints out whent the book has been successfully added
});
//helps make the form work for the second list page
app.get('/list2', function(req, res) {
let sqlquery = "SELECT name, price FROM books WHERE price < 20"; // query database to get books under 20
// execute sql query
db.query(sqlquery, (err, result) => {
if (err) {
res.redirect('./');
}
res.render('list2.ejs', {availablebooks: result});
});//shows available books
});
/*
//renders the login page
app.get('/login', function (req,res) {
res.render('login.html');
});//helps make the form for the log in page work
app.post('/loggedin', function (req,res) {
const bcrypt = require('bcrypt');
const saltRounds = 10
const plainPassword = req.body.password;
var hashedPassword = req.body.password;
// saving data in database
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost';
MongoClient.connect(url, function(err, client) {
if (err) throw err;
var db = client.db('listusersdb');
db.collection('users').find({
name: req.body.name,
password: req.body.password
});
client.close();
// Load hashed password from your password database.
bcrypt.compare(plainPassword, hashedPassword, function(err, result) {
if (result == true){
res.send('you have logged in')
req.session.userId = req.body.username;
}
if (result == false){
res.send('incorrect details')
}
});
});
});
*/
//this will acess the login.HTML. users will input there username and it will ourput text if its valid or not
app.get('/login', function (req,res) {
res.render('login.html');
});
app.post('/loggedin', function (req,res) {
// This code is only provided for students who have not implemented the loggedin route in their lab6
// This code only let's the user login with 'user1' as the username
// when collecting form data (req.body.username),
// make sure to collect form data according to variables defined in login.html file,
// (Is it name or username or something else?)
if (req.body.name == 'user1'){
// *** save user session here, when login is successful
req.session.userId = req.body.name;
res.send('Login successful, You are now logged in, Welcome '+ req.body.name + '<br />'+'<a href='+'./'+'>Home</a>' );}
else{
res.send('Login unsuccessful, wrong username ' + '<br />'+'<a href='+'./'+'>Home</a>');}
})
app.get('/logout', redirectLogin, (req,res) => {
req.session.destroy(err => {
if (err) {
return res.redirect('./')
}
res.send('you are now logged out. <a href='+'./'+'>Home</a>');
})
})
//creates the delete user page
app.get('/deluser',redirectLogin, function (req,res) {
res.render('deluser.html');
});//helps make the delete user form work