Skip to content

Commit

Permalink
Merge pull request #4 from YURAresearch/master
Browse files Browse the repository at this point in the history
catch up
  • Loading branch information
Surya Dutta authored Oct 2, 2017
2 parents 29831b3 + 1ad5a5d commit 8cdc39c
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 126 deletions.
7 changes: 5 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var admin = require('./routes/admin');
var listings = require('./routes/listings');
var terms = require('./routes/terms');
var feedback = require('./routes/feedback');
var feedbackconfirm = require('./routes/feedback-confirm');

var config = require('./bin/config');
var depts = require('./bin/departments');
Expand Down Expand Up @@ -40,7 +39,6 @@ app.use('/', index);
//app.use('/listings', listings);
app.use('/terms', terms);
app.use('/feedback', feedback);
app.use('/feedback/confirm', feedbackconfirm);

app.use(session({
secret: config.sessionSecret,
Expand Down Expand Up @@ -77,6 +75,11 @@ app.post('/listings/removeFavorite/:listingid', listings);

app.post('/listings/addFavorite/:listingid', listings);

// letsencrypt ssl certificate confirmation
app.get('/.well-known/acme-challenge/:content', function(req, res) {
res.send('_Vz9KLmSfZNgaOvmV2SP29dF3UYbRqnxs4u6zuA9nRs.eQwz2bF_dTl8qE6neOOeozqWGRcP-4Jiolfu9-s9xtA')
})

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
Expand Down
1 change: 1 addition & 0 deletions bin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var config = {};
config.port = process.env.PORT || '3000';
config.host = process.env.HOST || 'localhost';
config.sessionSecret = process.env.SESSION_SECRET || 'e70a1e1ee4b8f662f78'
config.mailgun_key = process.env.MAILGUN_KEY

config.cookieduration = 1000 * 60 * 60 * 24; //one hour

Expand Down
2 changes: 1 addition & 1 deletion bin/departments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var sciences = [
'Applied Physics',
'Astronomy',
'Chemistry',
'Evolutionary Biology',
'Ecology and Evolutionary Biology',
'Geology and Geophysics',
'Molecular Biophysics and Biochemistry',
'Molecular, Cellular and Developmental Biology',
Expand Down
30 changes: 10 additions & 20 deletions models/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,31 @@ function searchHandler(searchString) {
return searchQuery;
}

function sortByParser(sortby){
if (sortby == 'name-asc') {
return "name ASC,";
} else if (sortby == 'name-desc') {
return "name DESC,";
} else if (sortby == 'dept-asc') {
return "departments ASC,";
} else if (sortby == 'dept-desc') {
return "departments DESC,";
} else {
return ""
}
function escapeRegExp(str) {
return str = str.replace(/\(/g, "\\\\(").replace(/\)/g, "\\\\)");
}

function saveSearch(searchQuery, deptFilter){
db.query("INSERT INTO searches (search_query, department_filter, datetime) VALUES ('" + searchQuery + "', '" + deptFilter + "', CURRENT_TIMESTAMP);");
}

function getAllListings(netID, sortby, callback) {
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings ORDER BY " + sortByParser(sortby) + "(SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),name,departments", callback);
function getAllListings(netID, callback) {
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings ORDER BY (SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),name,departments", callback);
}

function searchListings(searchString, netID, sortby, callback) {
function searchListings(searchString, netID, callback) {
saveSearch(searchString, "");
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings WHERE (to_tsvector(listings.name) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.description) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.keywords) @@ to_tsquery('"+searchHandler(searchString)+"')) ORDER BY "+ sortByParser(sortby) + "(SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),(SELECT CASE WHEN (ts_rank(to_tsvector(listings.departments), to_tsquery('"+searchHandler(searchString)+"'))) > 0 THEN 1 ELSE 0 END) DESC, listings.custom_desc DESC,(SELECT ts_rank(to_tsvector(listings.name), to_tsquery('"+searchHandler(searchString)+"'))) DESC,(SELECT ts_rank(to_tsvector(listings.description), to_tsquery('"+searchHandler(searchString)+"'))) DESC, (SELECT ts_rank(to_tsvector(listings.keywords), to_tsquery('"+searchHandler(searchString)+"'))) DESC,name,departments", callback);
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings WHERE (to_tsvector(listings.name) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.description) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.keywords) @@ to_tsquery('"+searchHandler(searchString)+"')) ORDER BY (SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),(SELECT CASE WHEN (ts_rank(to_tsvector(listings.departments), to_tsquery('"+searchHandler(searchString)+"'))) > 0 THEN 1 ELSE 0 END) DESC, listings.custom_desc DESC,(SELECT ts_rank(to_tsvector(listings.name), to_tsquery('"+searchHandler(searchString)+"'))) DESC,(SELECT ts_rank(to_tsvector(listings.description), to_tsquery('"+searchHandler(searchString)+"'))) DESC, (SELECT ts_rank(to_tsvector(listings.keywords), to_tsquery('"+searchHandler(searchString)+"'))) DESC,name,departments", callback);
}

function filterDepts(deptString, netID, sortby, callback) {
function filterDepts(deptString, netID, callback) {
saveSearch("",deptString);
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings WHERE LOWER(departments) LIKE LOWER('%" + deptString + "%') ORDER BY " + sortByParser(sortby) + "(SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),listings.custom_desc DESC,name,departments", callback);
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings WHERE departments ~* E'" + escapeRegExp(deptString) + ";' ORDER BY (SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),listings.custom_desc DESC,name,departments", callback);
}

function searchANDfilter(searchString, deptString, netID, sortby, callback) {
function searchANDfilter(searchString, deptString, netID, callback) {
saveSearch(searchString, deptString);
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings WHERE LOWER(departments) LIKE LOWER('%" + deptString + "%') AND (to_tsvector(listings.name) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.description) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.keywords) @@ to_tsquery('"+searchHandler(searchString)+"')) ORDER BY "+ sortByParser(sortby) + "(SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),(SELECT CASE WHEN (ts_rank(to_tsvector(listings.departments), to_tsquery('"+searchHandler(searchString)+"'))) > 0 THEN 1 ELSE 0 END) DESC, listings.custom_desc DESC,(SELECT ts_rank(to_tsvector(listings.name), to_tsquery('"+searchHandler(searchString)+"'))) DESC,(SELECT ts_rank(to_tsvector(listings.description), to_tsquery('"+searchHandler(searchString)+"'))) DESC, (SELECT ts_rank(to_tsvector(listings.keywords), to_tsquery('"+searchHandler(searchString)+"'))) DESC,name,departments", callback);
callbackData("SELECT *, exists(SELECT 1 FROM favorites WHERE favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "') AS isFavorite FROM listings WHERE departments ~* E'" + escapeRegExp(deptString) + ";' AND (to_tsvector(listings.name) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.description) @@ to_tsquery('"+searchHandler(searchString)+"') OR to_tsvector(listings.keywords) @@ to_tsquery('"+searchHandler(searchString)+"')) ORDER BY (SELECT favorites.listingid FROM favorites WHERE (favorites.listingid = listings.list_id AND favorites.netid = '" + netID + "')),(SELECT CASE WHEN (ts_rank(to_tsvector(listings.departments), to_tsquery('"+searchHandler(searchString)+"'))) > 0 THEN 1 ELSE 0 END) DESC, listings.custom_desc DESC,(SELECT ts_rank(to_tsvector(listings.name), to_tsquery('"+searchHandler(searchString)+"'))) DESC,(SELECT ts_rank(to_tsvector(listings.description), to_tsquery('"+searchHandler(searchString)+"'))) DESC, (SELECT ts_rank(to_tsvector(listings.keywords), to_tsquery('"+searchHandler(searchString)+"'))) DESC,name,departments", callback);
}

//user functions
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"express-session": "^1.15.2",
"handlebars-paginate": "^0.1.0",
"hbs": "^4.0.1",
"mailgun": "^0.5.0",
"morgan": "~1.7.0",
"nodemailer": "^3.1.3",
"pg-promise": "^5.5.8",
"postmark": "^1.3.1",
"query-string": "^5.0.0",
"readmore-js": "^2.1.0",
"request": "^2.79.0",
"serve-favicon": "~2.3.2",
Expand Down
Binary file added public/images/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions public/stylesheets/listingsstyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ form#filter {
margin-bottom: 20px;
}
#resultcount {
font-size: 14px;
color: #C0C0C0;
font-size: 16px;
color: #A0A0A0;
padding-bottom: 20px;
border: none;
}
Expand Down Expand Up @@ -58,7 +58,8 @@ form#filter {
list-style-type: none;
}
.list li {
margin-bottom: 5px;
margin-bottom: 10px;
box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}
.list div:hover {
background-color: #FAFDFE;
Expand All @@ -80,6 +81,9 @@ form#filter {
.card-row-properties dd {
margin-right: 8px;
}
.list .card-row, .list li {
border-radius: 4px;
}
a.website:focus {
color: #008675;
}
Expand Down Expand Up @@ -110,13 +114,18 @@ mark {
#remFav, #addFav {
color: #FFFFFF;
border: none;
width: 200px;
float: right;
}
#remFav {
background-color: #CE962A;
}
#addFav {
background-color: #279678;
}
.fav-form {
height: 40px;
}

/* Buttons and filters */
#searchbox, #deptDropdown .selectize-input {
Expand Down
38 changes: 0 additions & 38 deletions routes/feedback-confirm.js

This file was deleted.

65 changes: 29 additions & 36 deletions routes/feedback.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
var express = require('express');
var router = express.Router();
const nodemailer = require('nodemailer');
var config = require('../bin/config');

var Mailgun = require('mailgun').Mailgun;

var mg = new Mailgun(config.mailgun_key);

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('feedback', {
title: 'Feedback'
});
res.render('feedback', {
title: 'Feedback'
});
});

router.post('/', function(req, res) {
if (req.query.submit) {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'undergradresearch'
}
});

// setup email data with unicode symbols
var mailOptions = {
from:'[email protected]', // sender address
to: '[email protected]', // list of receivers
replyTo: req.body.email,
subject: '[RDB Feedback] '+req.body.subject, // Subject line
html: '<h3>Name</h3><p>' + req.body.name + '</p><h1>Message</h1><p>' + req.body.message+ '</p>', // plain text body
};
if(req.body.email){
var sender = (req.body.name + ' <' + req.body.email + '>');
} else{
var sender = ('Anonymous User <[email protected]>');
}
var subject = (req.body.subject || 'RDB Feedback');
var message = (req.body.message);

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.render('feedback', {
title: 'Feedback',
message: 'Error: Please try again, or email us at [email protected].',
});
} else {
res.render('feedback', {
title: 'Feedback',
message: 'Your message has been sent! Thank you for your feedback.',
});
}
mg.sendText(sender, '[email protected]', subject, message,
function(err) {
if (err) {
console.log(err);
res.render('feedback', {
title: 'Feedback',
message: 'Error: Please try again, or email us at [email protected].',
});
} else {
res.render('feedback', {
title: 'Feedback',
message: 'Your message has been sent! Thank you for your feedback.',
});
} else {
res.send('Error');
}
}
});
});

module.exports = router;
32 changes: 23 additions & 9 deletions routes/listings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var postgresModel = require('../models/postgres.js');
var hbs = require('hbs');
var paginate = require('handlebars-paginate');
var ua = require('universal-analytics');
var queryString = require('querystring');


hbs.registerHelper('paginate', paginate);
hbs.registerHelper('paginate-link', function(url, pageNum) {
Expand Down Expand Up @@ -38,15 +40,19 @@ function listAll(req, res) {

if (req.session.loggedin == false || !(req.session.loggedin)) res.redirect('/users'); //if user info not loaded, redirect to users route


if (req.query){
req.session.lastquery = queryString.stringify(req.query);
console.log(req.session.lastquery);
}

var callback = function(listings) {

res.render('listings', {
title: 'Listings',
searchPlaceholder: req.query.search || '',
deptPlaceholder: req.query.departments || 'Departments',
depts: depts,
sortNameDesc: (req.query.sort=="name-asc"),
sortDeptDesc: (req.query.sort=="dept-asc"),
listings: listings.slice((req.query.p - 1) * resultsPerPage || 0, req.query.p * resultsPerPage || resultsPerPage), //gets entries for current page
numberOfResults: listings.length,
url: req.url,
Expand All @@ -65,34 +71,42 @@ function listAll(req, res) {
}
if (req.query.search) {
if (req.query.departments && req.query.departments != "Departments") {
postgresModel.searchANDfilter(req.query.search, req.query.departments, req.session.cas_user, req.query.sort, callback);
postgresModel.searchANDfilter(req.query.search, req.query.departments, req.session.cas_user, callback);
} else {
postgresModel.searchListings(req.query.search, req.session.cas_user, req.query.sort, callback);
postgresModel.searchListings(req.query.search, req.session.cas_user, callback);
}
} else {
if (req.query.departments && req.query.departments != "Departments") {
postgresModel.filterDepts(req.query.departments, req.session.cas_user, req.query.sort, callback);
postgresModel.filterDepts(req.query.departments, req.session.cas_user, callback);
} else {
postgresModel.getAllListings(req.session.cas_user, req.query.sort, callback);
postgresModel.getAllListings(req.session.cas_user, callback);
}
}
}

//GET home page.
router.get('/listings', listAll);

router.post('/listings/addFavorite/:listingid',function(req,res){
router.post('/listings/addFavorite/:listingid/',function(req,res){
postgresModel.addFavorite(req.session.cas_user, req.params.listingid, function(log) {
console.log('Entry '+ req.params.listingid.toString() +' Added To Favorites');
res.redirect('/listings');
if (req.session.lastquery){
res.redirect('/listings?'+req.session.lastquery);
} else{
res.redirect('/listings');
}
});
});

router.post('/listings/removeFavorite/:listingid',function(req,res){
console.log(req.params.listingid);
postgresModel.removeFavorite(req.session.cas_user, req.params.listingid, function(log) {
console.log('Entry '+ req.params.listingid.toString() +' Removed From Favorites');
res.redirect('/listings');
if (req.session.lastquery){
res.redirect('/listings?'+req.session.lastquery);
} else{
res.redirect('/listings');
}
});
});

Expand Down
4 changes: 2 additions & 2 deletions views/feedback.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<p>Let us know what you think, and what we can do better! <br/> If you would prefer to keep your comments anonymous, please only fill out the message textbox.</p>
<br/>
<div class="contact-form-wrapper clearfix background-white p30 boxMD" id="feedback-form">
<form class="contact-form" name='feedback' id='feedback' method='post' action='/feedback?submit=1'>
<form class="contact-form" name='feedback' id='feedback' method='post' action='/feedback'>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
Expand All @@ -27,7 +27,7 @@

<div class="col-sm-6">
<div class="form-group">
<label for="contact-form-email">Email address</label>
<label for="contact-form-email">Email Address</label>
<input type="text" name="email" id="contact-form-email" class="form-control">
</div><!-- /.form-group -->
</div><!-- /.col-* -->
Expand Down
Loading

0 comments on commit 8cdc39c

Please sign in to comment.