Skip to content

Commit

Permalink
Merge pull request #33 from fac-14/backend-ref
Browse files Browse the repository at this point in the history
Backend ref
  • Loading branch information
sbinlondon authored Sep 18, 2018
2 parents 29dd074 + e73b402 commit f7aa704
Show file tree
Hide file tree
Showing 8 changed files with 1,607 additions and 1,545 deletions.
2,945 changes: 1,479 additions & 1,466 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"node-sass-middleware": "^0.11.0",
"pg": "^7.4.3",
"pg-promise": "^8.4.6",
"query-string": "^6.1.0",
"querystring": "^0.2.0",
"sequelize": "^4.38.0",
"serve-favicon": "^2.5.0"
Expand Down
24 changes: 13 additions & 11 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ const background = require('./background');
const resources = require('./resources');
const error = require('./error');
const moreinfo = require('./moreinfo');
const sendinfo = require('./sendinfo');
const results = require('./results');

router.use(bodyParser.urlencoded({extended: true}));
router.use(bodyParser.urlencoded({ extended: true }));
router.get('/', home.get);
router.get('/welcome', welcome.get);
router.get('/background', background.get);
router.get('/resources', resources.get);
router.get('/more-info', moreinfo.get);
router.get('/sendinfo', sendinfo.get); //used to be results.get
router.get('/results', results.get);

// trigger 500 error for testing

if(process.env.NODE_ENV === 'test') {
if (process.env.NODE_ENV === 'test') {
const triggerError = () => (req, res, next) => {
console.log('Testing 500')
try {
throw new Error('This function causes an error')
} catch (error) {
console.log("caught error");
next(error)
}
};
router.get("/error", triggerError());
console.log('Testing 500')
try {
throw new Error('This function causes an error')
} catch (error) {
console.log("caught error");
next(error)
}
};
router.get("/error", triggerError());
}

// client error routes
Expand Down
10 changes: 7 additions & 3 deletions src/controllers/results.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
exports.get = (req, res) => {
res.render('results', {
layout: 'scrollable'
});
res.render('results',
{
layout: 'scrollable',
progressamt: '100',
title: 'Results',
pageInfo: 'Based on the checkboxes you have selected, here are your results.'
});
}
85 changes: 85 additions & 0 deletions src/controllers/sendinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const queries = require('../model/index')
const queryString = require('query-string');

exports.get = (req, response) => {
var queryObj = queryString.parseUrl(req.url).query;
var backgroundArr = queryObj.bg.split(",");
var resourceArr = queryObj.rsc.split(",");
console.log("bg Arr is", backgroundArr, "rsc Arr is", resourceArr);

queries.getData(backgroundArr, resourceArr)
.then( dbObj => {
const resultArray = arrangeArray(dbObj.rows);
console.log('resultArray', resultArray);
const outArray = filterByType(resultArray);
console.log('OUTPUT ARRAY', outArray);
//res.render('results', {outArray});
response.send(outArray);
})
.catch(err => console.log(err));

function filterByType(inArray) {
const types = ['meetup', 'online course', 'article', 'classroom course'];
const outArr = []
types.forEach(mytype => {
//outArr[meetup]= filtered result
// setting keys to values from filter function
outArr[mytype] = inArray.filter(function (value) {
return value.resource_type == mytype;
})
})
return outArr;
}
//to arrange repeated resource as {resource_id='1', resource_name='codebar', direct=['age','ethnicity'], indirect=['disability']},....
function arrangeArray(inArray) {
var resultArray = []; //empty array to start with
for (let i = 0; i < inArray.length; i++) {
if (i == 0) {
//populate new resouce in our result array
resultArrIndx = 0;
resultArray.push({
resource_id: inArray[i].resource_id, resource_name: inArray[i].resource_name,
url: inArray[i].url, resource_type: inArray[i].resource_type
});
resultArray[resultArrIndx].direct = [];
resultArray[resultArrIndx].indirect = [];
//if incoming demo_tag_relevance is direct push it into resultArray[resultArrIndx].direct array else
//push into resultArray[resultArrIndx].indirect array
if (inArray[i].relevance === 'direct') {
resultArray[resultArrIndx].direct.push(inArray[i].tag_name);
} else {
resultArray[resultArrIndx].indirect.push(inArray[i].tag_name);
}
resultArrIndx++;
}
else if (resultArray[resultArrIndx - 1].resource_id == inArray[i].resource_id) {
if (inArray[i].demo_tag_relevance === 'direct') {
resultArray[resultArrIndx - 1].direct.push(inArray[i].tag_name);
} else {
resultArray[resultArrIndx - 1].indirect.push(inArray[i].tag_name);
}
}
else {
//populate new resouce in our result array
//create new key demo_tag_relevance and push {tagname:'tagname value', relevance:'rel value'}
resultArray.push({
resource_id: inArray[i].resource_id, resource_name: inArray[i].resource_name,
url: inArray[i].url, resource_type: inArray[i].resource_type
});
resultArray[resultArrIndx].direct = [];
resultArray[resultArrIndx].indirect = [];
//if incoming demo_tag_relevance is direct push it into resultArray[resultArrIndx].direct array else
//push into resultArray[resultArrIndx].indirect array
if (inArray[i].relevance === 'direct') {
resultArray[resultArrIndx].direct.push(inArray[i].tag_name);
} else {
resultArray[resultArrIndx].indirect.push(inArray[i].tag_name);
}
resultArrIndx++;
}
} //end of for loop

return resultArray;
}

}
81 changes: 19 additions & 62 deletions src/model/getData.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
const db = require("../database/db_connection");
var inputArray = ["age"];
const getResource = inputArray => {
var dataQuery = `select resource.resource_id ,resource.resource_name,resource.url, demographic_tag. demographic_tag_id, demographic_tag.tag_name, demographic_resource_link.relevance from resource INNER JOIN demographic_resource_link ON resource.resource_id = demographic_resource_link.link_resource_id INNER JOIN demographic_tag ON demographic_resource_link.link_demographic_tag_id = demographic_tag. demographic_tag_id WHERE demographic_tag.tag_name = '${
inputArray[0]
}'`;
// 'refugee' OR demographic_tag.tag_name = 'ethnicity' OR demographic_tag.tag_name = 'age' " ;

const getData = (bgArray, rscArray) => {
console.log('INPUTARR', bgArray, rscArray);
var dataQuery = `select resource.resource_id ,resource.resource_name,resource.url, resource.resource_type, demographic_tag. demographic_tag_id, demographic_tag.tag_name, demographic_resource_link.relevance from resource INNER JOIN demographic_resource_link ON resource.resource_id = demographic_resource_link.link_resource_id INNER JOIN demographic_tag ON demographic_resource_link.link_demographic_tag_id = demographic_tag. demographic_tag_id WHERE (demographic_tag.tag_name='${
bgArray[0]
}'`;


//iterator appends users selected demographic tags
for (let i = 0; i < inputArray.length - 1; i++) {
dataQuery += ` OR demographic_tag.tag_name = '${inputArray[i]}'`;
for (let i = 1; i < bgArray.length; i++) {
dataQuery += ` OR demographic_tag.tag_name = '${bgArray[i].trim()}'`;
}

dataQuery += `) AND (resource.resource_type = '${rscArray[0].trim()}'`;

for (let i = 1; i < rscArray.length; i++) {
dataQuery += ` OR resource.resource_type = '${rscArray[i].trim()}'`;
}

dataQuery += ')'

console.log("DATAQUERY=", dataQuery);

return new Promise((resolve, reject) => {
Expand All @@ -18,58 +28,5 @@ const getResource = inputArray => {
.catch(err => reject(err));
});
};
module.exports = getResource;

console.log("HERE");

// getResource(['age', 'disability', 'gender'])
// .then(res => console.log(res.rows))
// .catch(err => console.log(err))
const resultArr = getResource(["age", "disability", "gender"])
.then(res => {
var resultArr = res.rows;
var newAns = resultArr.reduce(reducer);
console.log("HELLO", newAns);
})
.catch(err => console.log(err));

////////

//myArray.reduce(reducer)
// var myarr = [1, 2, 3];
// const reducer = (accumulator, currentvalue) => {
// return accumulator + currentvalue;
// };

// var sum = myarr.reduce(reducer);
// console.log(sum);
var accumulator = []; //empty array to start with
const reducer = (accumulator, currentvalue) => {
// accumulator.forEach(function(element) {
// if (element.resource_id == currentvalue.resource_id) {
// //if one of the relevance is direct set our result's relevance to direct
// if (element.relevance == "direct" || currentvalue.relevance == "direct") {
// element.relevane = "direct";
// }
// } else {
// //populate new resouce in our result array
// accumulator.push(currentvalue);
// }
// });
for (let i = 0; i < accumulator.length; i++) {
if (accumulator[i].resource_id == currentvalue.resource_id) {
if (
accumulator[i].relevance == "direct" ||
currentvalue.relevance == "direct"
) {
accumulator[i].relevance = "direct";
}
} else {
//populate new resouce in our result array
accumulator.push(currentvalue);
}
}
};
module.exports = getData;

//var newAns = resultArr.reduce(reducer);
//console.log("HELLO", newAns);
2 changes: 1 addition & 1 deletion src/model/get_resource_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const db = require("../database/db_connection");

const getResourceDetail = resourceId => {
return new Promise((resolve, reject) => {
db.query(`SELECT * from resource WHERE id ='${resourceId}' `)
db.query(`SELECT * from resource WHERE resource_id ='${resourceId}' `)
.then(res => resolve(res))
.catch(err => reject(err));
});
Expand Down
4 changes: 2 additions & 2 deletions src/model/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const getDemoTagId = require("./get_demo_tag_id");
const getResourceDetail = require("./get_resource_detail");
const getResource = require("./getResource");
const getData = require("./getData");

module.exports = {
getDemoTagId: getDemoTagId,
getResourceDetail: getResourceDetail,
getResource: getResource
getData: getData
};

0 comments on commit f7aa704

Please sign in to comment.